简体   繁体   English

C# 处理夏令时转换

[英]C# Handling Daylight Saving in Time Conversion

I have a date string 2020-11-26T14:24:29-08:00 which is passed to the backend ASP NET MVC application.我有一个日期字符串2020-11-26T14:24:29-08:00传递给后端 ASP NET MVC 应用程序。

I am parsing the date/time string using the below code:我正在使用以下代码解析日期/时间字符串:

    DateTime dateTime = DateTimeOffset.ParseExact("2020-11-26T14:24:29-08:00", 
                                                  "yyyy'-'MM'-'dd'T'HH':'mm':'sszzz",
                                                  CultureInfo.InvariantCulture).DateTime;

This code is giving me time which is 1 hour less than the actual time.这段代码给了我比实际时间少 1 小时的时间。 This is because of Daylight Savings.这是因为夏令时。

Now, I want to know if daylight saving is active in the timezone or not.现在,我想知道夏令时是否在时区有效。 So I wrote the below code:所以我写了下面的代码:

var offset = DateTimeOffset.ParseExact("2020-11-26T14:24:29-08:00", 
                                       "yyyy'-'MM'-'dd'T'HH':'mm':'sszzz", 
                                       CultureInfo.InvariantCulture);

TimeZoneInfo zone = null;
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
{
    if(zone.BaseUtcOffset == offset.Offset)
            zone = z;                        // this runs multiple times   
}

The above code if condition is hitting multiple times with the following zones satisfying condition.上面的代码if条件多次命中,并且以下区域满足条件。

  • Yukon Standard Time育空标准时间
  • Pacific Standard Time (Mexico)太平洋标准时间(墨西哥)
  • UTC-08 UTC-08
  • Pacific Standard Time太平洋标准时间

So, I further narrowed down the code所以,我进一步缩小了代码范围

var offset = DateTimeOffset.ParseExact("2020-11-26T14:24:29-08:00", 
                                       "yyyy'-'MM'-'dd'T'HH':'mm':'sszzz", 
                                       CultureInfo.InvariantCulture);

TimeZoneInfo zone = null;
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
{
    if(zone.BaseUtcOffset == offset.Offset && zone.SupportsDaylightSavingTime)   // added second condition
            zone = z;                        // this runs multiple times   
}

This time I got three:这次我得到了三个:

  • Yukon Standard Time育空标准时间
  • Pacific Standard Time (Mexico)太平洋标准时间(墨西哥)
  • Pacific Standard Time太平洋标准时间

Is it possible to get the exact timezone?是否有可能获得确切的时区? (I know this user is in Pacific Standard Time ) Or is it okay to use any (first one) and add one hour to time to get the correct time? (我知道这个用户在Pacific Standard Time )或者可以使用任何(第一个)并添加一个小时来获得正确的时间吗?

If that datetime string is all you're able to use, I suspect you have to hope the offset they've given you is in line with their time zones daylight savings time (and so could be different for the same location).如果您只能使用该日期时间字符串,我怀疑您必须希望他们给您的偏移量与他们的时区夏令时一致(因此对于同一位置可能会有所不同)。

I think more information might be needed before a useful answer can be given.我认为在给出有用的答案之前可能需要更多信息。 Where is this value coming from, what use is the daylight savings time?这个值来自哪里,夏令时有什么用?

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

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