PostgreSQL Source Code  git master
win32gettimeofday.c
Go to the documentation of this file.
1 /*
2  * win32gettimeofday.c
3  * Win32 gettimeofday() replacement
4  *
5  * src/port/win32gettimeofday.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 <sysinfoapi.h>
32 
33 #include <sys/time.h>
34 
35 /* FILETIME of Jan 1 1970 00:00:00, the PostgreSQL epoch */
36 static const unsigned __int64 epoch = UINT64CONST(116444736000000000);
37 
38 /*
39  * FILETIME represents the number of 100-nanosecond intervals since
40  * January 1, 1601 (UTC).
41  */
42 #define FILETIME_UNITS_PER_SEC 10000000L
43 #define FILETIME_UNITS_PER_USEC 10
44 
45 
46 /*
47  * timezone information is stored outside the kernel so tzp isn't used anymore.
48  *
49  * Note: this function is not for Win32 high precision timing purposes. See
50  * elapsed_time().
51  */
52 int
53 gettimeofday(struct timeval *tp, void *tzp)
54 {
55  FILETIME file_time;
56  ULARGE_INTEGER ularge;
57 
58  /*
59  * POSIX declines to define what tzp points to, saying "If tzp is not a
60  * null pointer, the behavior is unspecified". Let's take this
61  * opportunity to verify that noplace in Postgres tries to use any
62  * unportable behavior.
63  */
64  Assert(tzp == NULL);
65 
66  GetSystemTimePreciseAsFileTime(&file_time);
67  ularge.LowPart = file_time.dwLowDateTime;
68  ularge.HighPart = file_time.dwHighDateTime;
69 
70  tp->tv_sec = (long) ((ularge.QuadPart - epoch) / FILETIME_UNITS_PER_SEC);
71  tp->tv_usec = (long) (((ularge.QuadPart - epoch) % FILETIME_UNITS_PER_SEC)
73 
74  return 0;
75 }
#define Assert(condition)
Definition: c.h:858
#define FILETIME_UNITS_PER_USEC
#define FILETIME_UNITS_PER_SEC
static const unsigned __int64 epoch
int gettimeofday(struct timeval *tp, void *tzp)