Date.prototype.setTime must store the clipped time value - #3042
Merged
Conversation
Date.prototype.setTime computed TimeClip(t) and returned it, but set [[DateValue]] to the unclipped t. https://tc39.es/ecma262/#sec-date.prototype.settime sets the slot to v, and every sibling setter in the file already did; this was the one that did not. It is visible because DatePresentation represents an infinity as a flag plus a Value of 0, and ToJsValue tested only IsNaN, so the stored infinity read back as the epoch: `d.setTime(Infinity); d.getTime()` answered 0 while setTime had just answered NaN. An out-of-range finite argument survived in the object too - `d.setTime(8.64e15 + 1)` kept 8640000000000001, which getTime reported as NaN but toISOString happily rendered as "+275760-09-13T00:00:00.001Z". toISOString has the same shape of gap one level down. Its guard asked whether the time value was NaN, which is exactly what the spec asks, but only because a spec [[DateValue]] is either NaN or a finite integral number in range. DatePresentation is wider than that domain, so an infinity-flagged value walked past the guard and the formatter emitted "1970-01-01T00:00:00.000Z" for it. It now tests IsFinite, which is the same step expressed over the representation Jint actually has. ToJsValue is hardened the same way, so the class of defect is closed rather than the instance: an infinity-flagged presentation can no longer read back as a number whichever caller reaches it. The other readers are unaffected - Date.parse builds from `out long` or DatePresentation.NaN, Date.UTC clips first, and the DateTimeMinValue/DateTimeMaxValue sentinels are finite in-range values that keep reading back as numbers. TimeClip itself was already right and is untouched. Frees staging/sm/Date/timeclip.js and staging/sm/Date/toISOString.js. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
lahma
force-pushed
the
staging/date-timeclip
branch
from
August 18, 2026 17:12
afdf2fa to
caea9fd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Date.prototype.setTimestored the unclipped argument:21.4.4.27 step 5 sets
[[DateValue]]tov. All 15 sibling setters already do this correctly —setTimewas the sole outlier.The effect is larger than "an infinity survives".
DatePresentation's conversion turns±Infinityinto a flagged presentation withValue = 0, andToJsValue()tested onlyIsNaN, so an infinity read back as the epoch:The last two are finite-but-out-of-range, so they are not fixed by a
toISOStringguard alone — only storingvfixes them. (Number.MAX_VALUEproduced year 292278994 because thedouble->longcast saturates.)The fix
Three lines:
setTimestoresv.toISOStringguards on "not finite" rather than "is NaN".DatePresentation.ToJsValue()answersNaNfor any non-finite presentation, not just a NaN one.On (2): 21.4.4.36 step 4 literally says "If tv is NaN, throw a RangeError", and in the spec that is the whole of "not a finite time value" — a
[[DateValue]]is by construction either NaN or a finite in-range integral Number.DatePresentationis wider than that domain, soIsFiniteis the same step expressed over the representation Jint actually has. The code comment says exactly this rather than misquoting the spec.(3) closes the class rather than the instance. Every
ToJsValueconsumer was checked; the only behavioural delta isIsInfinityreading back asNaNinstead of0. In particular theDateTimeMinValue/DateTimeMaxValueflags are not in the finite mask but their values sit well inside the TimeClip range, so they still read back as numbers — pinned by a test.Tests
Jint.Tests/Runtime/DateTests.cs— 30 cases, 9 failing against unfixed code, coveringsetTimewith±Infinity/NaN/8.64e15 + 1/Number.MAX_VALUE, the exact8.64e15boundary,toISOStringraisingRangeError, andsetTimeagreeing with the subsequentgetTime/valueOf. No existing expectation changed.Frees two
staging/exclusions:Date/timeclip.jsandDate/toISOString.js.Refs #3021.