Skip to content

Commit

Permalink
feat: TimeSpan - rounding to interval
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikostiha committed Nov 20, 2024
1 parent 90f5b81 commit 910df1d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Atin.Tests/TimeSpanExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,37 @@ public void ToAtin_ForTwoDigits()

Assert.Equal("M10", result);
}

[Fact]
public void RoundTo_M5()
{
var subInterval = TimeSpan.FromMinutes(5);
var totalInterval = new TimeSpan(5, 12, 45);

var result = totalInterval.RoundTo(subInterval);

Assert.Equal(new TimeSpan(5, 15, 00), result);
}

[Fact]
public void RoundDown_M5()
{
var subInterval = TimeSpan.FromMinutes(5);
var totalInterval = new TimeSpan(5, 12, 45);

var result = totalInterval.RoundDown(subInterval);

Assert.Equal(new TimeSpan(5, 10, 00), result);
}

[Fact]
public void RoundUp_M5()
{
var subInterval = TimeSpan.FromMinutes(5);
var totalInterval = new TimeSpan(5, 12, 45);

var result = totalInterval.RoundUp(subInterval);

Assert.Equal(new TimeSpan(5, 15, 00), result);
}
}
36 changes: 36 additions & 0 deletions src/Atin/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,40 @@ public static string ToAtin(this TimeSpan timeSpan)

return result.ToString();
}

/// <summary>
/// Rounds the <see cref="TimeSpan"/> value to the nearest interval.
/// </summary>
/// <param name="timeSpan">The original <see cref="TimeSpan"/> to round.</param>
/// <param name="subInterval">The interval to which the value should be rounded.</param>
/// <returns>The rounded <see cref="TimeSpan"/>.</returns>
public static TimeSpan RoundTo(this TimeSpan timeSpan, TimeSpan subInterval)
{
long ticks = (timeSpan.Ticks + subInterval.Ticks / 2) / subInterval.Ticks * subInterval.Ticks;
return TimeSpan.FromTicks(ticks);
}

/// <summary>
/// Rounds the <see cref="TimeSpan"/> value down to the nearest interval.
/// </summary>
/// <param name="timeSpan">The original <see cref="TimeSpan"/> to round.</param>
/// <param name="subInterval">The interval to which the value should be rounded down.</param>
/// <returns>The rounded-down <see cref="TimeSpan"/>.</returns>
public static TimeSpan RoundDown(this TimeSpan timeSpan, TimeSpan subInterval)
{
long ticks = timeSpan.Ticks / subInterval.Ticks * subInterval.Ticks;
return TimeSpan.FromTicks(ticks);
}

/// <summary>
/// Rounds the <see cref="TimeSpan"/> value up to the nearest interval.
/// </summary>
/// <param name="timeSpan">The original <see cref="TimeSpan"/> to round.</param>
/// <param name="subInterval">The interval to which the value should be rounded up.</param>
/// <returns>The rounded-up <see cref="TimeSpan"/>.</returns>
public static TimeSpan RoundUp(this TimeSpan timeSpan, TimeSpan subInterval)
{
long ticks = (timeSpan.Ticks + subInterval.Ticks - 1) / subInterval.Ticks * subInterval.Ticks;
return TimeSpan.FromTicks(ticks);
}
}

0 comments on commit 910df1d

Please sign in to comment.