FreeRDP
Loading...
Searching...
No Matches
TestTimeZoneBrowser.c
1#include <stdio.h>
2#include <string.h>
3
4#include <winpr/wtypes.h>
5#include <winpr/string.h>
6#include <winpr/timezone.h>
7
8#if defined(__EMSCRIPTEN__)
9/* in TestTimeZoneBrowser.js */
10extern void test_timezone_js_set(const char* tz);
11extern int test_timezone_js_is(const char* tz);
12
13/* node re-reads its time zone when process.env.TZ changes on its main thread */
14static BOOL set_browser_timezone(const char* tz)
15{
16 test_timezone_js_set(tz);
17 return test_timezone_js_is(tz) != 0;
18}
19
20static BOOL get_time_zone_key(char* key, size_t size)
21{
22 DYNAMIC_TIME_ZONE_INFORMATION tzif = WINPR_C_ARRAY_INIT;
23
24 (void)GetDynamicTimeZoneInformation(&tzif);
25 return ConvertWCharNToUtf8(tzif.TimeZoneKeyName, ARRAYSIZE(tzif.TimeZoneKeyName), key, size) >=
26 0;
27}
28
29static BOOL test_browser_time_zone(const char* iana, const char* expected)
30{
31 char key[128] = WINPR_C_ARRAY_INIT;
32
33 if (!set_browser_timezone(iana))
34 {
35 (void)printf("skipping %s, cannot change the time zone here\n", iana);
36 return TRUE;
37 }
38
39 if (!get_time_zone_key(key, sizeof(key)))
40 {
41 (void)fprintf(stderr, "GetDynamicTimeZoneInformation failed for %s\n", iana);
42 return FALSE;
43 }
44
45 if (strcmp(key, expected) != 0)
46 {
47 (void)fprintf(stderr, "%s: expected time zone key '%s', got '%s'\n", iana, expected, key);
48 return FALSE;
49 }
50 return TRUE;
51}
52#endif
53
54int TestTimeZoneBrowser(int argc, char* argv[])
55{
56 WINPR_UNUSED(argc);
57 WINPR_UNUSED(argv);
58
59#if defined(__EMSCRIPTEN__)
60 /* A browser always knows its time zone, so it must map to a Windows one */
61 char key[128] = WINPR_C_ARRAY_INIT;
62 if (!get_time_zone_key(key, sizeof(key)) || (strlen(key) == 0))
63 {
64 (void)fprintf(stderr, "no Windows time zone key for the browser time zone\n");
65 return -1;
66 }
67
68 if (!test_browser_time_zone("Europe/Lisbon", "GMT Standard Time"))
69 return -1;
70 if (!test_browser_time_zone("America/New_York", "Eastern Standard Time"))
71 return -1;
72 if (!test_browser_time_zone("UTC", "UTC"))
73 return -1;
74#endif
75
76 return 0;
77}