Shane Fulmer
Shane Fulmer

Reputation: 7708

Handling DST in 24H Application

I'm working on an application that needs to run 24H a day, and am working on correctly implementing support for DST. All times are currently stored in UTC, but I'm having issues when I try to display in local time. Right now I'm using savedDate.ToLocalTime() to convert from DB values to local time for display. This seems to be working fine, except for when I change the time zone information in an effort to test DST. If I modify the timezone on the client pc, the display doesn't update with the new time zone. Is there a better way to test DST, or is my conversion to local time incorrect for this scenario?

Upvotes: 0

Views: 789

Answers (4)

Larry
Larry

Reputation: 51

I have discovered that some .Net CF 2.0 builds do not properly do time translations between UTC and local time. The issues surround the change that occurred in US timezone several years ago.

The symptom is that DST appears to not be considered to go into effect until April (old rule) rather than March (new rule). Presumably, there is also a problem on the back-end of the DST change (October/November).

As yet I have not found information about what might be the minimum build needed for the translation to work correctly.

Note the this is NOT fixed by applying the operating system DST fix (which, obviously, should also be installed). It is completely intrinsic to CF as far as I can tell. I've not tried every combination, but just be careful.

If you want accurate translations in CF, you have only two options:

  1. do the conversion yourself (very complex, rigid, and error-prone).

  2. ensure you have the necessary .Net and OS patches on the device so that translations are accurate.

You might also be able to find a class library that will work for you. One promising class was World Clock / Code Project), but, unfortunately it relies on registry information not available in CF. Thus, it's not directly portable to CF.

And then finally, any roll-your-own solution will require on having time zone information available (UTC offsets, DST on/off dates, etc). and accurate at all times.

Sorry for the bad news.

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124696

If you retrieve a DateTime value from a database, you will generally get a DateTime with the Kind property set to DateTimeKind.Unspecified. Basically because any database-specific DateTime types I'm aware of (e.g. SqlDateTime) don't include timezone information.

If you then try to "convert" this to local time using savedDate.ToLocalTime(), you'll get back the same DateTime value that was stored, without any conversion.

What you need to do is indicate that the saved date is in UTC before performing the conversion, something like:

DateTime savedDateTime = (DateTime) reader["SomeDateTimeColumn"];

// Convert from unspecified to UTC
DateTime utcDateTime = savedDateTime.SpecifyKind(savedDateTime, DateTimeKind.Utc);
...
// Convert from UTC to local
DateTime localDateTime = utcDateTime.ToLocalTime();

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500514

Hmm. Do you expect to actually have to change time zone while the app is running? I wouldn't be surprised if the framework cached it after it's first fetched.

I would set the time zone on the device, change the time to "just before the change to DST", run the app and check that it's reporting times correctly. Then change the time to "just before the change out of DST".

Bear in mind that depending on your device and the age of its time zone database, it may not know about the change to DST in the US a few years ago. (Ah, time zones. Gotta love 'em.)

Upvotes: 1

lance
lance

Reputation: 16342

I remember reading, perhaps regarding the Compact Framework, about .NET not being the most reliable when it comes to reporting about DST. More reliable was getting the current time in local time and UTC, and then determining (and using in future adjustments) the difference yourself. That seems like a horrible route to go if it's not really necessary. I'll see if I can find a reference backing up what I think I remember.

What are you doing, exactly, to change the timezone on the workstation?

Upvotes: 1

Related Questions