节能日光问题在计算消耗时间在C# [英] Daylight saving issue while calculating Elapsed Time in C#

查看:250
本文介绍了节能日光问题在计算消耗时间在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来计算经过的时间为我的项目。
例如,
开始时间为06 - 11月2011年1时59分58秒A.M
结束时间是06 - 11月,2011 01:00:00 A.M
实际用掉的时间是00:00:02
但是,让经过的时间-00:59:58(因夏令时,时钟又回到1小时)
在夏令时期间如何计算这更好的方式与正确经过时间?

code:

 日期时间startDttm = DateTime.Parse(2011年11月6号上午01时59分58秒);
日期时间endDttm = DateTime.Parse(2011年11月6日上午01时零零分00秒);
时间跨度elapsedTime = endDttm.Subtract(startDttm);
Console.WriteLine(已用时间 - + elapsedTime);
 

解决方案

你的价值观基本上是不明确的 - 这两个时间发生的两次的假设时钟又回到在凌晨2点。

如果你知道你要正确对待的开始时间为前面的选项,结束时间为以后的选项,你可以使用的野田佳彦时间

 使用系统;
使用NodaTime;
使用NodaTime.Text;

公共类测试
{
    静态无效的主要(字串[] args)
    {
        VAR模式= LocalDateTimePattern.CreateWithInvariantInfo
              (MMM DD,YYYY HH:MM:SS TT);
        LocalDateTime开始= pattern.Parse(2011年11月6日上午01时59分58秒)值。
        LocalDateTime结束= pattern.Parse(2011年11月6日上午01时零零分00秒)值。

        DateTimeZone区= DateTimeZone.ForId(美国/芝加哥);

        //如果这是不明确的,挑前面的选项
        ZonedDateTime zonedStart = zone.AtEarlier(开始);

        //如果这是不明确的,挑选后选择
        ZonedDateTime zonedEnd = zone.AtLater(完)

        持续时间的持续时间= zonedEnd.ToInstant() -  zonedStart.ToInstant();

        //打印00:00:02
        Console.WriteLine(duration.ToTimeSpan());
    }
}
 

在一个理想的世界,但是,你不会有解析不明确的地方时,你猜他们是否注定要提前或推后。有什么背景吗?

如果的所有的可能,改变你的数据源来记录UTC日期/时间,而不是当地的日期/时间...最好是在一个更加解析友好的格式,例如: YYYY-MM-DDTHH:MM:SS

I'm trying to calculate elapsed time for my project.
For example,
Start time is 06-Nov-2011 01:59:58 A.M
End Time is 06-Nov-2011 01:00:00 A.M
Actual Elapsed Time is 00:00:02
But getting Elapsed Time is -00:59:58 (Due to Daylight Saving, clock went back 1 hour)
How can i calculate this in better way with correct elapsed time during Daylight Saving?

Code:

DateTime startDttm = DateTime.Parse("Nov 06, 2011 01:59:58 AM");  
DateTime endDttm = DateTime.Parse("Nov 06, 2011 01:00:00 AM");  
TimeSpan elapsedTime = endDttm.Subtract(startDttm);  
Console.WriteLine("Elapsed Time - " + elapsedTime);

解决方案

Your values are basically ambiguous - both of those times occurred twice assuming the clocks went back at 2am.

If you know that you want to treat the start time as the earlier option, and the end time as the later option, you can use Noda Time:

using System;
using NodaTime;
using NodaTime.Text;

public class Test
{
    static void Main(string[] args)
    {
        var pattern = LocalDateTimePattern.CreateWithInvariantInfo
              ("MMM dd, yyyy HH:mm:ss tt");
        LocalDateTime start = pattern.Parse("Nov 06, 2011 01:59:58 AM").Value;
        LocalDateTime end = pattern.Parse("Nov 06, 2011 01:00:00 AM").Value;

        DateTimeZone zone = DateTimeZone.ForId("America/Chicago");

        // Where this is ambiguous, pick the earlier option
        ZonedDateTime zonedStart = zone.AtEarlier(start);

        // Where this is ambiguous, pick the later option
        ZonedDateTime zonedEnd = zone.AtLater(end);

        Duration duration = zonedEnd.ToInstant() - zonedStart.ToInstant();

        // Prints 00:00:02
        Console.WriteLine(duration.ToTimeSpan());
    }        
}

In an ideal world, however, you wouldn't be having to parse ambiguous local times and guess whether they're meant to be earlier or later. What's the context here?

If at all possible, change your data source to record UTC date/times instead of local dates/times... ideally in a more parse-friendly format, e.g. yyyy-MM-ddTHH:mm:ss

这篇关于节能日光问题在计算消耗时间在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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