Preserve sub-second precision when coercing a DateTime in Time.at - #58085
Merged
byroot merged 1 commit intoJul 16, 2026
Conversation
Time.at(datetime) round-trips a DateTime through a Float unix timestamp, which cannot represent every microsecond exactly, so the resulting Time.usec was wrong for roughly half of all sub-second values. The sibling TimeWithZone branch already passes an exact rational timestamp; the DateTime branch now does the same, using the exact fractional second. dt = DateTime.civil(2000, 1, 1, 0, 0, Rational(1, 1_000_000)) # .000001s Time.at(dt).usec # before => 0 # after => 1 Whole-second, offset, and pre-epoch values are unaffected.
55728
force-pushed
the
fix-time-at-datetime-subsecond-precision
branch
from
July 15, 2026 15:04
a2e7517 to
1432277
Compare
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.
Summary
Time.ataccepts anActiveSupport::TimeWithZoneor aDateTimeas a single argument (ActiveSupport layers this on top ofTime.at). TheTimeWithZonebranch converts via an exact rational timestamp (to_r), but theDateTimebranch converted via aFloat(to_f). AFloatcannot represent every microsecond exactly, so the resultingTime#usecwas wrong for roughly half of all sub-secondDateTimevalues.Before / After
Why this is correct
DateTimebranch in line with theTimeWithZonesibling, which already passes an exact rational timestamp so precision survives the round-trip. The exact fractional second isdatetime.to_i + datetime.sec_fraction.datetime.to_i + datetime.sec_fractionexactly, and the returnedTimeis still local (getlocal).