【问题标题】:C# Simulate DateTimeC# 模拟日期时间
【发布时间】:2012-05-17 13:47:15
【问题描述】:

我想模拟 DateTime。假设我有要执行的操作列表,每个操作都有一个日期时间字段。当该日期时间到来时,应该执行该操作。我可以用 DateTime.Now 检查日期时间;但是我怎样才能模拟 DateTime。我的意思是如果当前时间是下午 2 点。并且该动作应该在下午 4 点、5 点运行。我可以使用模拟当前时间到下午 4 点,然后执行第一个操作,一小时后执行第二个操作。

谢谢,

【问题讨论】:

  • 时间应该从下午 4 点开始继续。即它应该像时钟一样工作。
  • 你需要引入日期时间提供者接口,一个将日期传递给你的类的类。出于测试目的,您需要实现该接口的虚拟版本
  • 我实际上已经使用 d Timespan 值实现了这个。例如。假设我希望 d 系统时钟为 2012 年 5 月 1 日晚上 21:00:00。当前日期是 5 月 8 日晚上 11:38:00。当 d 应用程序启动时,我得到 d 差异 DateTime simulationDateTime = '01-May-2012 21:00:00"; long ticksDifference = DateTime.Now.Subtract; compare d date DateTime currentDateTime = DateTime.Now.Subtract(new Timespan(ticksDifference) ); 这正是我现在所需要的。自 DateTime 以来,CurrentDateTime 字段就像时钟一样工作。现在总是随着时间跨度的变化而变化,d 差异为我提供了准确的模拟时钟。

标签: c# datetime simulation


【解决方案1】:

前段时间我发布了一些关于以这种方式测试日期的方法:

http://ivowiblo.wordpress.com/2010/02/01/how-to-test-datetime-now/

希望对你有帮助

【讨论】:

  • 感谢您的建议。 :-)
【解决方案2】:

实现此目的的最简单方法是将系统时钟更改为“测试时间”,运行测试,然后再改回来。这很 hacky,我真的不推荐它,但它会起作用。

更好的方法是使用DateTime.Now 之上的抽象,这将允许您注入静态值或操作检索到的值以进行测试。鉴于您希望测试值“打勾”,而不是保持静态快照,将TimeSpan 添加到“现在”将是最简单的。

所以添加一个名为“offset”的应用程序设置,可以解析为TimeSpan

<appSettings>
    <add key="offset" value="00:00:00" />
</appSettings>

然后每次检索时将此值添加到您的DateTime.Now

public DateTime Time
{ 
    get 
    { 
        var offset = TimeSpan.Parse(ConfigurationManager.AppSettings["offset"]);
        return DateTime.Now + offset;
    }
}

要在未来 1 小时 20 分钟后运行,您只需调整 offset

<add key="offset" value="01:20:00" />

理想情况下,您应该为DateTime 创建一个接口并实现依赖注入,但出于您的目的——尽管这将是首选——我建议打开的蠕虫罐头会给您带来混乱的世界。这很简单,而且会奏效。

【讨论】:

    【解决方案3】:

    这实际上是一个复杂的问题,但幸运的是有一个解决方案:Noda Time

    【讨论】:

    • 感谢您的建议 :-)
    【解决方案4】:

    最简单的方法是注释掉检查 DateTime.Now 的部分并创建一个您可以调用的新方法/属性,该方法/属性将返回一组脚本时间。

    例如:

    class FakeDateTime
    {
        private static int currentIndex = -1;
        private static DateTime[] testDateTimes = new DateTime[]
            {
                new DateTime(2012,5,8,8,50,10),
                new DateTime(2012,5,8,8,50,10)  //List out the times you want to test here
            };
    
        /// <summary>
        /// The property to access to check the time.  This would replace DateTime.Now.
        /// </summary>
        public DateTime Now
        {
            get
            {
                currentIndex = (currentIndex + 1) % testDateTimes.Length;
                return testDateTimes[currentIndex];
            }
        }
    
        /// <summary>
        /// Use this if you want to specifiy the time.
        /// </summary>
        /// <param name="timeIndex">The index in <see cref="testDateTimes"/> you want to return.</param>
        /// <returns></returns>
        public DateTime GetNow(int timeIndex)
        {
            return testDateTimes[timeIndex % testDateTimes.Length];
        }
    }
    

    如果您想要更具体(或更好)的答案,请提供一些代码示例。

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 1970-01-01
      • 2017-11-28
      • 2021-10-24
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 2012-01-20
      相关资源
      最近更新 更多