-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
TimeUtils.cpp
69 lines (61 loc) · 1.91 KB
/
TimeUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by Administrator on 2022.11.30.
//
#include "TimeUtils.h"
#include <windows.h>
u64 currentTimeMillis() {
FILETIME ft = {};
GetSystemTimeAsFileTime(&ft);
u64 ret = ft.dwHighDateTime;
ret <<= 32;
ret |= ft.dwLowDateTime;
ret /= 10000LLU;
return ret - 116444736000000000LLU;
}
u64 currentTimeSeconds() {
return currentTimeMillis() / 1000LLU;
}
void timeMillisToUTCCalendar(u64 timeMillis, int *year, int *month, int *day, int *hour, int *minute, int *second, int *millisecond) {
SYSTEMTIME st = {};
FILETIME ft = {};
u64 fileTime = (timeMillis + 116444736000000000LLU) * 10000LLU;
ft.dwHighDateTime = (DWORD) (fileTime >> 32);
ft.dwLowDateTime = (DWORD) fileTime;
FileTimeToSystemTime(&ft, &st);
if (year != nullptr) {
*year = st.wYear;
}
if (month != nullptr) {
*month = st.wMonth;
}
if (day != nullptr) {
*day = st.wDay;
}
if (hour != nullptr) {
*hour = st.wHour;
}
if (minute != nullptr) {
*minute = st.wMinute;
}
if (second != nullptr) {
*second = st.wSecond;
}
if (millisecond != nullptr) {
*millisecond = st.wMilliseconds;
}
}
static u64 gsTimeBiasSeconds = 0;
static bool gsTimeBiasSecondsInitialized = false;
s32 getLocalTimeBiasSeconds() {
if (!gsTimeBiasSecondsInitialized) {
TIME_ZONE_INFORMATION tzi = {};
GetTimeZoneInformation(&tzi);
gsTimeBiasSeconds = -tzi.Bias * 60LU;
gsTimeBiasSecondsInitialized = true;
}
return gsTimeBiasSeconds;
}
void timeMillisToLocalCalendar(u64 timeMillis, int *year, int *month, int *day, int *hour, int *minute, int *second, int *millisecond) {
u64 timeWithBias = timeMillis + getLocalTimeBiasSeconds() * 1000;
timeMillisToUTCCalendar(timeWithBias, year, month, day, hour, minute, second, millisecond);
}