Skip to content
Closed
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
23 changes: 20 additions & 3 deletions core/engine/src/builtins/temporal/plain_date_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,10 +1456,25 @@ impl PlainDateTime {
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.with
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/with
fn to_locale_string(this: &JsValue, _args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tolocalestring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime/toLocaleString
fn to_locale_string(
this: &JsValue,
_args: &[JsValue],
_context: &mut Context,
) -> JsResult<JsValue> {
// TODO: Update for ECMA-402 compliance
// The spec requires integration with Intl.DateTimeFormat which is still in progress.
// For now, this returns the ISO string representation similar to toString().
//
// Proper implementation should:
// 1. Create an Intl.DateTimeFormat with the provided locales and options
// 2. Convert PlainDateTime to a formattable datetime
// 3. Format using the DateTimeFormat
//
// See: https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.tolocalestring
// and https://tc39.es/ecma402/#sec-datetimeformat-objects

let object = this.as_object();
let dt = object
.as_ref()
Expand All @@ -1468,6 +1483,8 @@ impl PlainDateTime {
JsNativeError::typ().with_message("the this object must be a PlainDateTime object.")
})?;

// Temporary implementation until full ECMA-402 integration
// This matches the current behavior of ZonedDateTime.toLocaleString
let ixdtf = dt
.inner
.to_ixdtf_string(ToStringRoundingOptions::default(), DisplayCalendar::Auto)?;
Expand Down
38 changes: 38 additions & 0 deletions core/engine/src/builtins/temporal/plain_date_time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,41 @@ fn pdt_year_of_week_basic() {
TestAction::assert_eq("pdt.yearOfWeek", 1976),
]);
}

#[test]
fn pdt_to_locale_string_basic() {
run_test_actions([
TestAction::run(
"let pdt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789, 'iso8601')",
),
// toLocaleString should return a string (currently falls back to ISO format)
TestAction::assert("typeof pdt.toLocaleString() === 'string'"),
// Should contain date and time components
TestAction::assert("pdt.toLocaleString().includes('1976')"),
TestAction::assert("pdt.toLocaleString().includes('11')"),
TestAction::assert("pdt.toLocaleString().includes('18')"),
]);
}

#[test]
fn pdt_to_locale_string_with_locales() {
run_test_actions([
TestAction::run(
"let pdt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789, 'iso8601')",
),
// Should accept locales parameter (even if not fully implemented yet)
TestAction::assert("typeof pdt.toLocaleString('en-US') === 'string'"),
TestAction::assert("typeof pdt.toLocaleString(['en-US', 'fr-FR']) === 'string'"),
]);
}

#[test]
fn pdt_to_locale_string_with_options() {
run_test_actions([
TestAction::run(
"let pdt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789, 'iso8601')",
),
// Should accept options parameter (even if not fully implemented yet)
TestAction::assert("typeof pdt.toLocaleString(undefined, {}) === 'string'"),
]);
}
Loading