Skip to content

Replace Time.Sub call in TIMESTAMPDIFF with microsecondsDiff#3410

Merged
angelamayxie merged 8 commits intomainfrom
angela/timesub
Feb 3, 2026
Merged

Replace Time.Sub call in TIMESTAMPDIFF with microsecondsDiff#3410
angelamayxie merged 8 commits intomainfrom
angela/timesub

Conversation

@angelamayxie
Copy link
Copy Markdown
Contributor

fixes dolthub/dolt#10397

Time.Sub doesn't work for times with a difference greater than 9,223,372,036,854,775,807 (2^63 - 1) nanoseconds, or ~292.47 years. This is because Time.Sub returns a Duration, which is really an int64 representing nanoseconds. MySQL only stores time precision to the microsecond so we actually don't care about the difference in nanoseconds.

However, there's no easy way to directly expose the number of microseconds or seconds since epoch using the public functions for Time -- this is because seconds since epoch are encoded differently with different epochs depending on whether the time is monotonic or not (Jan 1, 1885 UTC or Jan 1, 0001 UTC).

Time.Sub uses Time.sec to normalize Time objects to seconds since the Jan 1, 0001 UTC epoch. But Time.sec isn't public so we can't call it ourselves. And Time.Second and Time.Nanosecond only give the second and nanosecond portion of a wall time, not the seconds/nanoseconds since an epoch. However, Time.UnixMicro does give us the microseconds since Unix epoch (January 1, 1970 UTC)...by calling Time.sec and then converting that to Unix time.

So microsecondsDiff calculates the difference in microseconds between two Time objects, getting their microsecond values by calling Time.UnixMicro on both of them. This isn't the most efficient but it's the best we can do with public functions.

@angelamayxie angelamayxie requested a review from jycor February 3, 2026 00:07
@angelamayxie angelamayxie marked this pull request as ready for review February 3, 2026 00:07
Copy link
Copy Markdown
Contributor

@jycor jycor left a comment

Choose a reason for hiding this comment

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

LGTM

@angelamayxie angelamayxie merged commit d81a7e2 into main Feb 3, 2026
8 of 9 checks passed
@angelamayxie angelamayxie deleted the angela/timesub branch February 3, 2026 01:50
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.

TIMESTAMPDIFF calculations are wrong for time differences over ~292 years

2 participants