Why doesn't C# detect that 1970/1/1 was under BST?

半腔热情 提交于 2019-12-02 03:30:28

When you use the TimeZoneInfo class to work with a system time zone ("GMT Standard Time" in this case), you are asking for time zone data from the Windows operating system, which is stored in the registry at:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones

Windows simply does not carry historical data back that far. For "GMT Standard Time", which is the time zone in London that alternates between GMT and BST, it only knows about one set of rules - the current ones that are in effect. It does not know about anything before that (it last changed in 1996).

Note that per Microsoft's DST/TZ Support Policy, only changes from 2010 forward are guaranteed to be recorded. There are several older changes that have historically been in Windows (such as the US 2007 DST change and others), which do indeed work, but from a global perspective you may not get the best results with dates before 2010.

For that, you'll need a full implementation of the IANA TZ Database, and in .NET, one of the best ways to do that is with the Noda Time library.

Your code, transliterated to Noda Time:

var utcTime = Instant.FromUtc(1970, 1, 1, 5, 0, 0);
var britishZone = DateTimeZoneProviders.Tzdb["Europe/London"];
var ukTime = utcTime.InZone(britishZone);
Console.WriteLine(ukTime.ToDateTimeUnspecified()); // just for equivalent output

// Prints:  1/1/1970 6:00:00 AM

See also the timezone tag wiki.

Have you looked at creating a custom time zone adjustment for the historical period(s) that are in question?

https://msdn.microsoft.com/en-us/library/bb397784(v=vs.110).aspx

which can be used in the case of:

The time zone does not have accurate information about time zone adjustments for a particular historic period.

I think the code is working correctly. BSt starts from last sunday of march until last sunday of october where the clock moves one hour forward . That is UTC+1. Since you are looking at a date in jan, london time will be same as UTC.that is utc+0 Check the wikipedia for timezone details https://en.m.wikipedia.org/wiki/British_Summer_Time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!