using System;

namespace InABox.Core
{
    
    public static class TimeExtensions
    {
        public static TimeSpan Floor(this TimeSpan timespan, TimeSpan interval)
        {
            return timespan.Subtract(new TimeSpan(timespan.Ticks % interval.Ticks));
        }
        
        public static TimeSpan Ceiling(this TimeSpan timespan, TimeSpan interval)
        {
            var overflow = timespan.Ticks % interval.Ticks;

            return overflow == 0 ? timespan : timespan.Add(new TimeSpan(interval.Ticks - overflow));
        }
        
        public static TimeSpan Round(this TimeSpan timespan, TimeSpan interval)
        {
            var halfIntervalTicks = (interval.Ticks + 1) >> 1;

            return timespan.Add(new TimeSpan(halfIntervalTicks - (timespan.Ticks + halfIntervalTicks) % interval.Ticks));
        }
    }

}