-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AddSeconds and FromSeconds rounding issues #66815
Comments
It might be somewhat worse than the title/issue describe Matt... It's not just Also, like you said, the behavior of all the |
If we could go back in time, I want something closer to JSR310/NodaTime. |
I'm pretty sure there's a dupe, but can't find it now. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsDescription
Reproduction Steps[Fact]
public void TestDateTimeOffsetAddSeconds()
{
var seconds = 0.9999999;
var ticks = DateTimeOffset.MinValue.AddSeconds(seconds).Ticks;
Assert.Equal(9999999, ticks);
}
[Fact]
public void TestDateTimeOffsetAddTimeSpanFromSeconds()
{
var seconds = 0.9999999;
var timeSpan = TimeSpan.FromSeconds(seconds);
var ticks = DateTimeOffset.MinValue.Add(timeSpan).Ticks;
Assert.Equal(9999999, ticks);
}
[Fact]
public void TestDateTimeOffsetAddTimeSpanFromTicks()
{
var seconds = 0.9999999;
var timeSpan = TimeSpan.FromTicks((long)(seconds * TimeSpan.TicksPerSecond));
var ticks = DateTimeOffset.MinValue.Add(timeSpan).Ticks;
Assert.Equal(9999999, ticks);
}
[Fact]
public void TestDateTimeOffsetAddTicks()
{
var seconds = 0.9999999;
var ticks = DateTimeOffset.MinValue.AddTicks((long)(seconds * TimeSpan.TicksPerSecond)).Ticks;
Assert.Equal(9999999, ticks);
}
[Fact]
public void TestDateTimeAddSeconds()
{
var seconds = 0.9999999;
var ticks = DateTime.MinValue.AddSeconds(seconds).Ticks;
Assert.Equal(9999999, ticks);
}
[Fact]
public void TestDateTimeAddTimeSpanFromSeconds()
{
var seconds = 0.9999999;
var timeSpan = TimeSpan.FromSeconds(seconds);
var ticks = DateTime.MinValue.Add(timeSpan).Ticks;
Assert.Equal(9999999, ticks);
}
[Fact]
public void TestDateTimeAddTimeSpanFromTicks()
{
var seconds = 0.9999999;
var timeSpan = TimeSpan.FromTicks((long)(seconds * TimeSpan.TicksPerSecond));
var ticks = DateTime.MinValue.Add(timeSpan).Ticks;
Assert.Equal(9999999, ticks);
}
[Fact]
public void TestDateTimeAddTicks()
{
var seconds = 0.9999999;
var ticks = DateTime.MinValue.AddTicks((long)(seconds * TimeSpan.TicksPerSecond)).Ticks;
Assert.Equal(9999999, ticks);
} Expected behaviorPreferably, all tests should pass. Rounding should not occur in any scenario. However, at minimum, the behavior should be consistent across all supported implementations. Actual behavior
Regression?? Known WorkaroundsCompute ticks and add ticks, or create a timespan from those ticks. ConfigurationTested on Windows x64 on .NET Core 2.1, 3.0, 3.1, .NET 5.0, 6.0 and .NET Framework 4.6.1, 4.6.2, 4.7.1, 4.7.2, and 4.8. Other informationNo response
|
I think there's a policy issue here around breaking behavior changes that needs clarification. It would seem that the original implementation rounded to milliseconds (though I can't fathom why), but that behavior was accidentally broken for |
FWIW - this is not critical for us. I just thought it was important to report it. |
Please look at #23771 (comment). We had an app compatibility issue when we tried to fix that. That is why we had the proposal #23799 to give better control. @mattjohnsonpint we needed feedback on it #23799 (comment) :-) I suggest closing this issue and focus more on the new proposal. |
that doesn't make sense to me Tarek. At the very least, the documentation needs to be updated so that it's correct for .NET Core 3.0+ |
What exactly doesn't make sense?
Do you suggest documenting the rounding behavior? I am fine updating the documentation as needed. We can log a doc issue or even if you prefer submitting PR, you are welcome to do so. |
* Set 60 second default Retry-After when not given This is to align with the SDK guidance at https://develop.sentry.dev/sdk/rate-limiting/#rate-limit-enforcement. * Refactor for readability * Update CHANGELOG.md * Use full double precision See dotnet/runtime#66815 * Cleanup
I was surprised to learn this. Went to review my code and found some places where this would definitely be undesirable, bordering on being a bug. I advocate for making this change. |
@mattjohnsonpint would it be crazy pants to propose removing all rounding from these, as part of a breaking change? At least there would be consistency, rather than the current mix of behavior (and also difference in behavior from the docs, which has been in .NET Core for too long to revert)
Did you by chance check if it was just the behavior of |
I believe the change done during 3.1 was 2503500. So, it is scoped to TimeSpan.FromXXX methods. @mattjohnsonpint, the only complaint in this issue is |
I tested some more. For clarity, I will restate my concerns:
My recommendation / ask is as follows:
I recognize that the change of behavior could be considered breaking, but we have already precedent for it with the |
I 100% agree with the reasoning and plan that @mattjohnsonpint laid out. Thank you Matt <3 |
Description
DateTimeOffset.AddSeconds(double)
andDateTime.AddSeconds(double)
both round the input value to the nearest millisecond, despite both structures being able to support precision to 7 decimal places. This is mentioned in the remarks section of the docs, but is surprising non-obvious behavior, IMHO.TimeSpan.FromSeconds(double)
also rounds the input to the nearest millisecond, and again is mentioned in the docs and is surprising, IMHO. However, it only does so on .NET Framework and .NET Core 2.1 and lower. From .Net Core 3.0 and higher (through to 6.0 latest), it does not round.Reproduction Steps
Expected behavior
Preferably, all tests should pass. Rounding should not occur in any scenario. However, at minimum, the behavior should be consistent across all supported implementations.
Actual behavior
AddTimeSpanFromSeconds
tests fail on .NET Framework (all versions) and .NET Core 2.1 and earlier, but pass on .NET Core 3.0 and higher (through 6.0 latest).AddSeconds
tests fail on all versions.Regression?
?
Known Workarounds
Compute ticks and add ticks, or create a timespan from those ticks.
Configuration
Tested on Windows x64 on .NET Core 2.1, 3.0, 3.1, .NET 5.0, 6.0 and .NET Framework 4.6.1, 4.6.2, 4.7.1, 4.7.2, and 4.8.
Also tested on macOS arm64 (Apple Silicon) on .NET 5.0 and 6.0, and .NET Core 3.1 with macOS x64 via Rosetta.
(I don't believe this is platform specific.)
Other information
No response
The text was updated successfully, but these errors were encountered: