From c53b2fef8b82d7ac2e0213527fb7240934c937a9 Mon Sep 17 00:00:00 2001 From: anuvratsingh Date: Fri, 29 Jul 2022 03:33:12 +0530 Subject: [PATCH] Fix: Unwanted comments, `get_*()`, and unimplemented functions --- boa_engine/src/builtins/date/mod.rs | 86 +++++++---------------------- 1 file changed, 21 insertions(+), 65 deletions(-) diff --git a/boa_engine/src/builtins/date/mod.rs b/boa_engine/src/builtins/date/mod.rs index 78faf69aa70..a2ba6df3b87 100644 --- a/boa_engine/src/builtins/date/mod.rs +++ b/boa_engine/src/builtins/date/mod.rs @@ -124,12 +124,10 @@ impl BuiltIn for Date { .method(Self::to_date_string, "toDateString", 0) .method(Self::to_gmt_string, "toGMTString", 0) .method(Self::to_iso_string, "toISOString", 0) - // Why toJSON length is 1, does `method()`'s length signifies number of - // inputs? If so shouldn't it be 0? .method(Self::to_json, "toJSON", 1) - .method(Self::to_locale_date_string, "toLocaleDateString", 0) - .method(Self::to_locale_string, "toLocaleString", 0) - .method(Self::to_locale_time_string, "toLocaleTimeString", 0) + .method(Self::to_locale_date_string, "toLocaleDateString", 2) + .method(Self::to_locale_string, "toLocaleString", 2) + .method(Self::to_locale_time_string, "toLocaleTimeString", 2) .method(Self::to_string, "toString", 0) .method(Self::to_time_string, "toTimeString", 0) .method(Self::to_utc_string, "toUTCString", 0) @@ -338,6 +336,7 @@ impl Date { .into()) } } + /// Utility: Returns `Date` object with current datetime pub(crate) fn date_create(prototype: Option, context: &mut Context) -> JsObject { let prototype = @@ -542,13 +541,8 @@ impl Date { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate pub fn get_date(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); + Ok(JsValue::new(local.day())) } else { Ok(JsValue::nan()) @@ -568,13 +562,8 @@ impl Date { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay pub fn get_day(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); + Ok(JsValue::new(local.weekday().num_days_from_sunday())) } else { Ok(JsValue::nan()) @@ -597,13 +586,8 @@ impl Date { context: &mut Context, ) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); + Ok(JsValue::new(local.year())) } else { Ok(JsValue::nan()) @@ -626,13 +610,8 @@ impl Date { context: &mut Context, ) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); + Ok(JsValue::new(local.hour())) } else { Ok(JsValue::nan()) @@ -655,13 +634,7 @@ impl Date { context: &mut Context, ) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); Ok(JsValue::new(local.timestamp_subsec_millis())) } else { Ok(JsValue::nan()) @@ -684,13 +657,7 @@ impl Date { context: &mut Context, ) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); Ok(JsValue::new(local.minute())) } else { Ok(JsValue::nan()) @@ -714,13 +681,7 @@ impl Date { context: &mut Context, ) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); Ok(JsValue::new(local.month0())) } else { Ok(JsValue::nan()) @@ -743,13 +704,7 @@ impl Date { context: &mut Context, ) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let date = Local::now() - .timezone() - .from_utc_datetime(&t) - .format("%Y-%m-%dT%H:%M:%S%.f%:z") - .to_string(); - let local = NaiveDateTime::parse_from_str(&date, "%Y-%m-%dT%H:%M:%S%.f%:z") - .expect("INFALLIBLE: Converting string built from `Local::now()` to NaiveDateTime"); + let local = Local::now().timezone().from_utc_datetime(&t); Ok(JsValue::new(local.second())) } else { Ok(JsValue::nan()) @@ -770,7 +725,7 @@ impl Date { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear pub fn get_year(this: &JsValue, _args: &[JsValue], context: &mut Context) -> JsResult { if let Some(t) = this_time_value(this, context)?.0 { - let local = Local::now().timezone().from_local_datetime(&t).unwrap(); + let local = Local::now().timezone().from_utc_datetime(&t); let year = JsValue::Integer(local.year()); year.sub(&JsValue::from(1900), context) } else { @@ -1833,7 +1788,7 @@ impl Date { _args: &[JsValue], _context: &mut Context, ) -> JsResult { - unimplemented!() + Err(JsValue::new("Function Unimplemented]")) } /// `Date.prototype.toGMTString()` @@ -1957,7 +1912,7 @@ impl Date { _: &[JsValue], _context: &mut Context, ) -> JsResult { - unimplemented!() + Err(JsValue::new("Function Unimplemented]")) } /// `Date.prototype.toTimeString()` @@ -2012,7 +1967,7 @@ impl Date { _args: &[JsValue], _context: &mut Context, ) -> JsResult { - unimplemented!() + Err(JsValue::new("Function Unimplemented]")) } /// `Date.prototype.toUTCString()` @@ -2153,6 +2108,7 @@ impl Date { .and_then(|f| Self::time_clip(f.timestamp_millis() as f64)) .map_or(Ok(JsValue::nan()), |time| Ok(JsValue::new(time))) } + /// Utility: Returns an `Object` representing `Date` from string in RFC3339 pub(crate) fn create_obj(value: &JsValue, context: &mut Context) -> JsObject { let prototype = context.intrinsics().constructors().date().prototype();