Skip to content

Commit

Permalink
Fix: Unwanted comments, get_*(), and unimplemented functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lameferret committed Jul 28, 2022
1 parent f4c6508 commit c53b2fe
Showing 1 changed file with 21 additions and 65 deletions.
86 changes: 21 additions & 65 deletions boa_engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -338,6 +336,7 @@ impl Date {
.into())
}
}

/// Utility: Returns `Date` object with current datetime
pub(crate) fn date_create(prototype: Option<JsObject>, context: &mut Context) -> JsObject {
let prototype =
Expand Down Expand Up @@ -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<JsValue> {
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())
Expand All @@ -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<JsValue> {
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())
Expand All @@ -597,13 +586,8 @@ impl Date {
context: &mut Context,
) -> JsResult<JsValue> {
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())
Expand All @@ -626,13 +610,8 @@ impl Date {
context: &mut Context,
) -> JsResult<JsValue> {
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())
Expand All @@ -655,13 +634,7 @@ impl Date {
context: &mut Context,
) -> JsResult<JsValue> {
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())
Expand All @@ -684,13 +657,7 @@ impl Date {
context: &mut Context,
) -> JsResult<JsValue> {
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())
Expand All @@ -714,13 +681,7 @@ impl Date {
context: &mut Context,
) -> JsResult<JsValue> {
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())
Expand All @@ -743,13 +704,7 @@ impl Date {
context: &mut Context,
) -> JsResult<JsValue> {
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())
Expand All @@ -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<JsValue> {
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 {
Expand Down Expand Up @@ -1833,7 +1788,7 @@ impl Date {
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
unimplemented!()
Err(JsValue::new("Function Unimplemented]"))
}

/// `Date.prototype.toGMTString()`
Expand Down Expand Up @@ -1957,7 +1912,7 @@ impl Date {
_: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
unimplemented!()
Err(JsValue::new("Function Unimplemented]"))
}

/// `Date.prototype.toTimeString()`
Expand Down Expand Up @@ -2012,7 +1967,7 @@ impl Date {
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
unimplemented!()
Err(JsValue::new("Function Unimplemented]"))
}

/// `Date.prototype.toUTCString()`
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c53b2fe

Please sign in to comment.