X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Futils%2Fmono-time.c;h=06f9f217b88cf6ce3a51b9fe65182ae38f182995;hb=f6b8e1f10d5cd18826aad50e5a2b8d2aabf3222a;hp=30fe621b6f3ee6ddb58fac746284a3d3c068ae50;hpb=018e826d47309aeb3cf2e9883c034ddd86da2a16;p=mono.git diff --git a/mono/utils/mono-time.c b/mono/utils/mono-time.c index 30fe621b6f3..06f9f217b88 100644 --- a/mono/utils/mono-time.c +++ b/mono/utils/mono-time.c @@ -2,22 +2,41 @@ * Time utility functions. * Author: Paolo Molaro () * Copyright (C) 2008 Novell, Inc. + * Licensed under the MIT license. See LICENSE file in the project root for full license information. */ -#include +#include #include #include -#define MTICKS_PER_SEC 10000000 +#ifdef HAVE_SYS_TIME_H +#include +#endif + +#include + + +#define MTICKS_PER_SEC (10 * 1000 * 1000) + +gint64 +mono_msec_ticks (void) +{ + return mono_100ns_ticks () / 10 / 1000; +} #ifdef HOST_WIN32 #include -guint32 -mono_msec_ticks (void) +#ifndef _MSC_VER +/* we get "error: implicit declaration of function 'GetTickCount64'" */ +WINBASEAPI ULONGLONG WINAPI GetTickCount64(void); +#endif + +gint64 +mono_msec_boottime (void) { /* GetTickCount () is reportedly monotonic */ - return GetTickCount (); + return GetTickCount64 (); } /* Returns the number of 100ns ticks from unspecified time: this should be monotonic */ @@ -41,12 +60,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,14 +70,11 @@ 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 (HAVE_SYS_PARAM_H) #include @@ -102,7 +113,7 @@ get_boot_time (void) if (uptime) { double upt; if (fscanf (uptime, "%lf", &upt) == 1) { - gint64 now = mono_100ns_ticks (); + gint64 now = mono_100ns_datetime (); fclose (uptime); return now - (gint64)(upt * MTICKS_PER_SEC); } @@ -114,14 +125,14 @@ get_boot_time (void) } /* Returns the number of milliseconds from boot time: this should be monotonic */ -guint32 -mono_msec_ticks (void) +gint64 +mono_msec_boottime (void) { static gint64 boot_time = 0; gint64 now; if (!boot_time) boot_time = get_boot_time (); - now = mono_100ns_ticks (); + now = mono_100ns_datetime (); /*printf ("now: %llu (boot: %llu) ticks: %llu\n", (gint64)now, (gint64)boot_time, (gint64)(now - boot_time));*/ return (now - boot_time)/10000; } @@ -131,7 +142,16 @@ gint64 mono_100ns_ticks (void) { struct timeval tv; -#ifdef CLOCK_MONOTONIC +#if 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; +#elif defined(CLOCK_MONOTONIC) struct timespec tspec; static struct timespec tspec_freq = {0}; static int can_use_clock = 0; @@ -145,16 +165,6 @@ mono_100ns_ticks (void) return ((gint64)tspec.tv_sec * MTICKS_PER_SEC + tspec.tv_nsec / 100); } } - -#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; @@ -162,20 +172,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