Skip to content

fix(temporal): align Duration toLocaleString with spec fallback and i…#5255

Open
Veercodeprog wants to merge 1 commit intoboa-dev:mainfrom
Veercodeprog:fix/temporal-duration-valueof-throws-5088
Open

fix(temporal): align Duration toLocaleString with spec fallback and i…#5255
Veercodeprog wants to merge 1 commit intoboa-dev:mainfrom
Veercodeprog:fix/temporal-duration-valueof-throws-5088

Conversation

@Veercodeprog
Copy link

@Veercodeprog Veercodeprog commented Mar 24, 2026

This PR brings Temporal.Duration.prototype.toLocaleString() and Temporal.Duration.prototype.valueOf() closer to the Temporal spec.

Since Boa does not currently implement Intl.DurationFormat, Duration.prototype.toLocaleString() should follow the spec-defined fallback behavior. In practice, that means returning the same serialized output as toJSON().

This change also keeps Duration.prototype.valueOf() as a throwing method, while updating the error message to point users to the APIs that are actually appropriate for comparison and string conversion.

Motivation

toLocaleString() previously produced output equivalent to the default duration serialization, but the implementation did not clearly reflect the spec's fallback path when ECMA-402 duration formatting is unavailable.

valueOf() already threw as expected, but the error message was not aligned with the spec note and did not direct users toward the best alternatives.

Changes

Temporal.Duration.prototype.toLocaleString()

  • Make toLocaleString() delegate to toJSON().
  • Preserve the expected API shape with locales and options, even though those arguments are currently ignored.
  • Update the Rustdoc to document the current behavior and clarify that Intl.DurationFormat is not implemented yet.

Temporal.Duration.prototype.valueOf()

  • Keep the method behavior unchanged in that it always throws TypeError.
  • Update the error message to reference:
    • Temporal.Duration.compare()
    • Temporal.Duration.prototype.toString()

Tests

Added test coverage for the following cases in core/engine/src/builtins/temporal/duration/tests.rs:

  • toLocaleString() returns the same result as toJSON().
  • toLocaleString(locales, options) still returns the same result while Intl-backed formatting is not implemented.
  • valueOf() throws TypeError with the updated error message.

Testing

cargo test -p boa_engine duration_to_locale_string_matches
cargo test -p boa_engine duration_value_of_throws

Note:
This PR does not implement localized duration formatting.

Proper locale-aware output will require Intl.DurationFormat support, along with the necessary locale data plumbing. This change only fixes the spec-defined fallback behavior used when that functionality is not available yet.

Copy link
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unclear on the purpose for this PR.

The implementation deleted is the exact same implementation as Duration::to_json, so we are just adding an unnecessary level of indirection for the same output.

There is probably a good argument that a more descriptive value_of message could be made, but this would be across all Temporal built-ins. But I think I'd expect it a bit more similar to the preexisting error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants