Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 127 additions & 22 deletions core/engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

use crate::{
Context, JsArgs, JsData, JsError, JsResult, JsString,
Context, JsArgs, JsData, JsResult, JsString,
builtins::{
BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
date::utils::{
Expand Down Expand Up @@ -1616,16 +1616,51 @@ impl Date {
/// More information:
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring
/// [spec]: https://tc39.es/ecma402/#sup-date.prototype.tolocaledatestring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
#[allow(
unused_variables,
reason = "`args` and `context` are used when the `intl` feature is enabled"
)]
pub(crate) fn to_locale_date_string(
_this: &JsValue,
_args: &[JsValue],
_context: &mut Context,
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
Err(JsError::from_opaque(JsValue::new(js_string!(
"Function Unimplemented"
))))
#[cfg(feature = "intl")]
{
use crate::builtins::intl::date_time_format::{
FormatDefaults, FormatType, format_date_time_locale,
};
// 1. Let dateObject be the this value.
// 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
// 3. Let x be dateObject.[[DateValue]].
let t = this
.as_object()
.and_then(|obj| obj.downcast_ref::<Date>().as_deref().copied())
.ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?
.0;
// 4. If x is NaN, return "Invalid Date".
if t.is_nan() {
return Ok(JsValue::new(js_string!("Invalid Date")));
}
// 5. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, date, date).
// 6. Return ! FormatDateTime(dateFormat, x).
let locales = args.get_or_undefined(0);
let options = args.get_or_undefined(1);
format_date_time_locale(
locales,
options,
FormatType::Date,
FormatDefaults::Date,
t,
context,
)
}
#[cfg(not(feature = "intl"))]
{
Self::to_string(this, &[], context)
}
}

/// [`Date.prototype.toLocaleString()`][spec].
Expand All @@ -1635,16 +1670,51 @@ impl Date {
/// More information:
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.tolocalestring
/// [spec]: https://tc39.es/ecma402/#sup-date.prototype.tolocalestring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
#[allow(
unused_variables,
reason = "`args` and `context` are used when the `intl` feature is enabled"
)]
pub(crate) fn to_locale_string(
_this: &JsValue,
_: &[JsValue],
_context: &mut Context,
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
Err(JsError::from_opaque(JsValue::new(js_string!(
"Function Unimplemented]"
))))
#[cfg(feature = "intl")]
{
use crate::builtins::intl::date_time_format::{
FormatDefaults, FormatType, format_date_time_locale,
};
// 1. Let dateObject be the this value.
// 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
// 3. Let x be dateObject.[[DateValue]].
let t = this
.as_object()
.and_then(|obj| obj.downcast_ref::<Date>().as_deref().copied())
.ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?
.0;
// 4. If x is NaN, return "Invalid Date".
if t.is_nan() {
return Ok(JsValue::new(js_string!("Invalid Date")));
}
// 5. Let dateFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, any, all).
// 6. Return ! FormatDateTime(dateFormat, x).
let locales = args.get_or_undefined(0);
let options = args.get_or_undefined(1);
format_date_time_locale(
locales,
options,
FormatType::Any,
FormatDefaults::All,
t,
context,
)
}
#[cfg(not(feature = "intl"))]
{
Self::to_string(this, &[], context)
}
}

/// [`Date.prototype.toLocaleTimeString()`][spec].
Expand All @@ -1655,16 +1725,51 @@ impl Date {
/// More information:
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-date.prototype.tolocaletimestring
/// [spec]: https://tc39.es/ecma402/#sup-date.prototype.tolocaletimestring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
#[allow(
unused_variables,
reason = "`args` and `context` are used when the `intl` feature is enabled"
)]
pub(crate) fn to_locale_time_string(
_this: &JsValue,
_args: &[JsValue],
_context: &mut Context,
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
Err(JsError::from_opaque(JsValue::new(js_string!(
"Function Unimplemented]"
))))
#[cfg(feature = "intl")]
{
use crate::builtins::intl::date_time_format::{
FormatDefaults, FormatType, format_date_time_locale,
};
// 1. Let dateObject be the this value.
// 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
// 3. Let x be dateObject.[[DateValue]].
let t = this
.as_object()
.and_then(|obj| obj.downcast_ref::<Date>().as_deref().copied())
.ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?
.0;
// 4. If x is NaN, return "Invalid Date".
if t.is_nan() {
return Ok(JsValue::new(js_string!("Invalid Date")));
}
// 5. Let timeFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, time, time).
// 6. Return ! FormatDateTime(timeFormat, x).
let locales = args.get_or_undefined(0);
let options = args.get_or_undefined(1);
format_date_time_locale(
locales,
options,
FormatType::Time,
FormatDefaults::Time,
t,
context,
)
}
#[cfg(not(feature = "intl"))]
{
Self::to_string(this, &[], context)
}
}

/// [`Date.prototype.toString()`][spec].
Expand Down
49 changes: 49 additions & 0 deletions core/engine/src/builtins/date/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,3 +943,52 @@ fn date_parse_hour24_validation() {
TestAction::assert("isNaN(Date.parse('2024-01-01T24:00:00.001Z'))"),
]);
}

#[test]
#[cfg(feature = "intl")]
fn date_proto_to_locale_string_intl() {
run_test_actions([
// Invalid receiver: spec requires TypeError
TestAction::assert_native_error(
"Date.prototype.toLocaleString.call({})",
JsNativeErrorKind::Type,
"'this' is not a Date",
),
TestAction::assert_native_error(
"Date.prototype.toLocaleDateString.call({})",
JsNativeErrorKind::Type,
"'this' is not a Date",
),
TestAction::assert_native_error(
"Date.prototype.toLocaleTimeString.call({})",
JsNativeErrorKind::Type,
"'this' is not a Date",
),
TestAction::assert_eq("new Date(NaN).toLocaleString()", js_str!("Invalid Date")),
TestAction::assert("typeof new Date(2020, 6, 8).toLocaleString() === 'string'"),
TestAction::assert("typeof new Date(2020, 6, 8).toLocaleDateString() === 'string'"),
TestAction::assert("typeof new Date(2020, 6, 8).toLocaleTimeString() === 'string'"),
TestAction::assert("typeof new Date(0).toLocaleString('en-US') === 'string'"),
TestAction::assert("typeof new Date(0).toLocaleDateString('en-US') === 'string'"),
TestAction::assert("typeof new Date(0).toLocaleDateString('de-DE') === 'string'"),
TestAction::assert("typeof new Date(0).toLocaleTimeString('en-US') === 'string'"),
// Prove locale pipeline: different locales produce different output
TestAction::assert(
"new Date(0).toLocaleDateString('en-US') !== new Date(0).toLocaleDateString('de-DE')",
),
TestAction::assert(
"new Date(0).toLocaleString('en-US') !== new Date(0).toLocaleString('de-DE')",
),
TestAction::assert(
"new Date(0).toLocaleTimeString('en-US') !== new Date(0).toLocaleTimeString('de-DE')",
),
// Prove ToDateTimeOptions pipeline: options affect output
TestAction::assert(
"typeof new Date(0).toLocaleDateString('en-US', { dateStyle: 'short' }) === 'string'",
),
// Prove output is a string and not empty
TestAction::assert(
"new Date(0).toLocaleDateString('en-US', { dateStyle: 'short' }).length > 0",
),
]);
}
Loading
Loading