Skip to content
Open
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
47 changes: 39 additions & 8 deletions core/engine/src/builtins/temporal/instant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ use temporal_rs::{
options::{RoundingIncrement, RoundingMode, RoundingOptions},
};

#[cfg(test)]
mod tests;

/// The `Temporal.Instant` built-in implementation
///
/// More information:
Expand Down Expand Up @@ -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<JsValue> {
// TODO: Update for ECMA-402 compliance
fn to_locale_string(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let object = this.as_object();
let instant = object
.as_ref()
Expand All @@ -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 ( )`
Expand Down
19 changes: 19 additions & 0 deletions core/engine/src/builtins/temporal/instant/tests.rs
Original file line number Diff line number Diff line change
@@ -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.",
)]);
}
Loading