Skip to content

Commit 32213cc

Browse files
authored
tzset: Fix timezone extraction not working for all languages (#22250)
With certain language I've noticed that the timezone extraction algorithm doesn't work. or example consider the following: Test file: ```JavaScript const date = new Date(); console.log(date.toLocaleTimeString(undefined, {hour12: false, timeZoneName: 'short'})) ``` Run test file with specific local such as `th-TH` or `ar-AE` ```bash LC_ALL="th-TH" TZ="Asia/Bangkok" node test.js # prints "15 นาฬิกา 53 นาที 54 วินาที GMT+7" ``` And so the current logic for extracting would fail and return "นาฬิกา" (second item when splitting by space) In this PR, a new approach is proposed for extracting the timezone offset and tests are updated with new test-case See #21596
1 parent 9a489b0 commit 32213cc

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/library.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,20 @@ addToLibrary({
657657

658658
{{{ makeSetValue('daylight', '0', 'Number(winterOffset != summerOffset)', 'i32') }}};
659659

660-
var extractZone = (date) => date.toLocaleTimeString(undefined, {hour12:false, timeZoneName:'short'}).split(' ')[1];
661-
var winterName = extractZone(winter);
662-
var summerName = extractZone(summer);
660+
var extractZone = (timezoneOffset) => {
661+
// Why inverse sign?
662+
// Read here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
663+
var sign = timezoneOffset >= 0 ? "-" : "+";
664+
665+
var absOffset = Math.abs(timezoneOffset)
666+
var hours = String(Math.floor(absOffset / 60)).padStart(2, "0");
667+
var minutes = String(absOffset % 60).padStart(2, "0");
668+
669+
return `UTC${sign}${hours}${minutes}`;
670+
}
671+
672+
var winterName = extractZone(winterOffset);
673+
var summerName = extractZone(summerOffset);
663674
#if ASSERTIONS
664675
assert(winterName);
665676
assert(summerName);

test/test_other.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from common import create_file, parameterized, NON_ZERO, node_pthreads, TEST_ROOT, test_file
3737
from common import compiler_for, EMBUILDER, requires_v8, requires_node, requires_wasm64, requires_node_canary
3838
from common import requires_wasm_exnref, crossplatform, with_all_eh_sjlj, with_all_sjlj
39-
from common import also_with_standalone_wasm, also_with_env_modify, also_with_wasm2js
39+
from common import also_with_standalone_wasm, also_with_wasm2js
4040
from common import also_with_minimal_runtime, also_with_wasm_bigint, also_with_wasm64, flaky
4141
from common import EMTEST_BUILD_VERBOSE, PYTHON, WEBIDL_BINDER
4242
from common import requires_network, parameterize
@@ -5942,11 +5942,20 @@ def test_force_stdlibs(self):
59425942
self.do_runf('hello_world.c', emcc_args=['-sWASM_BIGINT'])
59435943

59445944
@crossplatform
5945-
@also_with_env_modify({'gb_locale': {'LC_ALL': 'en_GB'}, 'long_tz': {'TZ': 'Asia/Kathmandu'}})
59465945
def test_strftime_zZ(self):
5947-
if os.environ.get('LC_ALL') == 'en_GB' and MACOS:
5946+
if MACOS:
59485947
self.skipTest('setting LC_ALL is not compatible with macOS python')
5949-
self.do_runf('other/test_strftime_zZ.c', 'ok!')
5948+
5949+
tz_lang_infos = [
5950+
{'env': {'LC_ALL': 'en_GB', 'TZ': 'Europe/London'}, 'expected_utc': 'UTC+0100'},
5951+
{'env': {'LC_ALL': 'th_TH', 'TZ': 'Asia/Bangkok'}, 'expected_utc': 'UTC+0700'},
5952+
{'env': {'LC_ALL': 'ar-AE', 'TZ': 'Asia/Dubai'}, 'expected_utc': 'UTC+0400'},
5953+
{'env': {'LC_ALL': 'en-US', 'TZ': 'America/Los_Angeles'}, 'expected_utc': 'UTC-0700'}
5954+
]
5955+
5956+
for tz_lang_info in tz_lang_infos:
5957+
with env_modify(tz_lang_info['env']):
5958+
self.do_runf('other/test_strftime_zZ.c', 'The current timezone is: %s' % (tz_lang_info['expected_utc']))
59505959

59515960
def test_strptime_symmetry(self):
59525961
self.do_other_test('test_strptime_symmetry.c')

0 commit comments

Comments
 (0)