Skip to content

fix(codemode): honor locale and options in Date toLocale methods - #64938

Merged
rekram1-node merged 5 commits into
devfrom
date-locale-options
Nov 19, 2026
Merged

rekram1-node merged 5 commits into
devfrom
date-locale-options

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Date.prototype.toLocaleString, toLocaleDateString and toLocaleTimeString ignored their arguments and always printed en-US in UTC, so timeZone, weekday, dateStyle, hour12, ... and the locale were silently dropped, and under a non-UTC TZ the output disagreed with getHours()/toDateString(). They now delegate to the host's Intl.DateTimeFormat with the program's locale and options.

const d = new Date(Date.UTC(2024, 0, 15, 13, 5, 9))
d.toLocaleString("en-US", { timeZone: "Asia/Tokyo" })
d.toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" })
d.toLocaleString("de-DE", { timeZone: "UTC" })
d.toLocaleString() // TZ=America/New_York
Bun 1.4.4 before after
Tokyo 1/15/2024, 10:05:09 PM 1/15/2024, 1:05:09 PM 1/15/2024, 10:05:09 PM
month/day Jan 15 1/15/2024 Jan 15
de-DE 15.1.2024, 13:05:09 1/15/2024, 1:05:09 PM 15.1.2024, 13:05:09
no args, NY 1/15/2024, 8:05:09 AM 1/15/2024, 1:05:09 PM 1/15/2024, 8:05:09 AM

No-argument default. Bun formats with en-US regardless of LANG/LC_ALL, in the local timezone. The old pin to UTC was meant to keep output host-independent, but getHours, toDateString, toTimeString and getTimezoneOffset already use the host's local time, so the pin only made toLocale* contradict them. This keeps the locale pinned to en-US (Bun's default, and deterministic) and drops the UTC pin, so no-argument output matches Bun and the other local-time methods.

Option objects are read in VM space: program getters run once (through withReads) and object values are converted with ToString/ToNumber before reaching the host; hour12 uses ToBoolean. Non-array objects as locales are read as array-like, as Intl does. Invalid input throws the host's RangeError/TypeError (null locales or options throw a Code Mode TypeError so the host's message doesn't leak its own source). No global Intl is added. Number.prototype.toLocaleString is untouched.

Testing

  • New stdlib.test.ts cases: locale/options (Tokyo, weekday, month/day, de-DE, en-GB, locale arrays, dateStyle/timeStyle, timeZoneName, hour12, invalid Date), no-argument output matching the host and getHours, getter-once and object conversion, and Intl errors. All use explicit time zones or compare against the host, so they pass under TZ=UTC, Asia/Tokyo and America/New_York.
  • bun test in packages/codemode: 1789 pass, 0 fail.
  • bun typecheck (codemode), bun run check (root).

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="none" font-family="ui-monospace,monospace" font-size="11">
<style>
.a{animation:a 4s infinite}.b{animation:b 4s infinite}.c{animation:c 4s infinite}
@keyframes a{0%,30%{opacity:1}40%,100%{opacity:0}}
@keyframes b{0%,35%{opacity:0}45%,65%{opacity:1}75%,100%{opacity:0}}
@keyframes c{0%,70%{opacity:0}80%,100%{opacity:1}}
@media (prefers-reduced-motion:reduce){.a,.b{animation:none;opacity:0}.c{animation:none;opacity:1}}
</style>
<rect x="1" y="1" width="238" height="70" rx="8" stroke="currentColor" opacity=".25"/>
<text x="12" y="22" fill="currentColor" opacity=".6">toLocaleString(locale, options)</text>
<g fill="currentColor">
<text class="a" x="12" y="50">"en-US"  1/15/2024, 1:05 PM</text>
<text class="b" x="12" y="50">"de-DE"  15.1.2024, 13:05:09</text>
<text class="c" x="12" y="50">Tokyo    1/15/2024, 10:05 PM</text>
</g>
<rect x="12" y="58" width="216" height="2" rx="1" fill="currentColor" opacity=".2"/>
</svg>

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This fixes the bug as described. On the base, with TZ=America/New_York, all of the description's examples print 1/15/2024, 1:05:09 PM while getHours() returns 8. With this PR they match Bun (10:05:09 PM for Tokyo, Jan 15, 15.1.2024, 13:05:09, 8:05:09 AM with no arguments). Getters, inherited options, hour12 given an object, hourCycle, fractionalSecondDigits and holes in the locale array all behave correctly. stdlib.test.ts passes (154 tests), and the toLocale tests also pass under TZ=Asia/Tokyo. tsc --noEmit is clean. bun run typecheck fails here only because this Bun version (1.4.2) has no bun check.

Public API: unchanged. Only the behaviour of Date methods inside Code Mode programs changes.

One edge case is worth fixing: locales isn't read the way Intl reads it (see the inline comment).

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commits fix both points from my last review. toLocaleString(null) now throws a Code Mode TypeError instead of the host's message, and non-array objects as locales are read as array-like lists: {} gives the default output and { length: 2, 1: "de-DE" } formats in German. I checked this with probe scripts through CodeMode.execute. stdlib.test.ts passes (154 tests) and tsc --noEmit is clean.

Public API: unchanged.

Nothing new to raise.

@rekram1-node

Copy link
Copy Markdown
Member Author

Security follow-up: locale lists over 100 entries (arrays and array-likes, checked before any element is read) and tags over 256 characters now throw a catchable RangeError before reaching host Intl, so one call can no longer stall the host event loop.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit's locale caps work as described. I checked them through CodeMode.execute: 100 locales format; 101, in an array or an array-like, throw the RangeError; { length: 2 ** 32 } throws without reading any element; and a tag over 256 characters, including one built by toString, is rejected. Long timeZone/calendar/numberingSystem values fail quickly in the host, so limiting tags alone looks enough. stdlib.test.ts passes (155 tests) and tsc --noEmit is clean.

Public API: unchanged.

Nothing new to raise.

@rekram1-node

Copy link
Copy Markdown
Member Author

Removed the size caps from this PR at the maintainer's request.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Dropping the tag-length cap reopens a way to hang a run that timeoutMs can't stop: formatting with one long locale tag takes time that grows with the square of its length (see inline). new Date(0).toLocaleString("de-x-" + "abcdefgh-".repeat(200000) + "a") ran for 15.8 s under limits: { timeoutMs: 1000 } and then returned normally. A 3.6M-character tag takes 82 s in Bun, and Code Mode strings can be up to 16M characters. The new array-like cap at 10M is fine: { length: 2 ** 32 } throws Invalid array length and { length: 1e7 } finishes in about 0.7 s. stdlib.test.ts passes (154 tests) and tsc --noEmit is clean.

Public API: unchanged.

],
[...tags, ...entries.map((entry) => entry[1])],
(primitives) =>
new Date(time)[name](

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With the tag-length check gone, a program-built tag reaches Bun's Intl without any limit, and Bun's tag parsing takes time that grows roughly with the square of the length. In Bun I measured 0.9M chars → 2.6 s, 1.8M → 15.9 s, 3.6M → 82 s, and Code Mode allows strings up to 16M characters. This runs as one host call that timeoutMs can't interrupt: a 1.8M-char tag ran 15.8 s under timeoutMs: 1000 and returned normally. Could the length check on string tags (the old MAX_LOCALE_LENGTH check, or any small bound) come back, even without the locale-count cap?

@rekram1-node
rekram1-node merged commit a21895a into dev Nov 19, 2026
12 checks passed
@rekram1-node
rekram1-node deleted the date-locale-options branch November 19, 2026 21:27
@amrkmn amrkmn mentioned this pull request Nov 23, 2026
1 task
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.

1 participant