Skip to content

Commit

Permalink
convert inner datetime to local in to_date_string (#1953)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
superhawk610 authored and Razican committed Jun 8, 2022
1 parent 20f604b commit 8ace5a5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion boa_engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/date/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 8ace5a5

Please sign in to comment.