|
| 1 | +/* |
| 2 | + * Copied from vcpkg: |
| 3 | + * https://github.com/microsoft/vcpkg/blob/master/ports/gettimeofday/gettimeofday.c |
| 4 | + * |
| 5 | + * Originally copied from PostgreSQL source: |
| 6 | + * http://doxygen.postgresql.org/gettimeofday_8c_source.html |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +/* |
| 11 | + * gettimeofday.c |
| 12 | + * Win32 gettimeofday() replacement |
| 13 | + * |
| 14 | + * src/port/gettimeofday.c |
| 15 | + * |
| 16 | + * Copyright (c) 2003 SRA, Inc. |
| 17 | + * Copyright (c) 2003 SKC, Inc. |
| 18 | + * |
| 19 | + * Permission to use, copy, modify, and distribute this software and |
| 20 | + * its documentation for any purpose, without fee, and without a |
| 21 | + * written agreement is hereby granted, provided that the above |
| 22 | + * copyright notice and this paragraph and the following two |
| 23 | + * paragraphs appear in all copies. |
| 24 | + * |
| 25 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, |
| 26 | + * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING |
| 27 | + * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS |
| 28 | + * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED |
| 29 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * |
| 31 | + * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT |
| 32 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 33 | + * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS |
| 34 | + * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, |
| 35 | + * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 36 | + */ |
| 37 | + |
| 38 | +#ifdef _MSC_VER |
| 39 | + |
| 40 | +#include <winsock2.h> |
| 41 | + |
| 42 | + |
| 43 | +/* FILETIME of Jan 1 1970 00:00:00. */ |
| 44 | +static const unsigned __int64 epoch = 116444736000000000Ui64; |
| 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 purpose. See |
| 50 | + * elapsed_time(). |
| 51 | + */ |
| 52 | +int |
| 53 | +gettimeofday(struct timeval * tp, struct timezone * tzp) |
| 54 | +{ |
| 55 | + FILETIME file_time; |
| 56 | + SYSTEMTIME system_time; |
| 57 | + ULARGE_INTEGER ularge; |
| 58 | + |
| 59 | + GetSystemTime(&system_time); |
| 60 | + SystemTimeToFileTime(&system_time, &file_time); |
| 61 | + ularge.LowPart = file_time.dwLowDateTime; |
| 62 | + ularge.HighPart = file_time.dwHighDateTime; |
| 63 | + |
| 64 | + tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L); |
| 65 | + tp->tv_usec = (long) (system_time.wMilliseconds * 1000); |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +#endif /* _MSC_VER */ |
0 commit comments