30 lines
861 B
C#
30 lines
861 B
C#
|
using System;
|
|||
|
|
|||
|
namespace Mk0.Tools.TimeTools
|
|||
|
{
|
|||
|
public static class TimeTools
|
|||
|
{
|
|||
|
public static DateTime RoundUp(this DateTime dt, TimeSpan d)
|
|||
|
{
|
|||
|
var modTicks = dt.Ticks % d.Ticks;
|
|||
|
var delta = modTicks != 0 ? d.Ticks - modTicks : 0;
|
|||
|
return new DateTime(dt.Ticks + delta, dt.Kind);
|
|||
|
}
|
|||
|
|
|||
|
public static DateTime RoundDown(this DateTime dt, TimeSpan d)
|
|||
|
{
|
|||
|
var delta = dt.Ticks % d.Ticks;
|
|||
|
return new DateTime(dt.Ticks - delta, dt.Kind);
|
|||
|
}
|
|||
|
|
|||
|
public static DateTime RoundToNearest(this DateTime dt, TimeSpan d)
|
|||
|
{
|
|||
|
var delta = dt.Ticks % d.Ticks;
|
|||
|
bool roundUp = delta > d.Ticks / 2;
|
|||
|
var offset = roundUp ? d.Ticks : 0;
|
|||
|
|
|||
|
return new DateTime(dt.Ticks + offset - delta, dt.Kind);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|