C#检查时间范围是否在时间范围和多少小时之间

假设我有3个时间范围:

07:00 - 18:00  
18:00 - 23:00  
23:00 - 07:00  

和代码:

public class TimeShift
{
   public TimeSpan Start { get; set; }
   public TimeSpan End { get; set; }
}

List<TimeShift> shifts = new List<TimeShift>();

如何检查列表中的每个项目是否在上述3个范围之间以及几个小时之间?

例如一个TimeShift,其中:

Start: 07:00
End:   23:30

那意味着16.5个小时.

对于以上示例:

Range 1: 11 hours  
Range 2: 5 hours  
Range 3: 0.5 hours  

解决方法:

这是一个包含测试的解决方案:

计算

public class TimeSpacCalculator
{
    public static TimeSpan GetTimeSpanIntersect(TimeShift input, TimeSpan start, TimeSpan end)
    {
        // Loopsback input from 23:59 - 00:00
        if (input.Start > input.End)
            return GetTimeSpanIntersect(new TimeShift(input.Start, TimeSpan.FromHours(24)), start, end) +
                   GetTimeSpanIntersect(new TimeShift(TimeSpan.FromHours(0), input.End), start, end);

        // Loopsback Shift from 23:59 - 00:00
        if (start > end)
            return GetTimeSpanIntersect(input, new TimeSpan(), end) +
                   GetTimeSpanIntersect(input, start, TimeSpan.FromHours(24));
        if (input.End < start)
            return new TimeSpan();

        if (input.Start > end)
            return new TimeSpan();

        var actualStart = input.Start < start
            ? start
            : input.Start;

        var actualEnd = input.End > end
            ? end
            : input.End;

        return actualEnd - actualStart;
    }
}

班级

public class TimeRange : TimeShift
{
    public TimeRange(string name, TimeSpan start, TimeSpan end) : base(start, end)
    {
        Name = name;
    }

    public string Name { get; set; }
}

public class TimeShift
{
    public TimeShift(TimeSpan start, TimeSpan end)
    {
        Start = start;
        End = end;
    }

    public TimeSpan Start { get; set; }
    public TimeSpan End { get; set; }
}

测验

[TestFixture]
internal class TimShiftTests
{
    [Test]
    [TestCase(7, 23.5, 11, 5, 0.5)]
    [TestCase(22, 7.5, 0.5, 1, 8)]
    public void Test(double inputStartHours, double inputEndHours, double expectedRange1Hours, double expectedRange2Hours, double expectedRange3Hours )
    {
        var input = new TimeShift(TimeSpan.FromHours(inputStartHours), TimeSpan.FromHours(inputEndHours));

        var ranges = new List<TimeRange>
        {
            new TimeRange("Range1", TimeSpan.FromHours(7), TimeSpan.FromHours(18)),
            new TimeRange("Range2", TimeSpan.FromHours(18), TimeSpan.FromHours(23)),
            new TimeRange("Range3", TimeSpan.FromHours(23), TimeSpan.FromHours(7))
        };


        var result = new Dictionary<string, TimeSpan>();

        foreach (var range in ranges)
        {
            var time = TimeSpacCalculator.GetTimeSpanIntersect(input, range.Start, range.End);

            result.Add(range.Name, time);

            Console.WriteLine($"{range.Name}: " + time.TotalHours);
        }

        result["Range1"].Should().Be(TimeSpan.FromHours(expectedRange1Hours));
        result["Range2"].Should().Be(TimeSpan.FromHours(expectedRange2Hours));
        result["Range3"].Should().Be(TimeSpan.FromHours(expectedRange3Hours));
}
上一篇:C#中的本地化字符串的时间跨度


下一篇:C# -- DateTime与TimeSpan