diff --git a/core/engine/src/builtins/temporal/instant/mod.rs b/core/engine/src/builtins/temporal/instant/mod.rs index 736fe88cc88..806ac9632c0 100644 --- a/core/engine/src/builtins/temporal/instant/mod.rs +++ b/core/engine/src/builtins/temporal/instant/mod.rs @@ -32,6 +32,9 @@ use temporal_rs::{ options::{RoundingIncrement, RoundingMode, RoundingOptions}, }; +#[cfg(test)] +mod tests; + /// The `Temporal.Instant` built-in implementation /// /// More information: @@ -664,8 +667,11 @@ impl Instant { /// /// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.instant.tolocalestring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant/toLocaleString - fn to_locale_string(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult { - // TODO: Update for ECMA-402 compliance + fn to_locale_string( + this: &JsValue, + args: &[JsValue], + context: &mut Context, + ) -> JsResult { let object = this.as_object(); let instant = object .as_ref() @@ -675,12 +681,37 @@ impl Instant { .with_message("the this object must be a Temporal.Instant object.") })?; - let ixdtf = instant.inner.to_ixdtf_string_with_provider( - None, - ToStringRoundingOptions::default(), - context.timezone_provider(), - )?; - Ok(JsString::from(ixdtf).into()) + #[cfg(feature = "intl")] + { + use crate::builtins::intl::date_time_format::{ + FormatDefaults, FormatType, format_date_time_locale, + }; + + let locales = args.get_or_undefined(0); + let options = args.get_or_undefined(1); + + // Converting epoch milliseconds + let epoch_ms = instant.inner.epoch_milliseconds() as f64; + + format_date_time_locale( + locales, + options, + FormatType::Any, + FormatDefaults::All, + epoch_ms, + context, + ) + } + + #[cfg(not(feature = "intl"))] + { + let ixdtf = instant.inner.to_ixdtf_string_with_provider( + None, + ToStringRoundingOptions::default(), + context.timezone_provider(), + )?; + Ok(JsString::from(ixdtf).into()) + } } /// 8.3.13 `Temporal.Instant.prototype.toJSON ( )` diff --git a/core/engine/src/builtins/temporal/instant/tests.rs b/core/engine/src/builtins/temporal/instant/tests.rs new file mode 100644 index 00000000000..80a40c26568 --- /dev/null +++ b/core/engine/src/builtins/temporal/instant/tests.rs @@ -0,0 +1,19 @@ +use crate::{JsNativeErrorKind, TestAction, run_test_actions}; + +#[test] +#[cfg(feature = "intl_bundled")] +fn instant_to_locale_string() { + run_test_actions([TestAction::assert( + "typeof new Temporal.Instant(0n).toLocaleString() === 'string'", + )]); +} + +#[test] +#[cfg(feature = "intl_bundled")] +fn instant_to_locale_string_invalid_this() { + run_test_actions([TestAction::assert_native_error( + "Temporal.Instant.prototype.toLocaleString.call({})", + JsNativeErrorKind::Type, + "the this object must be a Temporal.Instant object.", + )]); +}