From 900f88209792e17f290b0b559b6d4623786aa5f7 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Wed, 27 Nov 2024 11:19:32 -0800 Subject: [PATCH] Add TimeSpan.FromMilliseconds(long) overload --- .../src/System/TimeSpan.cs | 16 ++++++++++++++-- .../System.Runtime/ref/System.Runtime.cs | 3 ++- .../System/TimeSpanTests.cs | 17 ++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs b/src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs index 1abb6be7c3bc68..e249332ecd106a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs +++ b/src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs @@ -15,7 +15,7 @@ namespace System // to 100 nanoseconds. While this maps well into units of time such as hours // and days, any periods longer than that aren't representable in a nice fashion. // For instance, a month can be between 28 and 31 days, while a year - // can contain 365 or 366 days. A decade can have between 1 and 3 leapyears, + // can contain 365 or 366 days. A decade can have between 1 and 3 leap-years, // depending on when you map the TimeSpan into the calendar. This is why // we do not provide Years() or Months(). // @@ -585,6 +585,18 @@ public static TimeSpan FromSeconds(long seconds, long milliseconds = 0, long mic return FromMicroseconds(totalMicroseconds); } + /// + /// Initializes a new instance of the structure to a specified number of + /// milliseconds. + /// + /// Number of milliseconds. + /// Returns a that represents a specified number of milliseconds. + /// + /// The parameter specify a value less than or greater than + /// + public static TimeSpan FromMilliseconds(long milliseconds) + => FromUnits(milliseconds, TicksPerMillisecond, MinMilliseconds, MaxMilliseconds); + /// /// Initializes a new instance of the structure to a specified number of /// milliseconds, and microseconds. @@ -595,7 +607,7 @@ public static TimeSpan FromSeconds(long seconds, long milliseconds = 0, long mic /// /// The parameters specify a value less than or greater than /// - public static TimeSpan FromMilliseconds(long milliseconds, long microseconds = 0) + public static TimeSpan FromMilliseconds(long milliseconds, long microseconds) { Int128 totalMicroseconds = Math.BigMul(milliseconds, MicrosecondsPerMillisecond) + microseconds; diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 46e4c9fc75df30..31c0c2a2f77783 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -6014,7 +6014,8 @@ protected TimeProvider() { } public static System.TimeSpan FromMicroseconds(double value) { throw null; } public static System.TimeSpan FromMicroseconds(long microseconds) { throw null; } public static System.TimeSpan FromMilliseconds(double value) { throw null; } - public static System.TimeSpan FromMilliseconds(long milliseconds, long microseconds = (long)0) { throw null; } + public static System.TimeSpan FromMilliseconds(long milliseconds) { throw null; } + public static System.TimeSpan FromMilliseconds(long milliseconds, long microseconds) { throw null; } public static System.TimeSpan FromMinutes(double value) { throw null; } public static System.TimeSpan FromMinutes(long minutes) { throw null; } public static System.TimeSpan FromMinutes(long minutes, long seconds = (long)0, long milliseconds = (long)0, long microseconds = (long)0) { throw null; } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeSpanTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeSpanTests.cs index 9085d957fa7e63..0de8d33c4d3565 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeSpanTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeSpanTests.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Linq.Expressions; using System.Text; using Xunit; @@ -631,7 +632,7 @@ public static void FromMinutes_Int_ShouldCreate(long minutes, long seconds, long [InlineData(0, -(maxSeconds + 1), 0, 0)] [InlineData(0, 0, maxMilliseconds + 1, 0)] [InlineData(0, 0, -(maxMilliseconds + 1), 0)] - [InlineData(0, 0, 0, maxMicroseconds + 1)] + [InlineData(0, 0, 0, maxMicroseconds + 1)] [InlineData(0, 0, 0, -(maxMicroseconds + 1))] public static void FromMinutes_Int_ShouldOverflow(long minutes, long seconds, long milliseconds, long microseconds) { @@ -713,7 +714,16 @@ public static void FromMilliseconds_Int_ShouldCreate(long milliseconds, long mic long ticksFromMicroseconds = microseconds * TimeSpan.TicksPerMicrosecond; var expected = TimeSpan.FromTicks(ticksFromMilliseconds + ticksFromMicroseconds); Assert.Equal(expected, TimeSpan.FromMilliseconds(milliseconds, microseconds)); + + expected = TimeSpan.FromTicks(ticksFromMilliseconds); + Assert.Equal(expected, TimeSpan.FromMilliseconds(milliseconds)); + + // The following exist to ensure compilation of the expressions + Expression a = () => TimeSpan.FromMilliseconds(milliseconds); + Expression b = () => TimeSpan.FromMilliseconds(milliseconds, microseconds); + Expression c = () => TimeSpan.FromMilliseconds((double)milliseconds); } + [Theory] [InlineData(maxMilliseconds + 1, 0)] [InlineData(-(maxMilliseconds + 1), 0)] @@ -730,6 +740,11 @@ public static void FromMilliseconds_Int_ShouldCreate(long milliseconds, long mic public static void FromMilliseconds_Int_ShouldOverflow(long milliseconds, long microseconds) { Assert.Throws(() => TimeSpan.FromMilliseconds(milliseconds, microseconds)); + + if (microseconds == 0) + { + Assert.Throws(() => TimeSpan.FromMilliseconds(milliseconds)); + } } [Theory]