C#有没有办法让时间范围名单?配置 [英] C# Is there a way to make a list of time range? Configurable

查看:101
本文介绍了C#有没有办法让时间范围名单?配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,使时间段的一个列表? 例如: 包含列表: 中午12:00至下午1:00 1:00至下午2:00 等等... 其中分割部是配置。 我认为你必须使用datetime和它划分到一定数量(在这种情况下,一小时)

Is there a way to make a list of time range? For example: A list containing: 12:00 to 1:00 pm 1:00 to 2:00 pm etc... Where the dividing section is configuration. I think you have to use datetime and divide it to a certain number(in this case one hour)

可能有人请点我的方向是正确的,或者提供个例子吗?

Could someone please point me to the right direction or provide me an example?

在此先感谢!

推荐答案

有没有内置的定义时间范围的类型,但是这将是pretty的方便相结合,以创建一个日期时间时间跨度。例如:

There's no built-in type that defines a time-range but it would be pretty easy to create one by combining a DateTime and a TimeSpan. For example:

struct TimeRange
{
    private readonly DateTime start;
    private readonly TimeSpan duration;

    public TimeRange ( DateTime start, TimeSpan duration )
    {
        this.start = start;
        this.duration = duration;
    }
}

您可以再建一个名单,其中,TIMERANGE> 使用特定的的DateTime 为出发点,并添加所需的时间跨度的每个元素。例如,下面的 TIMERANGE 的一个非常基本的实施,包括一个名为方法分割返回一个 IEnumerable的< TIMERANGE> 基于当前的 TIMERANGE 和子范围所需的时间

You could then build a List<TimeRange> using a specific DateTime as the starting point and adding the required TimeSpan for each element. For example, here's a very basic implementation of TimeRange including a method called Split which returns an IEnumerable<TimeRange> based on the current TimeRange and the required duration of the sub-ranges.

struct TimeRange
{
    private readonly DateTime start;
    private readonly TimeSpan duration;

    public TimeRange ( DateTime start, TimeSpan duration )
    {
        this.start = start;
        this.duration = duration;
    }

    public DateTime From { get { return start; } }

    public DateTime To { get { return start + duration; } }

    public TimeSpan Duration { get { return duration; } }

    public IEnumerable<TimeRange> Split (TimeSpan subDuration)
    {
        for (DateTime subRangeStart = From; subRangeStart < this.To; subRangeStart += subDuration)
        {
            yield return new TimeRange(subRangeStart, subDuration);
        }
    }

    public override string ToString()
    {
        return String.Format ("{0} -> {1}", From, To);
    }
}

然后,您可以做这样的事情:

You can then do something like this:

TimeRange mainRange = new TimeRange(DateTime.Now, new TimeSpan(12, 0, 0));
List<TimeRange> rangeList = mainRange.Split(new TimeSpan(1, 0, 0)).ToList();

这会给1小时的时间开始12时间范围的列表,从当前的时间。

This will give a list of 12 time ranges of 1-hour duration starting from the current time.

**更新**

请注意,上述实施是非常基本的。该分割的方法,例如,将愉快地产生范围的双床,其中最后一个子范围的末端超出父范围的结束如果子期间不父范围的组成分裂。这将是很难增加检查这种事情,虽然。真正的问题是要发生在这些类型的场景是什么。

Note that the above implementation is VERY basic. The Split method, for example, will happily produce a lits of ranges where the end of the last sub-range is beyond the end of the parent range if the sub duration is not an integral division of the parent range. It would be hard to add checks for this kind of thing, though. The real question is what you want to happen in those kind of scenarios.

这也将是很容易的创建一个静态 TimeRange.CreateList 方法建立了一个名单,其中,TIMERANGE&GT; 而不需要明确的亲范围。

It would also be very easy to create a static TimeRange.CreateList method that builds a List<TimeRange> without the need for an explicit parent range.

这篇关于C#有没有办法让时间范围名单?配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆