Debian Codegraph @

debian/rust-vendor/jiff/src/tz/system/wasm_js.rs

raw
1 use alloc::string::String;
2
3 use crate::tz::{TimeZone, TimeZoneDatabase};
4
5 pub(super) fn get(db: &TimeZoneDatabase) -> Option<TimeZone> {
6 let fmt = js_sys::Intl::DateTimeFormat::new(
7 &js_sys::Array::new(),
8 &js_sys::Object::new(),
9 );
10 let options = fmt.resolved_options();
11 // Documented to be an IANA tz ID:
12 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions#timezone
13 let key = wasm_bindgen::JsValue::from("timeZone");
14 let val = match js_sys::Reflect::get(&options, &key) {
15 Ok(val) => val,
16 Err(_err) => {
17 trace!(
18 "failed to get `timeZone` key on \
19 Intl.DateTimeFormat options: {_err:?}"
20 );
21 return None;
22 }
23 };
24 trace!("got `timeZone` value from Intl.DateTimeFormat options: {val:?}");
25 let name = match String::try_from(val) {
26 Ok(string) => string,
27 Err(_err) => {
28 trace!(
29 "failed to convert `timeZone` on \
30 Intl.DateTimeFormat to string"
31 );
32 return None;
33 }
34 };
35 let tz = match db.get(&name) {
36 Ok(tz) => tz,
37 Err(_err) => {
38 trace!(
39 "got {name:?} as time zone name, \
40 but failed to find time zone with that name in \
41 zoneinfo database {db:?}: {_err}",
42 );
43 return None;
44 }
45 };
46 Some(tz)
47 }
48
49 pub(super) fn read(_db: &TimeZoneDatabase, path: &str) -> Option<TimeZone> {
50 match super::read_unnamed_tzif_file(path) {
51 Ok(tz) => Some(tz),
52 Err(_err) => {
53 trace!("failed to read {path} as unnamed time zone: {_err}");
54 None
55 }
56 }
57 }