Skip to content

Commit fb2d992

Browse files
committed
feat(intl): implement Temporal.Instant.prototype.toLocaleString
1 parent f075094 commit fb2d992

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

core/engine/src/builtins/temporal/instant/mod.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ use temporal_rs::{
3232
options::{RoundingIncrement, RoundingMode, RoundingOptions},
3333
};
3434

35+
#[cfg(test)]
36+
mod tests;
37+
3538
/// The `Temporal.Instant` built-in implementation
3639
///
3740
/// More information:
@@ -664,8 +667,11 @@ impl Instant {
664667
///
665668
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.instant.tolocalestring
666669
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant/toLocaleString
667-
fn to_locale_string(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
668-
// TODO: Update for ECMA-402 compliance
670+
fn to_locale_string(
671+
this: &JsValue,
672+
args: &[JsValue],
673+
context: &mut Context,
674+
) -> JsResult<JsValue> {
669675
let object = this.as_object();
670676
let instant = object
671677
.as_ref()
@@ -675,12 +681,37 @@ impl Instant {
675681
.with_message("the this object must be a Temporal.Instant object.")
676682
})?;
677683

678-
let ixdtf = instant.inner.to_ixdtf_string_with_provider(
679-
None,
680-
ToStringRoundingOptions::default(),
681-
context.timezone_provider(),
682-
)?;
683-
Ok(JsString::from(ixdtf).into())
684+
#[cfg(feature = "intl")]
685+
{
686+
use crate::builtins::intl::date_time_format::{
687+
FormatDefaults, FormatType, format_date_time_locale,
688+
};
689+
690+
let locales = args.get_or_undefined(0);
691+
let options = args.get_or_undefined(1);
692+
693+
// Converting epoch milliseconds
694+
let epoch_ms = instant.inner.epoch_milliseconds() as f64;
695+
696+
return format_date_time_locale(
697+
locales,
698+
options,
699+
FormatType::Any,
700+
FormatDefaults::All,
701+
epoch_ms,
702+
context,
703+
);
704+
}
705+
706+
#[cfg(not(feature = "intl"))]
707+
{
708+
let ixdtf = instant.inner.to_ixdtf_string_with_provider(
709+
None,
710+
ToStringRoundingOptions::default(),
711+
context.timezone_provider(),
712+
)?;
713+
Ok(JsString::from(ixdtf).into())
714+
}
684715
}
685716

686717
/// 8.3.13 `Temporal.Instant.prototype.toJSON ( )`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::{JsNativeErrorKind, TestAction, run_test_actions};
2+
3+
#[test]
4+
#[cfg(feature = "intl_bundled")]
5+
fn instant_to_locale_string() {
6+
run_test_actions([TestAction::assert(
7+
"typeof new Temporal.Instant(0n).toLocaleString() === 'string'",
8+
)]);
9+
}
10+
11+
#[test]
12+
#[cfg(feature = "intl_bundled")]
13+
fn instant_to_locale_string_invalid_this() {
14+
run_test_actions([TestAction::assert_native_error(
15+
"Temporal.Instant.prototype.toLocaleString.call({})",
16+
JsNativeErrorKind::Type,
17+
"the this object must be a Temporal.Instant object.",
18+
)]);
19+
}

0 commit comments

Comments
 (0)