Skip to content

Add TimeSpan.FromMilliseconds(long) overload #110234

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

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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().
//
Expand Down Expand Up @@ -585,6 +585,18 @@ public static TimeSpan FromSeconds(long seconds, long milliseconds = 0, long mic
return FromMicroseconds(totalMicroseconds);
}

/// <summary>
/// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of
/// milliseconds.
/// </summary>
/// <param name="milliseconds">Number of milliseconds.</param>
/// <returns>Returns a <see cref="TimeSpan"/> that represents a specified number of milliseconds.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// The parameter specify a <see cref="TimeSpan"/> value less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/>
/// </exception>
public static TimeSpan FromMilliseconds(long milliseconds)
=> FromUnits(milliseconds, TicksPerMillisecond, MinMilliseconds, MaxMilliseconds);

/// <summary>
/// Initializes a new instance of the <see cref="TimeSpan"/> structure to a specified number of
/// milliseconds, and microseconds.
Expand All @@ -595,7 +607,7 @@ public static TimeSpan FromSeconds(long seconds, long milliseconds = 0, long mic
/// <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)
public static TimeSpan FromMilliseconds(long milliseconds, long microseconds)
{
Int128 totalMicroseconds = Math.BigMul(milliseconds, MicrosecondsPerMillisecond)
+ microseconds;
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using System.Text;
using Xunit;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<Action> a = () => TimeSpan.FromMilliseconds(milliseconds);
Expression<Action> b = () => TimeSpan.FromMilliseconds(milliseconds, microseconds);
Expression<Action> c = () => TimeSpan.FromMilliseconds((double)milliseconds);
}

[Theory]
[InlineData(maxMilliseconds + 1, 0)]
[InlineData(-(maxMilliseconds + 1), 0)]
Expand All @@ -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<ArgumentOutOfRangeException>(() => TimeSpan.FromMilliseconds(milliseconds, microseconds));

if (microseconds == 0)
{
Assert.Throws<ArgumentOutOfRangeException>(() => TimeSpan.FromMilliseconds(milliseconds));
}
}

[Theory]
Expand Down
Loading