8000 Use floating-point arithmetic in MakeTime, MakeDay and MakeDate by xmasdev · Pull Request #869 · dop251/goja · GitHub
[go: up one dir, main page]

Skip to content

Use floating-point arithmetic in MakeTime, MakeDay and MakeDate - #869

Open
xmasdev wants to merge 2 commits into
dop251:masterfrom
xmasdev:fix-date-float-arithmetic
Open

xmasdev wants to merge 2 commits into
dop251:masterfrom
xmasdev:fix-date-float-arithmetic

Conversation

@xmasdev
@xmasdev xmasdev commented Nov 14, 2026 •
Copy link
Copy Markdown

Date.UTC(1970, 0, 1, 80063993375, 29, 1, -288230376151711740) returned 29256 instead of 29312, and Date.UTC(1970, 0, 213503982336, 0, 0, 0, -18446744073709552000) returned NaN instead of 34447360. Those are the values test/built-ins/Date/UTC/fp-evaluation-order.js expects, and the test was in the skip list.

MakeTime, MakeDay and MakeDate are defined on floats, so large components get rounded at each step. goja normalised them as exact int64 values, which gives a different result, clamps arguments outside the int64 range and rejects day counts above MaxInt32 even when the final time is in range.

The date components are now carried as float64 and mkTime follows the spec formulas, then lets time.Date resolve the local time as before. The products are wrapped in explicit float64() conversions so the compiler can't fuse them into FMA instructions on arm64 and similar platforms, which would change the rounding.

The setters now check for an invalid date before unsetting it. They relied on the computation on the placeholder time always succeeding, and that is no longer true when it ends up outside the int64 range.

fp-evaluation-order.js is removed from the skip list. go vet ./... and go test ./... with test262 pass, on amd64 and with GOARCH=386. On 32-bit the time of day goes to time.Date as seconds and nanoseconds, because in nanoseconds it doesn't fit in an int, and values more than a day outside the TimeClip range are rejected before the day count is converted to an int.

Date.UTC(1970, 0, 1, 80063993375, 29, 1, -288230376151711740) returned
29256 instead of 29312, and Date.UTC(1970, 0, 213503982336, 0, 0, 0,
-18446744073709552000) returned NaN instead of 34447360. The spec
combines the date and time components as floats, rounding at each step,
while goja combined them as exact int64 values, which also clamped
arguments outside the int64 range and rejected large day counts.

The components are now carried as floats and combined as in the spec.
The setters check for an invalid date before unsetting it, so a failed
computation on the placeholder time no longer resets a date whose time
was changed in valueOf.

Enables test/built-ins/Date/UTC/fp-evaluation-order.js.
Comment thread builtin_date.go Outdated
month := time.Month(m) + 1
return time.Date(int(year), month, int(day), int(hour), int(min), int(sec), int(nsec), loc), true
d := math.Floor(tv / msPerDay)
return time.Date(1970, time.January, 1+int(d), 0, 0, 0, int(tv-float64(d*msPerDay))*1e6, loc), true
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 breaks on 32-bit targets (CI runs GOARCH=386). int is 32 bits there, and the ms-of-day goes up to 86,399,999, so int(...)*1e6 overflows for anything past ~2.1 s into the day.

On 386 (built with GOOS=linux GOARCH=386 go test -c, run in an i386/alpine container):

Date.UTC(2020, 0, 1, 12)              master: 1577880000000   this PR: 1577836801218  (00:00:01.218)
new Date(2020, 0, 1, 12).getHours()   master: 12              this PR: 0

and the existing TestDateUTC and TestDateSetters fail on this branch (both pass on master).

Splitting the ms into seconds + nanoseconds keeps every value inside int32, and time.Date still normalises the wall clock the same way:

Suggested change
return time.Date(1970, time.January, 1+int(d), 0, 0, 0, int(tv-float64(d*msPerDay))*1e6, loc), true
ms := int(tv - float64(d*msPerDay))
return time.Date(1970, time.January, 1+int(d), 0, 0, ms/1e3, ms%1e3*1e6, loc), true

With that, go test -run Date passes on 386, and fp-evaluation-order.js gives 29312 there too. Native go test -run 'Date|TestTC39' . with test262 still passes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thanks. Applied this. I also made mkTime reject values more than a day past the TimeClip range before the int(d) conversion. Converting a float64 that doesn't fit in an int is implementation defined, so the day count shouldn't depend on it on 32-bit either. The callers still apply the exact TimeClip.

go test ./... with test262 passes on amd64 and with GOARCH=386 now, including TestDateUTC, TestDateSetters and fp-evaluation-order.js.

The time of day was passed to time.Date as nanoseconds, which overflows
an int on 32-bit platforms for anything past about 2.1 seconds into the
day. Pass it as seconds and nanoseconds instead.

Also reject values more than a day outside the TimeClip range before
converting the day count to an int. The callers clip the result anyway,
and this way the conversion cannot overflow on 32-bit platforms either.
@dop251
dop251 commented Nov 19, 2026
Copy link
Copy Markdown
Owner

LGTM, but there is a section in the README.md that lists the old behaviour as a caveat, it needs to be removed.

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.

3 participants

0