1 /*
2  * gettimeofday.c
3  *	  Win32 gettimeofday() replacement
4  *
5  * src/port/gettimeofday.c
6  *
7  * Copyright (c) 2003 SRA, Inc.
8  * Copyright (c) 2003 SKC, Inc.
9  *
10  * Permission to use, copy, modify, and distribute this software and
11  * its documentation for any purpose, without fee, and without a
12  * written agreement is hereby granted, provided that the above
13  * copyright notice and this paragraph and the following two
14  * paragraphs appear in all copies.
15  *
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
17  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
18  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
19  * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
20  * OF THE POSSIBILITY OF SUCH DAMAGE.
21  *
22  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
25  * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
26  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27  */
28 
29 #include "c.h"
30 
31 #include <sys/time.h>
32 
33 /* FILETIME of Jan 1 1970 00:00:00, the PostgreSQL epoch */
34 static const unsigned __int64 epoch = UINT64CONST(116444736000000000);
35 
36 /*
37  * FILETIME represents the number of 100-nanosecond intervals since
38  * January 1, 1601 (UTC).
39  */
40 #define FILETIME_UNITS_PER_SEC	10000000L
41 #define FILETIME_UNITS_PER_USEC 10
42 
43 /*
44  * Both GetSystemTimeAsFileTime and GetSystemTimePreciseAsFileTime share a
45  * signature, so we can just store a pointer to whichever we find. This
46  * is the pointer's type.
47  */
48 typedef VOID(WINAPI * PgGetSystemTimeFn) (LPFILETIME);
49 
50 /* One-time initializer function, must match that signature. */
51 static void WINAPI init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime);
52 
53 /* Storage for the function we pick at runtime */
54 static PgGetSystemTimeFn pg_get_system_time = &init_gettimeofday;
55 
56 /*
57  * One time initializer.  Determine whether GetSystemTimePreciseAsFileTime
58  * is available and if so, plan to use it; if not, fall back to
59  * GetSystemTimeAsFileTime.
60  */
61 static void WINAPI
init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime)62 init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime)
63 {
64 	/*
65 	 * Because it's guaranteed that kernel32.dll will be linked into our
66 	 * address space already, we don't need to LoadLibrary it and worry about
67 	 * closing it afterwards, so we're not using Pg's dlopen/dlsym() wrapper.
68 	 *
69 	 * We'll just look up the address of GetSystemTimePreciseAsFileTime if
70 	 * present.
71 	 *
72 	 * While we could look up the Windows version and skip this on Windows
73 	 * versions below Windows 8 / Windows Server 2012 there isn't much point,
74 	 * and determining the windows version is its self somewhat Windows
75 	 * version and development SDK specific...
76 	 */
77 	pg_get_system_time = (PgGetSystemTimeFn) GetProcAddress(
78 															GetModuleHandle(TEXT("kernel32.dll")),
79 															"GetSystemTimePreciseAsFileTime");
80 	if (pg_get_system_time == NULL)
81 	{
82 		/*
83 		 * The expected error from GetLastError() is ERROR_PROC_NOT_FOUND, if
84 		 * the function isn't present. No other error should occur.
85 		 *
86 		 * We can't report an error here because this might be running in
87 		 * frontend code; and even if we're in the backend, it's too early to
88 		 * elog(...) if we get some unexpected error.  Also, it's not a
89 		 * serious problem, so just silently fall back to
90 		 * GetSystemTimeAsFileTime irrespective of why the failure occurred.
91 		 */
92 		pg_get_system_time = &GetSystemTimeAsFileTime;
93 	}
94 
95 	(*pg_get_system_time) (lpSystemTimeAsFileTime);
96 }
97 
98 /*
99  * timezone information is stored outside the kernel so tzp isn't used anymore.
100  *
101  * Note: this function is not for Win32 high precision timing purposes. See
102  * elapsed_time().
103  */
104 int
gettimeofday(struct timeval * tp,struct timezone * tzp)105 gettimeofday(struct timeval *tp, struct timezone *tzp)
106 {
107 	FILETIME	file_time;
108 	ULARGE_INTEGER ularge;
109 
110 	(*pg_get_system_time) (&file_time);
111 	ularge.LowPart = file_time.dwLowDateTime;
112 	ularge.HighPart = file_time.dwHighDateTime;
113 
114 	tp->tv_sec = (long) ((ularge.QuadPart - epoch) / FILETIME_UNITS_PER_SEC);
115 	tp->tv_usec = (long) (((ularge.QuadPart - epoch) % FILETIME_UNITS_PER_SEC)
116 						  / FILETIME_UNITS_PER_USEC);
117 
118 	return 0;
119 }
120