summaryrefslogtreecommitdiff
path: root/src/os_windows/os_clock.c
blob: e548729b982738ba719ce069cd871bd4a2b5273a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2001, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"

/*
 * __os_gettime --
 *	Return the current time-of-day clock in seconds and nanoseconds.
 */
void
__os_gettime(env, tp, monotonic)
	ENV *env;
	db_timespec *tp;
	int monotonic;
{
	if (monotonic) {
		/*
		 * The elapsed time is stored as a DWORD value, so time wraps
		 * around to zero if the system runs for 49.7 days.  Initialize
		 * a base value with 50 days worth of seconds, and add 50 more
		 * days every time the counter wraps.  That ensures we always
		 * move forward.
		 *
		 * It's possible this code could race, but the danger is we
		 * would increment base_seconds more than once per wrap and
		 * eventually overflow, which is a pretty remote possibility.
		 */
#define	TIMER_WRAP_SECONDS	(50 * 24 * 60 * 60)
		static DWORD last_ticks;
		static time_t base_seconds;
		DWORD ticks;

		ticks = GetTickCount();
		if (ticks < last_ticks)
			base_seconds += TIMER_WRAP_SECONDS;
		last_ticks = ticks;
		tp->tv_sec = base_seconds + (u_int32_t)(ticks / 1000);
		tp->tv_nsec = (u_int32_t)((ticks % 1000) * NS_PER_MS);
	} else {
#ifdef DB_WINCE
		FILETIME ft;
		LARGE_INTEGER large_int;
		LONGLONG ns_since_epoch, utc1970;
		SYSTEMTIME st;

		(void)GetSystemTime(&st);
		(void)SystemTimeToFileTime(&st, &ft);

		/*
		 * A FILETIME expresses time as 100 nanosecond chunks from
		 * Jan 1, 1601; convert to a timespec where the time is
		 * is expressed in seconds and nanoseconds from Jan 1, 1970.
		 *
		 * UTC_1970 is the number of 100-nano-second chunks from
		 * 1601 to 1970.
		 */
#define	NS100_PER_SEC	(NS_PER_SEC / 100)
#define	UTC_1970	(((LONGLONG)27111902 << 32) + (LONGLONG)3577643008)
		memcpy(&large_int, &ft, sizeof(large_int));
		utc1970 = UTC_1970;
		ns_since_epoch = (large_int.QuadPart - utc1970);
		tp->tv_sec = (time_t)(ns_since_epoch / NS100_PER_SEC);
		tp->tv_nsec = (long)(ns_since_epoch % NS100_PER_SEC);
#else
		struct _timeb now;

		_ftime(&now);
		tp->tv_sec = now.time;
		tp->tv_nsec = now.millitm * NS_PER_MS;
#endif
	}
}