根据时间间隔验证时间

时间:2017-04-17 20:17:02

标签: c# timespan

我有三条规则:

  • 甲。时间段在上午9点到下午3点之间
  • B中。时间段在下午3点到晚上7点之间
  • ℃。时间段在晚上7点到9点之间

目前我将其表示为TimeSpan s

public class Rule
{
    public string Name {get; set;}
    public TimeSpan From {get; set;}
}

List<Rule> rules = new List<Rule>()
{
   new Rule() {From = new TimeSpan(9, 0, 0), Name = "A"},
   new Rule() {From = new TimeSpan(15, 0, 0), Name = "B"},
   new Rule() {From = new TimeSpan(19, 0, 0), Name = "C"}
};

我的问题是如何验证时间输入让我们说9,10pm反对这些规则?

它应该选择第三条规则。

4 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

DateTime input = DateTime.Now;

TimeSpan span = input.TimeOfDay;
for (int i = 0; i < rules.Count - 1; i++) {
    if (span >= rules[i].From && span < rules[i + 1].From) {
        return rules[i];
    }
}
return rules[rules.Count - 1];

答案 1 :(得分:1)

下面的内容可能会起到作用:

var currentSpan = DateTime.Now - DateTime.Now.Date;
int ruleIndex = -1;

for (int i = 0; i < rules.Count - 1; i++)
{
    if (currentSpan >= rules[i].From && currentSpan < rules[i + 1].From)
    {
        ruleIndex = i;
        break;
    }
}

if (ruleIndex == -1 && (currentSpan >= rules.Last().From || currentSpan < rules.First().From))
{
    ruleIndex = rules.Count - 1;
}

var rule = rules[ruleIndex];

答案 2 :(得分:1)

通过向Rule对象

添加结束时间稍作修改
public class Rule {
    public string Name { get; set; }
    public TimeSpan From { get; set; }
    public TimeSpan To { get; set; }
}

此扩展方法用于检查提供的输入是否在规则的时间范围内。

public static class RuleExtension {
    public static bool Contains(this Rule rule, TimeSpan input) {
        var value = TimeSpan.Parse(input.ToString());
        var start = rule.From;
        var end = rule.To;

        if (end < start) {
            //loopback
            end += TimeSpan.FromHours(24);
            if (value < start)
                value += TimeSpan.FromHours(24);
        }

        return start.CompareTo(value) <= 0 && value.CompareTo(end) < 0;
    }
}

以下单元测试用于验证扩展方法,并从集合中提取规则。 (注意:使用FluentAssertions来断言结果。)

[TestClass]
public class MyTestClass {
    [TestMethod]
    public void _ValidateTime() {
        var rules = new List<Rule>()
        {
           new Rule() {From = new TimeSpan(9, 0, 0), To = new TimeSpan(15, 0, 0), Name = "A"},
           new Rule() {From = new TimeSpan(15, 0, 0), To = new TimeSpan(19, 0, 0), Name = "B"},
           new Rule() {From = new TimeSpan(19, 0, 0), To= new TimeSpan(5, 0, 0), Name = "C"}
        };

        var input = TimeSpan.Parse("21:10");
        rules.FirstOrDefault(r => r.Contains(input))
            .Should()
            .NotBeNull()
            .And
            .Match((Rule r) => r.Name == "C");

        input = TimeSpan.Parse("08:10");
        rules.FirstOrDefault(r => r.Contains(input))
            .Should()
            .BeNull();

        input = TimeSpan.Parse("18:10");
        rules.FirstOrDefault(r => r.Contains(input))
            .Should()
            .NotBeNull()
            .And
            .Match((Rule r) => r.Name == "B");

        input = TimeSpan.Parse("10:10");
        rules.FirstOrDefault(r => r.Contains(input))
            .Should()
            .NotBeNull()
            .And
            .Match((Rule r) => r.Name == "A");
    }

答案 3 :(得分:0)

一天只有24小时。你能不能制作一个带有枚举枚举的地图数组:

public enum Category
{
 A,
 B,
 C
}

然后是阵列。所以从00:00到09:00之间的类别将是C。

Category[] values = new Category[24];
for(int i = 0;i<9;i++)
{
 values[i] = Category.C;
}

所以你可以同样分配每个小时。

现在给出一小时(比如早上6点)作为输入,您可以使用开关:

switch(values[6]) // gets the appropriate category.
{
  case Category.A:
   // handle
   break;
  case Category.B:
   // Handle
   break;
  case Category.C:
   // handle
   break;
  default:
   break;

}