【问题标题】:Incorrect timezone offset时区偏移不正确
【发布时间】:2019-03-24 19:21:38
【问题描述】:

我试图弄明白时区问题。我位于挪威,即 gmt+1,我正在尝试将 UTC 时间转换为本地时间。据我了解,这意味着当地时间应该是 utc + 1 小时。所以 12:00 UTC 应该变成 13:00 local。出于某种原因,我不断得到+ 2小时。

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nb-NO");
var zone = System.TimeZoneInfo.Local;                           // Zone = {(UTC+01:00) Amsterdam, Berlin, Bern, Oslo, Roma, Wien}
DateTime time = DateTime.Parse("2019-04-01T00:00:00+00:00");    // time = {01.04.2019 02:00:00} Why is this not 01:00:00 when timzone = utc+1?
var offset = zone.GetUtcOffset(time);                           // Offset = {02:00:00}

DateTime timeWithoutUtc = DateTime.Parse("2019-04-01T00:00:00");
var t2 = timeWithoutUtc.ToLocalTime();                          // {01.04.2019 02:00:00}

DateTimeOffset timeOffsetWithUtc = DateTimeOffset.Parse("2019-04-01T00:00:00+00:00");
var t3 = timeOffsetWithUtc.ToLocalTime();                       // {01.04.2019 02:00:00 +02:00}

我确信这是非常基本的东西,但我似乎无法理解我哪里出错了。

【问题讨论】:

  • 您在挪威有夏令时,不是吗?那么到 4 月 1 日它开始生效,距离 UTC 2 小时是正确的吗?
  • 谢谢!夏令时尚未开始,但到 4 月 1 日已经开始。我快疯了

标签: c# timezone


【解决方案1】:

我不认为示例中的日期是夏令时开始。感谢 sellotape 指出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 2016-02-26
    • 2015-07-20
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2012-04-20
    相关资源
    最近更新 更多