Replace Time.Sub call in TIMESTAMPDIFF with microsecondsDiff#3410
Merged
angelamayxie merged 8 commits intomainfrom Feb 3, 2026
Merged
Replace Time.Sub call in TIMESTAMPDIFF with microsecondsDiff#3410angelamayxie merged 8 commits intomainfrom
Time.Sub call in TIMESTAMPDIFF with microsecondsDiff#3410angelamayxie merged 8 commits intomainfrom
Conversation
This was referenced Feb 3, 2026
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.
fixes dolthub/dolt#10397
Time.Subdoesn'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 becauseTime.Subreturns aDuration, which is really anint64representing 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.SubusesTime.secto normalizeTimeobjects to seconds since the Jan 1, 0001 UTC epoch. ButTime.secisn't public so we can't call it ourselves. AndTime.SecondandTime.Nanosecondonly give the second and nanosecond portion of a wall time, not the seconds/nanoseconds since an epoch. However,Time.UnixMicrodoes give us the microseconds since Unix epoch (January 1, 1970 UTC)...by callingTime.secand then converting that to Unix time.So
microsecondsDiffcalculates the difference in microseconds between twoTimeobjects, getting their microsecond values by callingTime.UnixMicroon both of them. This isn't the most efficient but it's the best we can do with public functions.