-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Timespan int overloads #95779
Timespan int overloads #95779
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/area-system-datetime Issue Detailsnull
|
|
||
public static TimeSpan FromDays(int days, int hours = 0, long minutes = 0, long seconds = 0, long milliseconds = 0, long microseconds = 0) | ||
{ | ||
long totalTicks = (long)days * TicksPerDay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to ensure handling the overflow. Please have a look at the discussion #94143 (review) for more info. This applies to all APIs implementation here.
@stefannikolei did you have a chance to look and address my comment #95779 (review)? |
I thought I did. It perhaps can be that I misunderstood that discussion |
Can you try to call the newly introduced APIs with values that mathematically overflow? The expectation is to get overflow exception. |
* Added new method IntervalFromTicks equivalent to IntervalFromDoubleTicks
…, int seconds, int milliseconds, int microseconds)
@@ -310,13 +310,13 @@ public static void Call() | |||
{ | |||
ParameterExpression x = Expression.Parameter(typeof(int), "x"); | |||
ParameterExpression y = Expression.Parameter(typeof(int), "y"); | |||
ParameterExpression d = Expression.Parameter(typeof(double), "d"); | |||
ParameterExpression l = Expression.Parameter(typeof(long), "l"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this test changing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because typeof(TimeSpan).GetMethod("FromSeconds", new[] { typeof(int) })
used to pick the overload which takes double
but now with adding the new overloads, it will pick the one that takes long
.
/// <exception cref="OverflowException"> | ||
/// </exception> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of these exception blocks are empty
public static TimeSpan FromDays(int days, int hours = 0, long minutes = 0, long seconds = 0, long milliseconds = 0, long microseconds = 0) | ||
{ | ||
return FromDays(days) | ||
+ FromHours(hours) | ||
+ FromMinutes(minutes) | ||
+ FromSeconds(seconds) | ||
+ FromMilliseconds(milliseconds) | ||
+ FromMicroseconds(microseconds); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if e.g. someone passes a days value that would overflow but then a negative hours value that would bring it back into the valid range? With this implementation that will still throw... is that the desired behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent question. We may implement this inside checked
block and do the math manually by converting all units to ticks and adding them. I know we'll sacrifice some perf, but we'll have the correct behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I think that we should only throw if the final tick count is out of range.
It's always annoying when x + y + z
fails but x + z + y
would succeed and I think it provides a better experience if we treat FromDays
as its own higher order thing.
/// </exception> | ||
public static TimeSpan FromDays(int days) | ||
{ | ||
return FromMicroseconds(days * MicrosecondsPerDay); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This multiplication by itself can overflow and make the operation succeed without exception, I guess.
For example, using 1067519900
days will result -1008547758080
microseconds which is going to be considered as a valid value while it is really overflowed.
The best thing that can be done here is to check the day's value and directly convert the result to ticks if it is valid.
This comment applies to similar calling of From
operations with other units (hours, minutes...etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we validate that it's less than MaxDays
(10_675_199
)? 1_067_519_900
is much larger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is what I was suggesting when I said the best thing that can be done here is to check the day's value and directly convert the result to ticks if it is valid.
@stefannikolei checking if you'll have a chance to address the feedback? |
This pull request has been automatically marked |
This pull request will now be closed since it had been marked |
Fixes #93890