3 * Win32 gettimeofday() replacement
5 * src/port/gettimeofday.c
7 * Copyright (c) 2003 SRA, Inc.
8 * Copyright (c) 2003 SKC, Inc.
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.
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.
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.
33 /* FILETIME of Jan 1 1970 00:00:00, the PostgreSQL epoch */
34 static const unsigned __int64 epoch
= UINT64CONST(116444736000000000);
37 * FILETIME represents the number of 100-nanosecond intervals since
38 * January 1, 1601 (UTC).
40 #define FILETIME_UNITS_PER_SEC 10000000L
41 #define FILETIME_UNITS_PER_USEC 10
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.
48 typedef VOID(WINAPI
* PgGetSystemTimeFn
) (LPFILETIME
);
50 /* One-time initializer function, must match that signature. */
51 static void WINAPI
init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime
);
53 /* Storage for the function we pick at runtime */
54 static PgGetSystemTimeFn pg_get_system_time
= &init_gettimeofday
;
57 * One time initializer. Determine whether GetSystemTimePreciseAsFileTime
58 * is available and if so, plan to use it; if not, fall back to
59 * GetSystemTimeAsFileTime.
62 init_gettimeofday(LPFILETIME lpSystemTimeAsFileTime
)
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.
69 * We'll just look up the address of GetSystemTimePreciseAsFileTime if
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...
77 pg_get_system_time
= (PgGetSystemTimeFn
) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
78 "GetSystemTimePreciseAsFileTime");
79 if (pg_get_system_time
== NULL
)
82 * The expected error from GetLastError() is ERROR_PROC_NOT_FOUND, if
83 * the function isn't present. No other error should occur.
85 * We can't report an error here because this might be running in
86 * frontend code; and even if we're in the backend, it's too early to
87 * elog(...) if we get some unexpected error. Also, it's not a
88 * serious problem, so just silently fall back to
89 * GetSystemTimeAsFileTime irrespective of why the failure occurred.
91 pg_get_system_time
= &GetSystemTimeAsFileTime
;
94 (*pg_get_system_time
) (lpSystemTimeAsFileTime
);
98 * timezone information is stored outside the kernel so tzp isn't used anymore.
100 * Note: this function is not for Win32 high precision timing purposes. See
104 gettimeofday(struct timeval
*tp
, struct timezone
*tzp
)
107 ULARGE_INTEGER ularge
;
109 (*pg_get_system_time
) (&file_time
);
110 ularge
.LowPart
= file_time
.dwLowDateTime
;
111 ularge
.HighPart
= file_time
.dwHighDateTime
;
113 tp
->tv_sec
= (long) ((ularge
.QuadPart
- epoch
) / FILETIME_UNITS_PER_SEC
);
114 tp
->tv_usec
= (long) (((ularge
.QuadPart
- epoch
) % FILETIME_UNITS_PER_SEC
)
115 / FILETIME_UNITS_PER_USEC
);