From 8ace5a583298922da167b410585ab28e0b88dab3 Mon Sep 17 00:00:00 2001 From: Aaron Ross Date: Thu, 17 Mar 2022 17:18:46 +0000 Subject: [PATCH] convert inner datetime to local in `to_date_string` (#1953) This Pull Request fixes/closes #1942. `Date.prototype.toDateString` should return a value representing the local date. The Rust `Date` inner value represents UTC time, so it should be adjusted to local time before formatting (see equivalent conversions performed by `to_string` and `to_time_string`). To verify this is working as intended, run the test suite with your OS timezone set to `GMT+0`, then again with `GMT+10`. The test `date_proto_to_date_string` should pass for each. For me (Ubuntu via WSL), this can be done with `sudo dpkg-reconfigure tzdata`. This PR also fixes a couple other test cases that used the wrong month value (as noted at the top of the file, JS months are 0-based while `chrono` months are 1-based). --- boa_engine/src/builtins/date/mod.rs | 7 ++++++- boa_engine/src/builtins/date/tests.rs | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/boa_engine/src/builtins/date/mod.rs b/boa_engine/src/builtins/date/mod.rs index 9460229aa0f..d6d6d43dd6f 100644 --- a/boa_engine/src/builtins/date/mod.rs +++ b/boa_engine/src/builtins/date/mod.rs @@ -1634,7 +1634,12 @@ impl Date { // 4. Let t be LocalTime(tv). // 5. Return DateString(t). if let Some(t) = tv.0 { - Ok(t.format("%a %b %d %Y").to_string().into()) + Ok(Local::now() + .timezone() + .from_utc_datetime(&t) + .format("%a %b %d %Y") + .to_string() + .into()) } else { Ok(JsString::from("Invalid Date").into()) } diff --git a/boa_engine/src/builtins/date/tests.rs b/boa_engine/src/builtins/date/tests.rs index 7a2eebdacd6..057fd576fd1 100644 --- a/boa_engine/src/builtins/date/tests.rs +++ b/boa_engine/src/builtins/date/tests.rs @@ -1199,7 +1199,7 @@ fn date_proto_to_string() { Some(JsValue::new( Local .from_local_datetime(&NaiveDateTime::new( - NaiveDate::from_ymd(2020, 6, 8), + NaiveDate::from_ymd(2020, 7, 8), NaiveTime::from_hms_milli(9, 16, 15, 779) )) .earliest() @@ -1225,7 +1225,7 @@ fn date_proto_to_time_string() { Some(JsValue::new( Local .from_local_datetime(&NaiveDateTime::new( - NaiveDate::from_ymd(2020, 6, 8), + NaiveDate::from_ymd(2020, 7, 8), NaiveTime::from_hms_milli(9, 16, 15, 779) )) .earliest()