简体   繁体   English

在C#中计算经过时间时的夏时制问题

[英]Daylight saving issue while calculating Elapsed Time in C#

I'm trying to calculate elapsed time for my project. 我正在尝试计算项目的经过时间。
For example, 例如,
Start time is 06-Nov-2011 01:59:58 AM 开始时间是06-Nov-2011 01:59:58 AM
End Time is 06-Nov-2011 01:00:00 AM 结束时间是06-Nov-2011 01:00:00 AM
Actual Elapsed Time is 00:00:02 实际经过时间为00:00:02
But getting Elapsed Time is -00:59:58 (Due to Daylight Saving, clock went back 1 hour) 但是,经过时间为-00:59:58(由于夏令时,时钟返回1小时)
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. 您的值基本上是模棱两可的-假设时钟回到凌晨2点,这两次都发生了两次

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 : 如果您知道要将开始时间视为较早的选项,而将结束时间视为较晚的选项,则可以使用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, eg yyyy-MM-ddTHH:mm:ss 如果可能话,改变你的数据源来记录的本地日期/时间UTC日期/时间,而不是...最好是在一个更解析友好的格式,如YYYY-MM-DDTHH:MM:SS

Use Coordinated Universal Time to store your timestamps. 使用世界标准时间存储您的时间戳。 Whenever you compare two timestamps in Coordinated Universal Time, you get the correct difference. 每当您比较协调世界时中的两个时间戳时,您都会得到正确的差值。

In C# you get the current timestamp in that format by using DateTime dt = DateTime.UtcNow . 在C#中,您可以通过使用DateTime dt = DateTime.UtcNow获得该格式的当前时间戳。

See also http://en.wikipedia.org/wiki/Coordinated_Universal_Time . 另请参见http://en.wikipedia.org/wiki/Coordinated_Universal_Time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM