Skip to content
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

Closed
wants to merge 16 commits into from

Conversation

stefannikolei
Copy link
Contributor

@stefannikolei stefannikolei commented Dec 8, 2023

Fixes #93890

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Dec 8, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners new-api-needs-documentation labels Dec 8, 2023
Copy link

Note regarding the new-api-needs-documentation label:

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.

@huoyaoyuan huoyaoyuan added area-System.DateTime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 8, 2023
@ghost
Copy link

ghost commented Dec 8, 2023

Tagging subscribers to this area: @dotnet/area-system-datetime
See info in area-owners.md if you want to be subscribed.

Issue Details

null

Author: stefannikolei
Assignees: -
Labels:

new-api-needs-documentation, community-contribution, area-System.DateTime

Milestone: -

@tarekgh tarekgh added this to the 9.0.0 milestone Dec 8, 2023

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
Copy link
Member

@tarekgh tarekgh Dec 8, 2023

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.

CC @tannergooding

@tarekgh
Copy link
Member

tarekgh commented Dec 12, 2023

@stefannikolei did you have a chance to look and address my comment #95779 (review)?

@stefannikolei
Copy link
Contributor Author

@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

@tarekgh
Copy link
Member

tarekgh commented Dec 12, 2023

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.

@@ -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");
Copy link
Member

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?

Copy link
Member

@tarekgh tarekgh Jan 2, 2024

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.

Comment on lines +317 to +318
/// <exception cref="OverflowException">
/// </exception>
Copy link
Member

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

Comment on lines +319 to +327
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);
}
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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);
Copy link
Member

@tarekgh tarekgh Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

days * MicrosecondsPerDay

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.)

CC @tannergooding

Copy link
Member

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

Copy link
Member

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.

@tarekgh
Copy link
Member

tarekgh commented Jan 12, 2024

@stefannikolei checking if you'll have a chance to address the feedback?

@tarekgh tarekgh added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 12, 2024
@ghost ghost added the no-recent-activity label Jan 27, 2024
@ghost
Copy link

ghost commented Jan 27, 2024

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@ghost
Copy link

ghost commented Feb 10, 2024

This pull request will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the pull request, but please note that it will be locked if it remains inactive for another 30 days.

@ghost ghost closed this Feb 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 12, 2024
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.DateTime community-contribution Indicates that the PR has been added by a community member needs-author-action An issue or pull request that requires more info or actions from the author. new-api-needs-documentation no-recent-activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New TimeSpan.From overloads which take integers.
6 participants