I am working on a cross platform timezone exchange app where I am trying to exchange the timezone in a platform-independent way following IANA's standards.
I am following this blog which briefly introduces to Windows ICU library, which seems like does more than interchanging different timezone formats, the one which I am interested in is ucal_setDefaultTimeZone()
.
Can someone help me understand how to make this work? Here is a sample code that I copied and modified from the blog, to change the timezone on local machine. But unfortunately I don't see my machine's timezone is changing. Though it is logging out that it has changed the timezone.
Here is my code:
#include <windows.h>
#include <stdio.h>
#define UCHAR_TYPE wchar_t
#include <icu.h>
int wmain(int argc, wchar_t** argv)
{
wchar_t buffer[128];
UErrorCode status = U_ZERO_ERROR;
if (argc == 2) {
auto result = ucal_getWindowsTimeZoneID(
argv[1], -1, buffer, ARRAYSIZE(buffer), &status);
if (U_SUCCESS(status)) {
printf("result = %d, IANA %ls -> Windows %ls\n",
result, argv[1], buffer);
result = ucal_getDefaultTimeZone(buffer, ARRAYSIZE(buffer), &status);
if (U_SUCCESS(status)) {
printf("Current timezone of the system is: %ls\n", buffer);
}
printf("Now setting System time\n");
ucal_setDefaultTimeZone(argv[1], &status);
if (U_SUCCESS(status)) {
printf("successfully set TZ\n");
result = ucal_getDefaultTimeZone(buffer, ARRAYSIZE(buffer), &status);
if (U_SUCCESS(status)) {
printf("After setting timezone of the system is: %ls\n", buffer);
}
}
else {
printf("successfully NOT set TZ\n");
}
}
}
else if (argc == 3) {
char region[64];
if (WideCharToMultiByte(CP_UTF8, 0, argv[2], -1,
region, 64, 0, NULL)) {
auto result = ucal_getTimeZoneIDForWindowsID(
argv[1], -1, region, buffer, ARRAYSIZE(buffer), &status);
if (U_SUCCESS(status)) {
printf("result = %d, Windows %ls:%s -> IANA %ls\n",
result, argv[1], region, buffer);
}
}
}
return 0;
}