X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Futils%2Fmono-time.c;h=3c940e73ddfb85b82607dafd526cad6b388219aa;hb=0416019a35f7df989bc4be9b2613e61a6ef29ff6;hp=4e923e64b1176671177dc45482372ee00a0bd352;hpb=8ef4e4703172fd32ada3f74b22df5203b4683493;p=mono.git diff --git a/mono/utils/mono-time.c b/mono/utils/mono-time.c index 4e923e64b11..3c940e73ddf 100644 --- a/mono/utils/mono-time.c +++ b/mono/utils/mono-time.c @@ -4,10 +4,17 @@ * Copyright (C) 2008 Novell, Inc. */ -#include +#include #include #include +#ifdef HAVE_SYS_TIME_H +#include +#endif + +#include + + #define MTICKS_PER_SEC 10000000 #ifdef HOST_WIN32 @@ -25,20 +32,23 @@ gint64 mono_100ns_ticks (void) { static LARGE_INTEGER freq; + static UINT64 start_time; + UINT64 cur_time; LARGE_INTEGER value; - if (!freq.QuadPart && !QueryPerformanceFrequency (&freq)) - return mono_100ns_datetime (); + if (!freq.QuadPart) { + if (!QueryPerformanceFrequency (&freq)) + return mono_100ns_datetime (); + QueryPerformanceCounter (&value); + start_time = value.QuadPart; + } QueryPerformanceCounter (&value); - return value.QuadPart * MTICKS_PER_SEC / freq.QuadPart; + cur_time = value.QuadPart; + /* we use unsigned numbers and return the difference to avoid overflows */ + 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) { @@ -48,26 +58,30 @@ 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 -#endif -#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) +#if defined (HAVE_SYS_PARAM_H) #include +#endif +#if defined(HAVE_SYS_SYSCTL_H) #include #endif +#if defined(PLATFORM_MACOSX) +#include +#include +#endif + #include 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; @@ -131,6 +145,15 @@ mono_100ns_ticks (void) } } +#elif defined(PLATFORM_MACOSX) + /* http://developer.apple.com/library/mac/#qa/qa1398/_index.html */ + static mach_timebase_info_data_t timebase; + guint64 now = mach_absolute_time (); + if (timebase.denom == 0) { + mach_timebase_info (&timebase); + timebase.denom *= 100; /* we return 100ns ticks */ + } + return now * timebase.numer / timebase.denom; #endif if (gettimeofday (&tv, NULL) == 0) return ((gint64)tv.tv_sec * 1000000 + tv.tv_usec) * 10; @@ -138,20 +161,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