区分上午 8 点至上午 9 点和晚上 8 点至晚上 9 点之间的时间跨度等

Differentiate time spans between 8am-9am and 8pm-9pm, etc

我正在寻找一种方法来区分时间跨度,例如,在 .NET 中,上午 8 点到晚上 9 点和晚上 8 点到晚上 9 点、上午 10 点到中午 12 点和晚上 10 点到中午 12 点等。

由于时间是相对的,我正在寻找一种创造性的方法来实现这一点。

这是我目前尝试过的方法:

// 3:41pm - 4:41pm

DateStart: 5/7/2015 3:41:41 PM
DateExpire: 5/7/2015 4:41:41 PM

DateExpire.Subtract(DateStart): 01:00:00
ToUnixEpocTime(DateStart): 1431013301
ToUnixEpocTime(DateExpire): 1431016901
int timespan = ToUnixEpocTime(DateExpire) - ToUnixEpocTime(DateStart);
timespan: 3600


// 4:41pm - 5:41pm
DateStart: 5/7/2015 4:41:41 PM
DateExpire: 5/7/2015 5:41:41 PM
DateExpire.Subtract(DateStart): 01:00:00
ToUnixEpocTime(DateStart): 1431016901
ToUnixEpocTime(DateExpire): 1431020501
int timespan = ToUnixEpocTime(DateExpire) - ToUnixEpocTime(DateStart);
timespan: 3600

...我不确定为什么这些结果让我感到惊讶,因为它们是有道理的——因为它只是基本的减法。

如前所述,TimeSpan 是时间间隔。上午 8 点至上午 9 点和晚上 8 点至晚上 9 点的时间间隔完全相同 - 均为 1 小时。如果你想区分它们,然后创建你自己的 class 它将保存每个间隔的开始和结束时间并使用这些值来比较对象:

public class DateRange
{
    public DateRange(DateTime start, DateTime end)
    {
        if (end < start)
           throw new ArgumentException("end");

        Start = start;
        End = end;
    }

    public DateTime Start { get; private set; }
    public DateTime End { get; private set; }
    public TimeSpan Duration { get { return End - Start; }}

    public override bool Equals(object obj)
    {
        DateRange other = obj as DateRange;
        if (other == null)
           return false;

        return Start == other.Start && End == other.End;
    }
    // override GetHashCode
}

现在,如果您有两个日期范围

var morningRange = new DateRange(8amTime, 9amTime);
var eveningRange = new DateRange(8pmTime, 9pmTime);

它们不会相同,但持续时间相同

morningRange.Duration == eveningRange.Duration // true
morningRange.Equals(eveningRange) // false

您还可以定义 ==!= 运算符

public static bool operator== (DateRange x, DateRange y)
{
    if (Object.ReferenceEquals(x, y))
        return true;

    if (((object)x == null) || ((object)y == null))
        return false;

    return x.Equals(y);
}

public static bool operator !=(DateRange x, DateRange y)
{
    return !(x == y);
}

现在比较更容易了

morningRange == eveningRange // false
morningRange != eveningRange // true

我的解决方案:

public class TimeRange
{
    private double _begin;              // total ms from 00:00
    private double _end;                // total ms from 00:00

    /// <param name="begin"> begin time (for example 8:30:15am) </param>
    /// <param name="end"> begin time (for example 12pm) </param>
    public TimeRange(DateTime begin, DateTime end)
    {
        _begin = begin.TimeOfDay.TotalMilliseconds;
        _end = end.TimeOfDay.TotalMilliseconds;
    }

    /// <summary>
    /// check time in range
    /// </summary>
    /// <param name="time"> time </param>
    /// <returns></returns>
    public bool IsDateInRange(DateTime time)
    {
        double value = time.TimeOfDay.TotalMilliseconds;
        return _end > _begin ? (value >= _begin && value <= _end) : (value >= _end && value <= _begin);
    }

    public override string ToString()
    {
        DateTime today = DateTime.Today;
        return string.Format(_end > _begin ? "{0}-{1}" : "{1}-{0}",
            (today + TimeSpan.FromMilliseconds(_begin)).ToString("HH:mm"),
            (today + TimeSpan.FromMilliseconds(_end)).ToString("HH:mm"));
    }
}

用法:

TimeRange range = new TimeRange(DateTime.Today, DateTime.Now);
Console.WriteLine("Range is {0}", range.ToString());
for (int i = 0; i < 24; i++)
{
    Console.WriteLine("Time {0:00}:00 is in range? {1}", i, range.IsDateInRange(DateTime.Today.AddHours(i)));
}

输出:

Range is 00:00-17:45
Time 00:00 is in range? True
Time 01:00 is in range? True
Time 02:00 is in range? True
Time 03:00 is in range? True
Time 04:00 is in range? True
Time 05:00 is in range? True
Time 06:00 is in range? True
Time 07:00 is in range? True
Time 08:00 is in range? True
Time 09:00 is in range? True
Time 10:00 is in range? True
Time 11:00 is in range? True
Time 12:00 is in range? True
Time 13:00 is in range? True
Time 14:00 is in range? True
Time 15:00 is in range? True
Time 16:00 is in range? True
Time 17:00 is in range? True
Time 18:00 is in range? False
Time 19:00 is in range? False
Time 20:00 is in range? False
Time 21:00 is in range? False
Time 22:00 is in range? False
Time 23:00 is in range? False

只是提供一个想法

如果两者都是上午或下午那么
加上时差12
否则

例如
// 3:41pm - 4:41pm
开始日期:5/7/2015 3:41:41 下午
到期日期:5/7/2015 4:41:41 下午

if(开始日期 = 下午和结束日期 = 下午)
timeSpan = (DateStart - DateExpire) + 12
否则
timeSpan = (DateStart - DateExpire)