.Net TimezoneInfo.ConvertTime将UTC日期转换为萨摩亚时返回了不正确的值。

huangapple go评论68阅读模式
英文:

.Net TimezoneInfo.ConvertTime UTC date to Samoa incorrect value returned

问题

var finalValue = TimeZoneInfo.ConvertTime(x, timeZoneInfo).ToString(format);
当将ConvertTime方法应用于我的'x'日期,该日期位于UTC时区,转换为萨摩亚标准时间时,它错误地增加了+14小时,而不是在timeZoneInfo的BaseUtcOffset中显示的+13小时,我的'finalValue'变量应匹配03:14:39而不是04:14:39,但此方法对其他时区的工作正常。

英文:

var finalValue = TimeZoneInfo.ConvertTime(x, timeZoneInfo).ToString(format);.Net TimezoneInfo.ConvertTime将UTC日期转换为萨摩亚时返回了不正确的值。[.Net TimezoneInfo.ConvertTime将UTC日期转换为萨摩亚时返回了不正确的值。](https://i.stack.imgur.com/YgLPc.png)
.Net TimezoneInfo.ConvertTime将UTC日期转换为萨摩亚时返回了不正确的值。
.Net TimezoneInfo.ConvertTime将UTC日期转换为萨摩亚时返回了不正确的值。

When applying the ConvertTime method to my 'x' date which is in UTC to the Samoa Standard Time it's incorrectly applying +14 hours instead of the +13 hours that is showing on the BaseUtcOffset in the timeZoneInfo , my 'finalValue' var should match 03:14:39 and not 04:14:39, this method is working correctly for other timezones though

答案1

得分: 3

这似乎确实是一个错误,因为与 2021 年关于萨摩亚夏令时规则的更改相关的 Windows 注册表中缺少数据。

这已经在 IANA 2021b 中实施,但微软似乎忽略了它。在 Microsoft 夏令时博客 上找不到相关信息。

更干净的重现方式:

var dto1 = DateTimeOffset.Parse("2022-01-01T00:00:00Z");
Console.WriteLine(dto1.ToString("o"));

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Samoa Standard Time");
var dto2 = TimeZoneInfo.ConvertTime(dto1, tz);
Console.WriteLine(dto2.ToString("o"));

输出:

2022-01-01T00:00:00.0000000+00:00
2022-01-01T14:00:00.0000000+14:00

(最后一个应该是 2022-01-01T13:00:00.0000000+13:00。)

相关的 Windows 注册表键是:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\Samoa Standard Time\Dynamic DST,在我的(完全更新的 Win 11 22H2)计算机上显示如下:

.Net TimezoneInfo.ConvertTime将UTC日期转换为萨摩亚时返回了不正确的值。

2012 年的条目适用于所有年份。应该有一个新的条目用于 2021 年结束夏令时但不重新开始,以及另一个条目用于 2022 年根本没有夏令时。

我会向 Microsoft 报告此问题。谢谢!

英文:

This indeed appears to be a bug, due to missing data in the Windows registry related to the 2021 change to DST rules in Samoa.

https://www.timeanddate.com/news/time/samoa-removes-dst.html

This was implemented in IANA 2021b, but it appears Microsoft missed it. It's not on the Microsoft DST blog.

A cleaner repoduction:

var dto1 = DateTimeOffset.Parse("2022-01-01T00:00:00Z");
Console.WriteLine(dto1.ToString("o"));

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Samoa Standard Time");
var dto2 = TimeZoneInfo.ConvertTime(dto1, tz);
Console.WriteLine(dto2.ToString("o"));

Output:

2022-01-01T00:00:00.0000000+00:00
2022-01-01T14:00:00.0000000+14:00

(The last one should be 2022-01-01T13:00:00.0000000+13:00.)

The relevant Windows Registry key is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\Samoa Standard Time\Dynamic DST which on my (fully updated Win 11 22H2) machine shows:

.Net TimezoneInfo.ConvertTime将UTC日期转换为萨摩亚时返回了不正确的值。

The 2012 entry is being applied for all years. There should have been a new entry for 2021 ending DST but not restarting it, and another entry for 2022 that doesn't have DST at all.

I'll report this to Microsoft. Thanks!

huangapple
  • 本文由 发表于 2023年3月9日 22:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定