1

我想处理时间块,这意味着一组两个日期时间,例如代表员工的存在。是否已经有任何结构可用于在特定时间之前或之后搜索块?我可以想象有很多方法来表达这种情况,就像我说的用两个日期时间作为开始和结束,或者用一个日期时间作为开始和一个时间跨度。但我希望它们以一种集合的方式处理。那么有什么类似的东西我可以使用还是我必须自己完全实现它?

谢谢

4

6 回答 6

2

这个图书馆很棒 - 愿你受到启发

.NET 的时间段库

于 2012-08-24T12:28:10.540 回答
1

班上:

public class TimePeriod
{
    public DateTime Oldest { get; set; }
    public DateTime Newest { get; set; }

    public TimePeriod(DateTime oldest, DateTime newest)
    {
        Oldest = oldest;
        Newest = newest;
    }

    public bool Contains (DateTime time)
    {
        return Oldest.CompareTo(time) <= 0 && Newest.CompareTo(time) >= 0;
    }

    public bool IsAfter(DateTime time)
    {
        return Newest.CompareTo(time) <= 0;
    }

    public bool IsBefore(DateTime time)
    {
        return Oldest.CompareTo(time) >= 0; 
    }
}

考试:

class Program
{
    static void Main(string[] args)
    {
        var period = new TimePeriod(
                    DateTime.Now.AddDays(-2),
                    DateTime.Now.AddDays(1));

        var date = DateTime.Now;
        var contains = period.Contains(date); // true
        var isBefore = period.IsBefore(date); // false
        var isAfter = period.IsAfter(date);   // false

        date = DateTime.Now.AddDays(-10);
        contains = period.Contains(date); // false
        isBefore = period.IsBefore(date); // true
        isAfter = period.IsAfter(date);   // false

        date = DateTime.Now.AddDays(10);
        contains = period.Contains(date); // false
        isBefore = period.IsBefore(date); // false
        isAfter = period.IsAfter(date);   // true
    }
}

现在您可以使用带有扩展方法和 lambda 表达式的集合和 linq 来查找时间块。

于 2012-08-24T12:36:06.483 回答
0

这不是内置的。如果你想自己实现这个,你可能想创建一个struct。这将为您提供值类型的复制语义。这样的值的行为就像内置类型一样intor DateTime。使用起来非常直观。

于 2012-08-24T12:16:27.000 回答
0

你可以看看 TimeSpan。那是处理“时间块”的结构

于 2012-08-24T12:30:57.823 回答
0

我以前使用过一个DateSpan结构。您可以随心所欲地扩展,但这将为您提供一个起点。

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace StackOverFlowDateSpan
{
[StructLayout(LayoutKind.Auto)]
[Serializable]
public struct DateSpan : IComparable, IComparable<DateSpan>, IEquatable<DateSpan>
{
    public DateSpan(DateTime start, DateTime end)
        : this()
    {
        Start = start;
        End = end;
    }

    #region Properties

    public TimeSpan Duration
    {
        get { return TimeSpan.FromTicks((End - Start).Ticks); }
    }

    public DateTime End { get; private set; }
    public DateTime Start { get; private set; }

    #endregion

    public int CompareTo(DateSpan other)
    {
        long otherTicks = other.Duration.Ticks;
        long internalTicks = Duration.Ticks;

        return internalTicks > otherTicks ? 1 : (internalTicks < otherTicks ? -1 : 0);
    }

    public bool Equals(DateSpan other)
    {
        return End.Equals(other.End) && Start.Equals(other.Start);
    }

    public int CompareTo(object other)
    {
        if (other == null)
        {
            return 1;
        }

        if (!(other is DateSpan))
        {
            throw new ArgumentNullException("other");
        }

        return CompareTo((DateSpan)other);
    }

    public override bool Equals(object other)
    {
        if (ReferenceEquals(null, other))
        {
            return false;
        }
        return other is DateSpan && Equals((DateSpan)other);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return (End.GetHashCode() * 397) ^ Start.GetHashCode();
        }
    }

    public static bool operator ==(DateSpan left, DateSpan right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(DateSpan left, DateSpan right)
    {
        return !left.Equals(right);
    }

    private sealed class EndStartEqualityComparer : IEqualityComparer<DateSpan>
    {
        #region IEqualityComparer<DateSpan> Members

        public bool Equals(DateSpan x, DateSpan y)
        {
            return x.End.Equals(y.End) && x.Start.Equals(y.Start);
        }

        public int GetHashCode(DateSpan obj)
        {
            unchecked
            {
                return (obj.End.GetHashCode() * 397) ^ obj.Start.GetHashCode();
            }
        }

        #endregion
    }

    private static readonly IEqualityComparer<DateSpan> _endStartComparerInstance = new EndStartEqualityComparer();

    public static IEqualityComparer<DateSpan> EndStartComparer
    {
        get { return _endStartComparerInstance; }
    }
}
}
于 2012-08-24T12:35:12.133 回答
0

谢谢您的帮助!我将仔细研究 TimePeriod 库并使用 Linq 做一些实验。我已经有一个实现二进制搜索的方法,所以如果有人感兴趣,你可以写信给我;)

于 2012-08-25T11:29:30.253 回答