Skip to content

Commit

Permalink
Add LocalTime.FromHoursSinceMidnight and FromMinutesSinceMidnight
Browse files Browse the repository at this point in the history
Fixes #1366

(FromHoursSinceMidnight is a little less useful, but I've included it for consistency/completeness.)
  • Loading branch information
jskeet committed May 4, 2019
1 parent 963a7a3 commit 3b6676f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/NodaTime.Test/LocalTimeTest.Construction.cs
Expand Up @@ -157,5 +157,33 @@ public void FromSecondsSinceMidnight_RangeChecks()
Assert.Throws<ArgumentOutOfRangeException>(() => LocalTime.FromSecondsSinceMidnight(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => LocalTime.FromSecondsSinceMidnight(NodaConstants.SecondsPerDay));
}

[Test]
public void FromMinutesSinceMidnight_Valid()
{
Assert.AreEqual(LocalTime.Midnight, LocalTime.FromMinutesSinceMidnight(0));
Assert.AreEqual(LocalTime.Midnight - Period.FromMinutes(1), LocalTime.FromMinutesSinceMidnight(NodaConstants.MinutesPerDay - 1));
}

[Test]
public void FromMinutesSinceMidnight_RangeChecks()
{
Assert.Throws<ArgumentOutOfRangeException>(() => LocalTime.FromMinutesSinceMidnight(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => LocalTime.FromMinutesSinceMidnight(NodaConstants.MinutesPerDay));
}

[Test]
public void FromHoursSinceMidnight_Valid()
{
Assert.AreEqual(LocalTime.Midnight, LocalTime.FromHoursSinceMidnight(0));
Assert.AreEqual(LocalTime.Midnight - Period.FromHours(1), LocalTime.FromHoursSinceMidnight(NodaConstants.HoursPerDay - 1));
}

[Test]
public void FromHoursSinceMidnight_RangeChecks()
{
Assert.Throws<ArgumentOutOfRangeException>(() => LocalTime.FromHoursSinceMidnight(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => LocalTime.FromHoursSinceMidnight(NodaConstants.HoursPerDay));
}
}
}
30 changes: 30 additions & 0 deletions src/NodaTime/LocalTime.cs
Expand Up @@ -302,6 +302,36 @@ public static LocalTime FromSecondsSinceMidnight(int seconds)
return new LocalTime(unchecked(seconds * NanosecondsPerSecond));
}

/// <summary>
/// Factory method for creating a local time from the number of minutes which have elapsed since midnight.
/// </summary>
/// <param name="minutes">The number of minutes, in the range [0, 1439]</param>
/// <returns>The resulting time.</returns>
public static LocalTime FromMinutesSinceMidnight(int minutes)
{
// Avoid the method calls which give a decent exception unless we're actually going to fail.
if (minutes < 0 || minutes > MinutesPerDay - 1)
{
Preconditions.CheckArgumentRange(nameof(minutes), minutes, 0, MinutesPerDay - 1);
}
return new LocalTime(unchecked(minutes * NanosecondsPerMinute));
}

/// <summary>
/// Factory method for creating a local time from the number of hours which have elapsed since midnight.
/// </summary>
/// <param name="hours">The number of hours, in the range [0, 23]</param>
/// <returns>The resulting time.</returns>
public static LocalTime FromHoursSinceMidnight(int hours)
{
// Avoid the method calls which give a decent exception unless we're actually going to fail.
if (hours < 0 || hours > HoursPerDay - 1)
{
Preconditions.CheckArgumentRange(nameof(hours), hours, 0, HoursPerDay - 1);
}
return new LocalTime(unchecked(hours * NanosecondsPerHour));
}

/// <summary>
/// Gets the hour of day of this local time, in the range 0 to 23 inclusive.
/// </summary>
Expand Down

0 comments on commit 3b6676f

Please sign in to comment.