-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertDateTime.cpp
50 lines (42 loc) · 1.48 KB
/
ConvertDateTime.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
#include <windows.h>
#include <chrono>
#include <iomanip>
#include <sstream>
int FindTimeDiff(const std::wstring &dateStr, const std::wstring &timeStr, UINT64 &timediff)
{
SYSTEMTIME stLocal, stUTC, stNow;
FILETIME ftUTC, ftNow;
ULARGE_INTEGER ui, uiNow;
// Combine the date and time into one string for easier parsing
std::wstring dateTimeStr = dateStr + L" " + timeStr;
std::wstringstream wss(dateTimeStr);
tm timeStruct = {};
// Parse the date and time string into the tm structure
wss >> std::get_time(&timeStruct, L"%Y-%m-%d %H:%M:%S");
if (wss.fail())
{
return 1; // Failed to parse the date and time
}
// Convert parsed time to SYSTEMTIME
stLocal.wYear = timeStruct.tm_year + 1900;
stLocal.wMonth = timeStruct.tm_mon + 1;
stLocal.wDay = timeStruct.tm_mday;
stLocal.wHour = timeStruct.tm_hour;
stLocal.wMinute = timeStruct.tm_min;
stLocal.wSecond = timeStruct.tm_sec;
stLocal.wMilliseconds = 0;
// Convert to UTC time and FILETIME
if (!TzSpecificLocalTimeToSystemTime(NULL, &stLocal, &stUTC))
return 1;
SystemTimeToFileTime(&stUTC, &ftUTC);
ui.LowPart = ftUTC.dwLowDateTime;
ui.HighPart = ftUTC.dwHighDateTime;
// Get the current system time and convert to FILETIME
GetSystemTime(&stNow);
SystemTimeToFileTime(&stNow, &ftNow);
uiNow.LowPart = ftNow.dwLowDateTime;
uiNow.HighPart = ftNow.dwHighDateTime;
// Calculate the time difference in milliseconds
timediff = (uiNow.QuadPart - ui.QuadPart) / 10000; // Convert from 100-nanoseconds to milliseconds
return 0;
}