NodaTime 时区解析:Etc/GMT+3 负偏移

NodaTime timezone parse: Etc/GMT+3 offset in negative

这是一个说明问题的小代码示例:

enter code here
var offset1 = DateTimeZoneProviders.Tzdb.GetZoneOrNull("Europe/Moscow")
            .GetUtcOffset(Instant.FromDateTimeUtc(DateTime.UtcNow));
var offset2 = DateTimeZoneProviders.Tzdb.GetZoneOrNull("Etc/GMT+3")
            .GetUtcOffset(Instant.FromDateTimeUtc(DateTime.UtcNow));

Console.WriteLine(offset1 + " vs " + offset2);

“{+03} 对比 {-03}”的结果

是的,没错。那是因为 "Etc/GMT+X" 区域 ID 令人困惑。它们匹配 POSIX TZ 字符串,出于某种原因,它表示 "the offset of UTC from local time" 而不是正常的 "offset of local time from UTC"。请参阅维基百科 list of tz database time zone 以确认这一点。

来自 etcetra 文件:

# Be consistent with POSIX TZ settings in the Zone names,
# even though this is the opposite of what many people expect.
# POSIX has positive signs west of Greenwich, but many people expect
# positive signs east of Greenwich.  For example, TZ='Etc/GMT+4' uses
# the abbreviation "-04" and corresponds to 4 hours behind UT
# (i.e. west of Greenwich) even though many people would expect it to
# mean 4 hours ahead of UT (i.e. east of Greenwich).

如果要创建表示固定偏移量的时区,可以使用标识符 "UTC+X" 或使用 DateTimeZone.ForOffset.