Merge pull request #2803 from BrzVlad/feature-conc-pinned-scan
[mono.git] / mono / utils / mono-time.c
index 2bf306e49d241a9d76c9246dac63c85446c6799d..5f0168ebb7475b94886d44a002331d2aa1184842 100644 (file)
@@ -2,12 +2,20 @@
  * Time utility functions.
  * Author: Paolo Molaro (<lupus@ximian.com>)
  * Copyright (C) 2008 Novell, Inc.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
-#include <utils/mono-time.h>
+#include <config.h>
 #include <stdlib.h>
 #include <stdio.h>
 
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#include <utils/mono-time.h>
+
+
 #define MTICKS_PER_SEC 10000000
 
 #ifdef HOST_WIN32
@@ -41,12 +49,7 @@ mono_100ns_ticks (void)
        return (cur_time - start_time) * (double)MTICKS_PER_SEC / freq.QuadPart;
 }
 
-/*
- * Magic number to convert FILETIME base Jan 1, 1601 to DateTime - base Jan, 1, 0001
- */
-#define FILETIME_ADJUST ((guint64)504911232000000000LL)
-
-/* Returns the number of 100ns ticks since 1/1/1, UTC timezone */
+/* Returns the number of 100ns ticks since Jan 1, 1601, UTC timezone */
 gint64
 mono_100ns_datetime (void)
 {
@@ -56,17 +59,16 @@ mono_100ns_datetime (void)
                g_assert_not_reached ();
 
        GetSystemTimeAsFileTime ((FILETIME*) &ft);
-       return FILETIME_ADJUST + ft.QuadPart;
+       return ft.QuadPart;
 }
 
 #else
 
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
 
-#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+#if defined (HAVE_SYS_PARAM_H)
 #include <sys/param.h>
+#endif
+#if defined(HAVE_SYS_SYSCTL_H)
 #include <sys/sysctl.h>
 #endif
 
@@ -80,7 +82,7 @@ mono_100ns_datetime (void)
 static gint64
 get_boot_time (void)
 {
-#if defined(PLATFORM_MACOSX) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+#if defined (HAVE_SYS_PARAM_H) && defined (KERN_BOOTTIME)
        int mib [2];
        size_t size;
        time_t now;
@@ -160,20 +162,26 @@ mono_100ns_ticks (void)
 }
 
 /*
- * Magic number to convert a time which is relative to
- * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
+ * Magic number to convert unix epoch start to windows epoch start
+ * Jan 1, 1970 into a value which is relative to Jan 1, 1601.
  */
-#define EPOCH_ADJUST    ((guint64)62135596800LL)
+#define EPOCH_ADJUST    ((guint64)11644473600LL)
 
-/* Returns the number of 100ns ticks since 1/1/1, UTC timezone */
+/* Returns the number of 100ns ticks since 1/1/1601, UTC timezone */
 gint64
 mono_100ns_datetime (void)
 {
        struct timeval tv;
        if (gettimeofday (&tv, NULL) == 0)
-               return (((gint64)tv.tv_sec + EPOCH_ADJUST) * 1000000 + tv.tv_usec) * 10;
+               return mono_100ns_datetime_from_timeval (tv);
        return 0;
 }
 
+gint64
+mono_100ns_datetime_from_timeval (struct timeval tv)
+{
+       return (((gint64)tv.tv_sec + EPOCH_ADJUST) * 1000000 + tv.tv_usec) * 10;
+}
+
 #endif