-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New TimeSpan.From overloads which take integers #98633
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
Changes from 11 commits
261158e
45aaffe
c526512
6ec77b0
c3c6be7
f3b5124
223e918
daafe23
2bf71ee
81d98f5
4f43bc6
764d849
b48ba23
f3c16cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -308,6 +308,149 @@ public TimeSpan Duration() | |
|
|
||
| public override int GetHashCode() => _ticks.GetHashCode(); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// days. | ||
| /// </summary> | ||
| /// <param name="days">Number of days.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of days.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromDays(int days) => FromDays(days, 0); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// days, hours, minutes, seconds, milliseconds, and microseconds. | ||
| /// </summary> | ||
| /// <param name="days">Number of days.</param> | ||
| /// <param name="hours">Number of hours.</param> | ||
| /// <param name="minutes">Number of minutes.</param> | ||
| /// <param name="seconds">Number of seconds.</param> | ||
| /// <param name="milliseconds">Number of milliseconds.</param> | ||
| /// <param name="microseconds">Number of microseconds.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of days, hours, minutes, seconds, milliseconds, and microseconds.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromDays(int days, int hours = 0, long minutes = 0, long seconds = 0, long milliseconds = 0, long microseconds = 0) | ||
| { | ||
| Int128 totalMicroseconds = Math.BigMul(days, MicrosecondsPerDay) | ||
| + Math.BigMul(hours, MicrosecondsPerHour) | ||
| + Math.BigMul(minutes, MicrosecondsPerMinute) | ||
| + Math.BigMul(seconds, MicrosecondsPerSecond) | ||
| + Math.BigMul(milliseconds, MicrosecondsPerMillisecond) | ||
| + microseconds; | ||
|
|
||
| if ((totalMicroseconds > MaxMicroseconds) || (totalMicroseconds < MinMicroseconds)) | ||
|
||
| { | ||
| ThrowHelper.ThrowArgumentOutOfRange_TimeSpanTooLong(); | ||
| } | ||
| var ticks = (long)totalMicroseconds * TicksPerMicrosecond; | ||
| return TimeSpan.FromTicks(ticks); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// hours. | ||
| /// </summary> | ||
| /// <param name="hours">Number of hours.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of hours.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromHours(int hours) => FromDays(0, hours: hours); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// hours, minutes, seconds, milliseconds, and microseconds. | ||
| /// </summary> | ||
| /// <param name="hours">Number of hours.</param> | ||
| /// <param name="minutes">Number of minutes.</param> | ||
| /// <param name="seconds">Number of seconds.</param> | ||
| /// <param name="milliseconds">Number of milliseconds.</param> | ||
| /// <param name="microseconds">Number of microseconds.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of hours, minutes, seconds, milliseconds, and microseconds.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromHours(int hours, long minutes = 0, long seconds = 0, long milliseconds = 0, long microseconds = 0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same general note for these ones. We have a few lines of code to replicate that would allow completely skipping more expensive parts of the It's a bit more code, but given these are in part there for perf and accuracy, I personally think that's fine. It might be different if we could constant fold |
||
| => FromDays(0, hours: hours, minutes: minutes, seconds: seconds, milliseconds: milliseconds, microseconds: microseconds); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// minutes. | ||
| /// </summary> | ||
| /// <param name="minutes">Number of minutes.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of minutes.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromMinutes(long minutes) => FromDays(0, minutes: minutes); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// minutes, seconds, milliseconds, and microseconds. | ||
| /// </summary> | ||
| /// <param name="minutes">Number of minutes.</param> | ||
| /// <param name="seconds">Number of seconds.</param> | ||
| /// <param name="milliseconds">Number of milliseconds.</param> | ||
| /// <param name="microseconds">Number of microseconds.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of minutes, seconds, milliseconds, and microseconds.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromMinutes(long minutes, long seconds = 0, long milliseconds = 0, long microseconds = 0) | ||
| => FromDays(0, minutes: minutes, seconds: seconds, milliseconds: milliseconds, microseconds: microseconds); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// seconds. | ||
| /// </summary> | ||
| /// <param name="seconds">Number of seconds.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of seconds.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromSeconds(long seconds) => FromDays(0, seconds: seconds); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// seconds, milliseconds, and microseconds. | ||
| /// </summary> | ||
| /// <param name="seconds">Number of seconds.</param> | ||
| /// <param name="milliseconds">Number of milliseconds.</param> | ||
| /// <param name="microseconds">Number of microseconds.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of seconds, milliseconds, and microseconds.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromSeconds(long seconds, long milliseconds = 0, long microseconds = 0) | ||
| => FromDays(0, seconds: seconds, milliseconds: milliseconds, microseconds: microseconds); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// milliseconds, and microseconds. | ||
| /// </summary> | ||
| /// <param name="milliseconds">Number of milliseconds.</param> | ||
| /// <param name="microseconds">Number of microseconds.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of milliseconds, and microseconds.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromMilliseconds(long milliseconds, long microseconds = 0) => FromDays(0, milliseconds: milliseconds, microseconds: microseconds); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of | ||
| /// microseconds. | ||
| /// </summary> | ||
| /// <param name="microseconds">Number of microseconds.</param> | ||
| /// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of microseconds.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The parameters specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/> | ||
| /// </exception> | ||
| public static TimeSpan FromMicroseconds(long microseconds) => FromDays(0, microseconds: microseconds); | ||
|
|
||
| public static TimeSpan FromHours(double value) => Interval(value, TicksPerHour); | ||
|
|
||
| private static TimeSpan Interval(double value, double scale) | ||
|
|
||
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.
It might be worth optimizing these ones (that take a single arg) directly rather than deferring them down. There's quite a bit of code that can be skipped and they could all use a single shared helper that looks like:
We could also just replicate the basically 3 lines of code in each case, it's not a lot. No preference on my end.
Note that I don't think it needs to be done in this PR, but its certainly easier to get in if we did it here.