Repository navigation
Conversation
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.
| 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 |
There was a problem hiding this comment.
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:
| 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.
There was a problem hiding this comment.
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.
|
LGTM, but there is a section in the README.md that lists the old behaviour as a caveat, it needs to be removed. |
Date.UTC(1970, 0, 1, 80063993375, 29, 1, -288230376151711740)returned29256instead of29312, andDate.UTC(1970, 0, 213503982336, 0, 0, 0, -18446744073709552000)returnedNaNinstead of34447360. Those are the valuestest/built-ins/Date/UTC/fp-evaluation-order.jsexpects, 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
mkTimefollows the spec formulas, then letstime.Dateresolve the local time as before. The products are wrapped in explicitfloat64()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.jsis removed from the skip list.go vet ./...andgo test ./...with test262 pass, on amd64 and withGOARCH=386. On 32-bit the time of day goes totime.Dateas 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.