Simplify TryParseFormatO fractional-seconds handling#129006
Closed
danmoseley wants to merge 1 commit into
Closed
Conversation
The "O" (ISO 8601 round-trip) format always contains exactly 7
fractional-second digits, matching DateTime's tick precision
(TimeSpan.TicksPerSecond == 10_000_000). The integer value of those
seven digits is therefore already the sub-second tick count.
The existing code computed `n / 1e7` then `Math.Round(... * 1e7)` to get
back to ticks -- a no-op for every value the parser will ever see. Each
7-digit fraction is <= 9_999_999 < 2^24, exactly representable as
double; 1e7 is exactly representable; the two correctly-rounded ops
bound the absolute error well under 0.5; Math.Round then recovers the
original integer. (The round-trip must already be exact today, since
DateTime.ToString("O") writes the same 7 digits -- otherwise
format-then-parse would drift.)
Keep the value as int and pass it straight to TryAddTicks. Bit-exact
equivalent, smaller, more obvious.
Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-datetime |
Copilot stopped reviewing on behalf of
danmoseley due to an error
June 4, 2026 19:38
This was referenced Jun 4, 2026
Open
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
"O"(ISO 8601 round-trip) format always contains exactly 7 fractional-second digits, matchingDateTime's tick precision (TimeSpan.TicksPerSecond == 10_000_000). The integer value of those seven digits is therefore already the sub-second tick count.Today's code in
TryParseFormatO:/ 1e7thenMath.Round(... * 1e7)is identity for every value the parser will ever see. Each 7-digit fraction is<= 9_999_999 < 2^24, exactly representable asdouble;1e7is exactly representable; the two correctly-rounded ops bound absolute error well under 0.5;Math.Roundthen recovers the original integer. (The round-trip must already be exact today, sinceDateTime.ToString("O")writes the same 7 digits — otherwise format-then-parse would drift.)So this just keeps the value as
int fractionTicksand passes it straight toTryAddTicks. Bit-exact equivalent, smaller, more obvious.Risk
None. Existing
"O"-format round-trip tests inDateTimeTestscontinue to pass.Perf
Small win on
Perf_DateTime.ParseOmicro-benchmark, but the absolute magnitude is hard to pin down precisely against R2R'd CoreLib because tiny IL changes can shift crossgen code layout: a short-job A/B showedParseOat ~9% but the unmodifiedParseRcontrol also drifted ~8%, indicating layout noise dominates at this measurement length. The change is justified as a simplification; perf is a side benefit.