Reputation: 55
System is in UTC, Is there a way to get a local time of a specific timezone?
Most windows api's return values based on system time.
If there is any api where we could pass the timezone indication and get the localtime?
Also, read different api's windows provides, and thought this one : "EnumDynamicTimeZoneInformation"
could be of use to me, but i cant get this to run, i see a undefined identifier error. error C3861: 'EnumDynamicTimeZoneInformation': identifier not found
Included windows.h as mentioned in this link: https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-enumdynamictimezoneinformation?redirectedfrom=MSDN
On getting info from the above api, i can try passing the same to :
SystemTimeToTzSpecificLocalTime
and hoping that would do it. Can anyone suggest what i'm missing.
Trying this on VS2010
File: time.cpp
#ifndef WINVER
#define WINVER 0x0602
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0602
#endif
#include <windows.h>
#include<time.h>
#include<iostream>
int main(){
DYNAMIC_TIME_ZONE_INFORMATION d_tz;
memset(&d_tz,0,sizeof(d_tz));
DWORD res=0;
res = EnumDynamicTimeZoneInformation(1, &d_tz);//fetch specific timezone info
/*Use this next*/
//SystemTimeToTzSpecificLocalTime();
return 0;
}
Upvotes: 1
Views: 1216
Reputation: 7170
Comprehensive comments, the following example works for me:
#define WINVER 0x0602
#define _WIN32_WINNT 0x0602
#include <windows.h>
#include<time.h>
#include<iostream>
int main() {
DYNAMIC_TIME_ZONE_INFORMATION d_tz;
memset(&d_tz, 0, sizeof(d_tz));
DWORD res = 0;
res = EnumDynamicTimeZoneInformation(1, &d_tz);//fetch specific timezone info
/*Use this next*/
SYSTEMTIME st = { 0 };
SYSTEMTIME lt = { 0 };
GetSystemTime(&st);
SystemTimeToTzSpecificLocalTimeEx(&d_tz,&st, <);
WCHAR time[250] = { 0 };
GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, <, L"HH':'mm':'ss tt", time, 250);
std::wcout << L"Timezone: " << d_tz.TimeZoneKeyName << std::endl;
std::wcout << lt.wYear << L"/" << lt.wMonth << L"/" << lt.wDay << L" " << time << std::endl;
return 0;
}
Use EnumDynamicTimeZoneInformation
get a DYNAMIC_TIME_ZONE_INFORMATION
instance and then pass it to SystemTimeToTzSpecificLocalTimeEx
with your SYSTEMTIME
. Finally get the specific local time.
Result:
Timezone: Alaskan Standard Time
2021/2/2 17:25:39 PM
Upvotes: 1