|
1
|
1 #include "StdAfx.h"
|
|
|
2 #include "win-systemtime.h"
|
|
|
3 #include <ctime>
|
|
|
4
|
|
|
5 #ifdef _WIN32
|
|
|
6 static void theSelfTest(); // has to be outside namespace
|
|
|
7 #endif
|
|
|
8
|
|
|
9 namespace winTimeWrapper {
|
|
|
10 uint64_t FileTimeToInt(FILETIME ft) {
|
|
|
11 return ((uint64_t)ft.dwLowDateTime) | ((uint64_t)ft.dwHighDateTime << 32);
|
|
|
12 }
|
|
|
13 FILETIME FileTimeFromInt(uint64_t i) {
|
|
|
14 return { (uint32_t)(i & 0xFFFFFFFF), (uint32_t)(i >> 32) };
|
|
|
15 }
|
|
|
16 bool SystemTimeToFileTime(const SYSTEMTIME* st, FILETIME* ft) {
|
|
|
17 struct tm tmTemp = {};
|
|
|
18 if (st->wYear < 1900) return false;
|
|
|
19 tmTemp.tm_year = st->wYear - 1900;
|
|
|
20 if (st->wMonth < 1 || st->wMonth > 12) return false;
|
|
|
21 tmTemp.tm_mon = st->wMonth - 1;
|
|
|
22 tmTemp.tm_mday = st->wDay;
|
|
|
23 tmTemp.tm_hour = st->wHour;
|
|
|
24 tmTemp.tm_min = st->wMinute;
|
|
|
25 tmTemp.tm_sec = st->wSecond;
|
|
|
26 tmTemp.tm_isdst = -1;
|
|
|
27
|
|
|
28 time_t timeTemp = mktime(&tmTemp);
|
|
|
29 uint64_t wintime = pfc::fileTimeUtoW(timeTemp);
|
|
|
30 wintime += (filetimestamp_1second_increment / 1000) * st->wMilliseconds;
|
|
|
31 auto ftTemp = FileTimeFromInt( wintime );
|
|
|
32 return LocalFileTimeToFileTime(&ftTemp, ft);
|
|
|
33 }
|
|
|
34 bool LocalFileTimeToFileTime(const FILETIME* lpLocalFileTime, FILETIME* lpFileTime) {
|
|
|
35 uint64_t t64 = FileTimeToInt(*lpLocalFileTime);
|
|
|
36
|
|
|
37 time_t rawtime = pfc::fileTimeWtoU(t64);
|
|
|
38
|
|
|
39 auto gm = * gmtime(&rawtime);
|
|
|
40 auto loc = * localtime(&rawtime);
|
|
|
41
|
|
|
42 int64_t diff = (int64_t)mktime(&loc) - (int64_t)mktime(&gm);
|
|
|
43
|
|
|
44 *lpFileTime = FileTimeFromInt(t64 + diff * filetimestamp_1second_increment);
|
|
|
45 return true;
|
|
|
46 }
|
|
|
47
|
|
|
48 #ifdef _WIN32
|
|
|
49 void selfTest() { theSelfTest(); }
|
|
|
50 #endif
|
|
|
51 }
|
|
|
52
|
|
|
53
|
|
|
54 #ifdef _WIN32
|
|
|
55 static void theSelfTest() {
|
|
|
56 uint64_t ft1, ft2;
|
|
|
57 auto ft_1s = filetimestamp_1second_increment;
|
|
|
58 {
|
|
|
59 SYSTEMTIME st = {};
|
|
|
60 st.wDay = 21; st.wMonth = 2; st.wYear = 2022;
|
|
|
61 st.wHour = 15; st.wMinute = 15;
|
|
|
62 FILETIME ft = {};
|
|
|
63 SystemTimeToFileTime(&st, &ft);
|
|
|
64 ft1 = *(uint64_t*)(&ft);
|
|
|
65 }
|
|
|
66 {
|
|
|
67 winTimeWrapper::SYSTEMTIME st = {};
|
|
|
68 st.wDay = 21; st.wMonth = 2; st.wYear = 2022;
|
|
|
69 st.wHour = 15; st.wMinute = 15;
|
|
|
70 winTimeWrapper::FILETIME ft = {};
|
|
|
71 winTimeWrapper::SystemTimeToFileTime(&st, &ft);
|
|
|
72 ft2 = winTimeWrapper::FileTimeToInt(ft);
|
|
|
73 }
|
|
|
74
|
|
|
75 if (ft1 < ft2) {
|
|
|
76 pfc::outputDebugLine(pfc::format("ahead by ", (ft2 - ft1) / ft_1s));
|
|
|
77 } else if (ft1 > ft2) {
|
|
|
78 pfc::outputDebugLine(pfc::format("behind by ", (ft1 - ft2) / ft_1s));
|
|
|
79 } else {
|
|
|
80 pfc::outputDebugLine("ok");
|
|
|
81 }
|
|
|
82
|
|
|
83 pfc::nop();
|
|
|
84 }
|
|
|
85 #endif
|