Hi, @Markus Freitag Refer to the document: Local Time To convert a local time to a UTC-based time, use the TzSpecificLocalTimeToSystemTime function.
//convert loacl tm to UTC
SYSTEMTIME UtcTm;
tm temptm;
localtime_s(&temptm, ¤tTm);
SYSTEMTIME localTm = { 1900 + temptm.tm_year,
1 + temptm.tm_mon,
temptm.tm_wday,
temptm.tm_mday,
temptm.tm_hour,
temptm.tm_min,
temptm.tm_sec,
0 };
TzSpecificLocalTimeToSystemTime(&tmInfo, &localTm, &UtcTm);
struct tm tempUTC = { UtcTm.wSecond, UtcTm.wMinute, UtcTm.wHour, UtcTm.wDay, UtcTm.wMonth - 1, UtcTm.wYear - 1900, UtcTm.wDayOfWeek, 0, 0 };
time_t utctm_t = mktime(&tempUTC);
// Calculate the time difference in seconds
auto difference = std::difftime(utctm_t, std::mktime(&dbTime));
Console sample:
#include <iostream>
#include <chrono>
#include <ctime>
#include<string>
#include<Windows.h>
int main() {
// Database time
TIME_ZONE_INFORMATION tmInfo;
GetTimeZoneInformation(&tmInfo);
std::tm dbTime = {};
dbTime.tm_year = std::stoi("2024") - 1900;
dbTime.tm_mon = std::stoi("01") - 1;
dbTime.tm_mday = std::stoi("11");
dbTime.tm_hour = std::stoi("9");
dbTime.tm_min = std::stoi("03");
dbTime.tm_sec = std::stoi("00");
// Current time
auto currentTime = std::chrono::system_clock::now();
auto currentTm = std::chrono::system_clock::to_time_t(currentTime);
//convert loacl tm to UTC
SYSTEMTIME UtcTm;
tm temptm;
localtime_s(&temptm, ¤tTm);
SYSTEMTIME localTm = { 1900 + temptm.tm_year,
1 + temptm.tm_mon,
temptm.tm_wday,
temptm.tm_mday,
temptm.tm_hour,
temptm.tm_min,
temptm.tm_sec,
0 };
TzSpecificLocalTimeToSystemTime(&tmInfo, &localTm, &UtcTm);
struct tm tempUTC = { UtcTm.wSecond, UtcTm.wMinute, UtcTm.wHour, UtcTm.wDay, UtcTm.wMonth - 1, UtcTm.wYear - 1900, UtcTm.wDayOfWeek, 0, 0 };
time_t utctm_t = mktime(&tempUTC);
// Calculate the time difference in seconds
auto difference = std::difftime(utctm_t, std::mktime(&dbTime));
// Check if the difference is greater than 30 seconds
if (difference > 30.0) {
std::cout << "All fine. Time difference: " << difference << " seconds.\n";
}
else {
std::cout << "Error message. Time difference: " << difference << " seconds.\n";
}
return 0;
}
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.