0

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;
}
Abdul ahad
  • 1,383
  • 7
  • 18
kishoredbn
  • 2,007
  • 4
  • 28
  • 47
  • 1
    [`ucal_setDefaultTimeZone`](https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/ucal_8h.html#a98e47f9fabcac37b29276009aff544b1) is an ICU API that changes the default time zone for other functions used by ICU within the same process. It doesn't change the system's time zone. Are you trying to write an app that changes the system's time zone? – Matt Johnson-Pint Jan 14 '22 at 19:27

1 Answers1

0

I don't find the Microsoft document about ucal_setDefaultTimeZone but I suppose ucal_setDefaultTimeZone is suitable for per process. The linked blog is concentrated on converting between IANA timezone and Microsoft timezone. Finally, to set Windows system timezone just as settings panel, SetDynamicTimeZoneInformation is suitable. There is a thread you can refer to . Regard.

YangXiaoPo-MSFT
  • 1,589
  • 1
  • 4
  • 22
  • Thanks for the respond. Thanks for clarifying the doubt on documentation. Can I say this API is undocumented? Problem with ```SetDynamicTimeZoneInformation``` is that it comes with a complicated struct which is not an ideal for cross-platform development. I am just looking for an API which can work with IANA TZ code across Linux and MacOS. ```ucal_setDefaultTimeZone``` was closest I could find. – kishoredbn Jan 14 '22 at 12:59