-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Adding fractional milliseconds (microseconds) to a DateTime or TimeSpan not working #23771
Comments
The docs for AddMilliseconds state "The value parameter is rounded to the nearest integer", so this appears to be by design... though I don't know why that's the design. |
Maybe @tarekgh happens to know. I guess this can be closed by design, unless we think it would be an acceptable change to "fix" it. |
Right. this is the current design how we round the values. |
Would it maybe be reasonable to have a way to get/set the lower precision values? The fact that the only way to view the lower precision numbers is by using the ticks and making extension methods it a bit cumbersome. I came across this when writing some unit tests and from the logging and debugger, the two values I was comparing looked identical down to the millisecond, but they were different ticks so they were microseconds apart. Would be nice to have some additional methods. Seems like this could be done in a minor version since it would be adding API surface, not changing existing API's, and functionality would be unchanged. It would also be nice for ToString to include micro and nano seconds if they aren't zero, or at the very least, the debugger display. Thanks |
You already can do that. Please check the following link:
does the comparing of the formatted dates (with fffffff pattern) would help in this case? did you try it? |
Yes, we are doing that, it's just not ideal. See what I had to do in this PR to add support for fractional seconds to FluentAssertions: https://github.com/fluentassertions/fluentassertions/pull/669/files This file in particular has methods that to me seems like they should exist in .NET: |
@ChristopherHaws you can log a new issue with the proposal of the new APIs you want to add and we can discuss the details. (here is issue example you can follow to file yours https://github.com/dotnet/corefx/issues/24449) For ToString() I don't think we can change the default behavior for the app compatibility reason and we already supporting the patterns that can help show the micro, mini...seconds. so I guess there are no more requirements there. So, I guess what you want to propose is APIs allow adding or setting micro, nano..seconds. |
Thanks. I created an issue for that: https://github.com/dotnet/corefx/issues/24555 |
For those who does not have time to follow the link, this is the code that adds "support for fractional seconds". public static TimeSpan Seconds(this double seconds) |
DateTime.AddMilliseconds
andTimeSpan.FromMilliseconds
currently take aDouble
as the parameter, however when passing a fractional value either one, they ignore the numbers after the decimal place. To get around this, I had to create an extension methods to add microseconds manually. With the current API surface, there are only two ways to add microseconds; usingParse
or adding them directly to theTicks
.Versions Used:
.NET Framework 4.7
.NET Core 2.0
Steps to Reproduce:
Create a new console application with the following code:
Expected Behavior:
Values
a
,b
,c
,x
,y
, andz
should all have the sameTicks
.DateTime.AddMilliseconds(123.4567d)
should add 1234567 ticks to theDateTime
.TimeSpan.FromMilliseconds(123.4567d)
should add 1234567 ticks to theTimeSpan
.Actual Behavior:
DateTime.AddMilliseconds(123.4567d)
only adds 123 milliseconds to theDateTime
, ignoring the 4567 microseconds.TimeSpan.FromMilliseconds(123.4567d)
only adds 123 milliseconds to theTimeSpan
, ignoring the 4567 microseconds.The text was updated successfully, but these errors were encountered: