How can I determine whether a given date is in Daylight Saving Time for a given timezone in .NET 2.0?

StackOverflow https://stackoverflow.com/questions/44795

Question

I'm on .NET 2.0, running under Medium Trust (so TimeZoneInfo and the Registry are not allowed options). I'm asking the user for two dates and a time zone, and would really love to be able to automatically determine whether I need to adjust the time zone for DST.

This probably isn't even a valid scenario unless I have some very robust support, a la TimeZoneInfo, to differentiate between all of the different varieties of Time Zones in the first place.

Was it helpful?

Solution

In .NET 2.0 you have to code this yourself. It involves researching daylight savings time laws in various regions and building that into your own data structures. The problem is somewhat simplified if you only care about a subset of time zones, for example just in the USA, but if you need all global time zones, you have a lot of work to do, and then the code has to be updated every few years when the laws change. Even the new time zone objects in the latest version of .NET will require windows updates to keep them correct as laws change.

Look here, here, and here for more info.

OTHER TIPS

The TZ Database is a public domain database of timezone rules that is very well maintained. There is also a compiled format for the data they provide, and there are lots of libraries available to read the compiled data, like this one: ZoneInfo (tz database / Olson database) .NET API

Excellent timezone library here: TZ4Net

Well, since TimeZoneInfo is excluded, you're probably not going to find a solution in the framework itself (but don't quote me on that).

In which case, have you considered reflectoring the TimeZoneInfo class and using what you find there?

@Domenic, I've considered, but I'd prefer to stay legal, and I'm fairly sure the information would have to be embedded into the framework anyway, or grabbed from the registry in some sneaky way that doesn't require permissions...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top