2 * Time utility functions.
3 * Author: Paolo Molaro (<lupus@ximian.com>)
4 * Copyright (C) 2008 Novell, Inc.
7 #include <utils/mono-time.h>
11 #define MTICKS_PER_SEC 10000000
17 mono_msec_ticks (void)
19 /* GetTickCount () is reportedly monotonic */
20 return GetTickCount ();
23 /* Returns the number of 100ns ticks from unspecified time: this should be monotonic */
25 mono_100ns_ticks (void)
27 static LARGE_INTEGER freq;
30 if (!freq.QuadPart && !QueryPerformanceFrequency (&freq))
31 return mono_100ns_datetime ();
32 QueryPerformanceCounter (&value);
33 return value.QuadPart * MTICKS_PER_SEC / freq.QuadPart;
37 * Magic number to convert FILETIME base Jan 1, 1601 to DateTime - base Jan, 1, 0001
39 #define FILETIME_ADJUST ((guint64)504911232000000000LL)
41 /* Returns the number of 100ns ticks since 1/1/1, UTC timezone */
43 mono_100ns_datetime (void)
47 if (sizeof(ft) != sizeof(FILETIME))
48 g_assert_not_reached ();
50 GetSystemTimeAsFileTime ((FILETIME*) &ft);
51 return FILETIME_ADJUST + ft.QuadPart;
56 #ifdef HAVE_SYS_TIME_H
60 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
61 #include <sys/param.h>
62 #include <sys/sysctl.h>
70 #if defined(PLATFORM_MACOSX) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
74 struct timeval boottime;
79 mib [1] = KERN_BOOTTIME;
81 size = sizeof(boottime);
83 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
84 return (gint64)((now - boottime.tv_sec) * MTICKS_PER_SEC);
86 FILE *uptime = fopen ("/proc/uptime", "r");
89 if (fscanf (uptime, "%lf", &upt) == 1) {
90 gint64 now = mono_100ns_ticks ();
92 return now - (gint64)(upt * MTICKS_PER_SEC);
97 /* a made up uptime of 300 seconds */
98 return (gint64)300 * MTICKS_PER_SEC;
101 /* Returns the number of milliseconds from boot time: this should be monotonic */
103 mono_msec_ticks (void)
105 static gint64 boot_time = 0;
108 boot_time = get_boot_time ();
109 now = mono_100ns_ticks ();
110 /*printf ("now: %llu (boot: %llu) ticks: %llu\n", (gint64)now, (gint64)boot_time, (gint64)(now - boot_time));*/
111 return (now - boot_time)/10000;
114 /* Returns the number of 100ns ticks from unspecified time: this should be monotonic */
116 mono_100ns_ticks (void)
119 #ifdef CLOCK_MONOTONIC
120 struct timespec tspec;
121 static struct timespec tspec_freq = {0};
122 static int can_use_clock = 0;
123 if (!tspec_freq.tv_nsec) {
124 can_use_clock = clock_getres (CLOCK_MONOTONIC, &tspec_freq) == 0;
125 /*printf ("resolution: %lu.%lu\n", tspec_freq.tv_sec, tspec_freq.tv_nsec);*/
128 if (clock_gettime (CLOCK_MONOTONIC, &tspec) == 0) {
129 /*printf ("time: %lu.%lu\n", tspec.tv_sec, tspec.tv_nsec); */
130 return ((gint64)tspec.tv_sec * MTICKS_PER_SEC + tspec.tv_nsec / 100);
135 if (gettimeofday (&tv, NULL) == 0)
136 return ((gint64)tv.tv_sec * 1000000 + tv.tv_usec) * 10;
141 * Magic number to convert a time which is relative to
142 * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
144 #define EPOCH_ADJUST ((guint64)62135596800LL)
146 /* Returns the number of 100ns ticks since 1/1/1, UTC timezone */
148 mono_100ns_datetime (void)
151 if (gettimeofday (&tv, NULL) == 0)
152 return (((gint64)tv.tv_sec + EPOCH_ADJUST) * 1000000 + tv.tv_usec) * 10;