From bc12f95418d92c2f6d570eca7c2b97dc7fa28f5d Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Fri, 20 Aug 2021 11:55:32 +0200 Subject: [PATCH] Properly handle `NaN` in `new Date()` (#1477) --- boa/src/builtins/date/mod.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/boa/src/builtins/date/mod.rs b/boa/src/builtins/date/mod.rs index 89a9d92ee1..cd6487506d 100644 --- a/boa/src/builtins/date/mod.rs +++ b/boa/src/builtins/date/mod.rs @@ -450,9 +450,13 @@ impl Date { }, tv => { let tv = tv.to_number(context)?; - let secs = (tv / 1_000f64) as i64; - let nsecs = ((tv % 1_000f64) * 1_000_000f64) as u32; - NaiveDateTime::from_timestamp_opt(secs, nsecs) + if tv.is_nan() { + None + } else { + let secs = (tv / 1_000f64) as i64; + let nsecs = ((tv % 1_000f64) * 1_000_000f64) as u32; + NaiveDateTime::from_timestamp_opt(secs, nsecs) + } } }, }; @@ -846,6 +850,13 @@ impl Date { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear pub fn set_full_year(&mut self, year: Option, month: Option, day: Option) { if let Some(year) = year { + if self.0.is_none() { + self.0 = NaiveDateTime::from_timestamp_opt(0, 0) + .and_then(|local| ignore_ambiguity(Local.from_local_datetime(&local))) + .map(|local| local.naive_utc()) + .filter(|time| Self::time_clip(time.timestamp_millis() as f64).is_some()); + } + self.set_components(false, Some(year), month, day, None, None, None, None) } else { self.0 = None