Repository navigation
Read Microsecond and Nanosecond straight from the ticks - #715
Merged
Merged
Conversation
This was referenced Oct 12, 2026
This was referenced Oct 14, 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.
The
Microsecond/Nanosecondpolyfills ofDateTimeandDateTimeOffset, andMicroseconds/NanosecondsofTimeSpan(all below net7), rebuilt the value from its components (new DateTime(Year, Month, Day, Hour, Minute, 0, Kind)and a subtraction) to get at the ticks within the minute.They now use the formulas the BCL uses:
DateTimeOffsetreadsUtcTicks: an offset is a whole number of minutes, so the UTC ticks have the same sub-second part as the clock time, andUtcTicksdoes not have to build the clockDateTimefirst.The results are identical. The old code effectively computed
Ticks % TicksPerMinute; a minute is a whole number of microseconds and of milliseconds, so dividing and taking the remainder on the full tick count gives the same digits, with the same sign for negativeTimeSpans (C# division and remainder both truncate toward zero, as theTimeSpancomponents do).Tests
DateTime: for eachDateTimeKind,MinValue,MaxValue, values within a tick, a microsecond, a millisecond, a second and a minute of both ends, and 2000 values spread over the whole range. Each is checked against the formula and against what is left of the ticks once the value rebuilt from year to millisecond is taken outDateTimeOffset: the same over the valid range for offsets of 0, +14:00, -14:00, +05:30, +05:45, -03:30, +00:01 and -00:01, also checked againstUtcDateTime.Microsecond/NanosecondTimeSpan:MinValue,MaxValue, zero, small positive and negative values, values around zero and 2000 values spread over the whole range (negative ones included): checked against the formula, that the components (days to nanoseconds) add back up to the ticks, and that every component carries the sign of the valueTimeSpan.MinValueis -580 µs / -800 ns,TimeSpan.MaxValue580 µs / 700 ns,DateTime.MaxValueandDateTimeOffset.MaxValue999 µs / 900 nsThe new tests pass against the previous implementation and against the BCL on net7+.
Measurements
Stopwatch, best of 9 rounds after warm-up, Release. Nothing allocates before or after.
DateTime.MicrosecondDateTime.NanosecondDateTimeOffset.MicrosecondDateTimeOffset.NanosecondTimeSpan.MicrosecondsTimeSpan.Nanoseconds(The figures include about 2 ns of delegate call from the harness.)