Skip to content

Commit 519b075

Browse files
Use GetSystemTimeAsFileTime directly in win32
PostgreSQL was calling GetSystemTime followed by SystemTimeToFileTime in the win32 port gettimeofday function. This is not necessary and limits the reported precision to the 1ms granularity that the SYSTEMTIME struct can represent. By using GetSystemTimeAsFileTime we avoid unnecessary conversions and capture timestamps at 100ns granularity, which is then rounded to 1µs granularity for storage in a PostgreSQL timestamp. On most Windows systems this change will actually have no significant effect on timestamp resolution as the system timer tick is typically between 1ms and 15ms depending on what timer resolution currently running applications have requested. You can check this with clockres.exe from sysinternals. Despite the platform limiation this change still permits capture of finer timestamps where the system is capable of producing them and it gets rid of an unnecessary syscall. The higher resolution GetSystemTimePreciseAsFileTime call available on Windows 8 and Windows Server 2012 has the same interface as GetSystemTimeAsFileTime, so switching to GetSystemTimeAsFileTime makes it easier to use the Precise variant later. Craig Ringer, reviewed by David Rowley
1 parent 611d46e commit 519b075

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/port/gettimeofday.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@
3131
#include <sys/time.h>
3232

3333

34-
/* FILETIME of Jan 1 1970 00:00:00. */
34+
/* FILETIME of Jan 1 1970 00:00:00, the PostgreSQL epoch */
3535
static const unsigned __int64 epoch = UINT64CONST(116444736000000000);
3636

37+
/*
38+
* FILETIME represents the number of 100-nanosecond intervals since
39+
* January 1, 1601 (UTC).
40+
*/
41+
#define FILETIME_UNITS_PER_SEC 10000000L
42+
#define FILETIME_UNITS_PER_USEC 10
43+
3744
/*
3845
* timezone information is stored outside the kernel so tzp isn't used anymore.
3946
*
@@ -44,16 +51,15 @@ int
4451
gettimeofday(struct timeval * tp, struct timezone * tzp)
4552
{
4653
FILETIME file_time;
47-
SYSTEMTIME system_time;
4854
ULARGE_INTEGER ularge;
4955

50-
GetSystemTime(&system_time);
51-
SystemTimeToFileTime(&system_time, &file_time);
56+
GetSystemTimeAsFileTime(&file_time);
5257
ularge.LowPart = file_time.dwLowDateTime;
5358
ularge.HighPart = file_time.dwHighDateTime;
5459

55-
tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
56-
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
60+
tp->tv_sec = (long) ((ularge.QuadPart - epoch) / FILETIME_UNITS_PER_SEC);
61+
tp->tv_usec = (long) (((ularge.QuadPart - epoch) % FILETIME_UNITS_PER_SEC)
62+
/ FILETIME_UNITS_PER_USEC);
5763

5864
return 0;
5965
}

0 commit comments

Comments
 (0)