Skip to content

Polyfill: Avoid TimeClip at edge of DateTimeFormat supported range#3284

Open
ptomato wants to merge 1 commit intomainfrom
fix-timeclip
Open

Polyfill: Avoid TimeClip at edge of DateTimeFormat supported range#3284
ptomato wants to merge 1 commit intomainfrom
fix-timeclip

Conversation

@ptomato
Copy link
Collaborator

@ptomato ptomato commented Feb 26, 2026

In the reference polyfill we have to simulate formatting Plain types by using the existing DateTimeFormat methods to format epoch milliseconds.

Since Plain types have a slightly larger range than the exact times that DateTimeFormat could previously format, we need to anticipate when they correspond to an epoch milliseconds value that's out of range. In this case, we calculate a "day adjustment" and apply it to the 'day' part of formatToParts() or formatRangeToParts() to fudge the resulting formatted string back to the day that it should have represented.

This is probably not 100% format-accurate. For one thing, it'll lose leading zeroes in the day, if the format calls for them. This workaround is mainly to make sure we can handle the full range of the type, and assumes that the workaround path will rarely be hit in practice.

In the reference polyfill we have to simulate formatting Plain types by
using the existing DateTimeFormat methods to format epoch milliseconds.

Since Plain types have a slightly larger range than the exact times that
DateTimeFormat could previously format, we need to anticipate when they
correspond to an epoch milliseconds value that's out of range. In this
case, we calculate a "day adjustment" and apply it to the 'day' part of
formatToParts() or formatRangeToParts() to fudge the resulting formatted
string back to the day that it should have represented.

This is probably not 100% format-accurate. For one thing, it'll lose
leading zeroes in the day, if the format calls for them. This workaround
is mainly to make sure we can handle the full range of the type, and
assumes that the workaround path will rarely be hit in practice.
@ptomato ptomato requested a review from Ms2ger February 26, 2026 23:34
@ptomato
Copy link
Collaborator Author

ptomato commented Feb 26, 2026

See tc39/test262#4954 for test coverage

@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

❌ Patch coverage is 47.66355% with 56 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.42%. Comparing base (385b479) to head (04d573c).

Files with missing lines Patch % Lines
polyfill/lib/intl.mjs 46.66% 53 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3284      +/-   ##
==========================================
- Coverage   97.93%   97.42%   -0.51%     
==========================================
  Files          22       22              
  Lines       10593    10677      +84     
  Branches     1829     1840      +11     
==========================================
+ Hits        10374    10402      +28     
- Misses        201      254      +53     
- Partials       18       21       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ptomato
Copy link
Collaborator Author

ptomato commented Feb 27, 2026

(The missing code coverage lines should resolve themselves once we update the tests)

@anba
Copy link
Contributor

anba commented Feb 27, 2026

I think it's also necessary to adjust the weekday and to take the numbering system into account.

For example this test case should print "๑๓ Saturday":

new Temporal.PlainDate(275760, 9, 13).toLocaleString("en-u-nu-thai", {day: "numeric", weekday: "long"})

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