Skip to content

Date.prototype.setTime must store the clipped time value - #3042

Merged
lahma merged 1 commit into
sebastienros:mainfrom
lahma:staging/date-timeclip
Aug 19, 2026
Merged

Date.prototype.setTime must store the clipped time value#3042
lahma merged 1 commit into
sebastienros:mainfrom
lahma:staging/date-timeclip

Conversation

@lahma

@lahma lahma commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator

Date.prototype.setTime stored the unclipped argument:

var v = t.TimeClip();
((JsDate) thisObject)._dateValue = t;   // spec stores v
return v.ToJsValue();

21.4.4.27 step 5 sets [[DateValue]] to v. All 15 sibling setters already do this correctly — setTime was the sole outlier.

The effect is larger than "an infinity survives". DatePresentation's conversion turns ±Infinity into a flagged presentation with Value = 0, and ToJsValue() tested only IsNaN, so an infinity read back as the epoch:

var d = new Date(); d.setTime(Infinity); d.getTime();   // 0, should be NaN
d.setTime(8.64e15 + 1); d.getTime();                    // NaN, but...
d.toISOString();                                        // "+275760-09-13T00:00:00.001Z"
d.setTime(Number.MAX_VALUE); d.toISOString();           // "+292278994-08-17T07:12:56.807Z"

The last two are finite-but-out-of-range, so they are not fixed by a toISOString guard alone — only storing v fixes them. (Number.MAX_VALUE produced year 292278994 because the double->long cast saturates.)

The fix

Three lines:

  1. setTime stores v.
  2. toISOString guards on "not finite" rather than "is NaN".
  3. DatePresentation.ToJsValue() answers NaN for 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. DatePresentation is wider than that domain, so IsFinite is 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 ToJsValue consumer was checked; the only behavioural delta is IsInfinity reading back as NaN instead of 0. In particular the DateTimeMinValue/DateTimeMaxValue flags 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, covering setTime with ±Infinity/NaN/8.64e15 + 1/Number.MAX_VALUE, the exact 8.64e15 boundary, toISOString raising RangeError, and setTime agreeing with the subsequent getTime/valueOf. No existing expectation changed.

Frees two staging/ exclusions: Date/timeclip.js and Date/toISOString.js.

Refs #3021.

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
lahma force-pushed the staging/date-timeclip branch from afdf2fa to caea9fd Compare August 18, 2026 17:12
@lahma
lahma merged commit 2d6d8d3 into sebastienros:main Aug 19, 2026
5 checks passed
@lahma
lahma deleted the staging/date-timeclip branch August 19, 2026 10:23
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