There are things that sound hard, such as build a table with edit, update, delete, and sorting, but in ASP.NET are trivial. Then there are things that sound trivial, like managing time zones, that are a real PITA.
.NET DateTime objects do not store any type of flag to say "I'm an EST object" - they just assume things. If you say myDateTime.ToUniversalTime() .NET assumes myDateTime is a local time object. If you say myDateTime.ToLocalTime() .NET assumes you have a UTC time. So *you* better know what you put in myDateTime because .NET won't remember.
Okay, this isn't a show stopper, just unexpected. We can make life simple by storing and using only UTC times, then convert to local time on display only. This means using DateTime.UtcNow instead of DateTime.Now, which is local. Once you have your site in UTC time, an idea might pop in your head, "Hey, it should be no problem to add a member to Profile to let users choose the time zone they want to see posts in!" Pop that idea right out...
.NET contains no methods to convert a time to an arbitrary time zone. Once you consider Daylight Savings time issues, you really, really start to wonder how such a feature could slip. It was planned for 1.0, planned for 1.1, planned for 2.0, and is planned for the next update. I decided at this point I would convert to EST for my postings, and deal with Profile members another day. I still need to take into account Daylight Savings, but I'm lucky that my (PST) web server also observes US Daylight Savings at the same time I do here in good ole EST. Thus, the following will convert a UTC date to EST on a PST server (I'm willing to be portable only to a certian extent):
int offset = TimeZone.CurrentTimeZone.GetUtcOffset(date).Hours + 3;
return date.AddHours(offset).ToString(format);
Adapting this to your server, or allowing users to select their time zone, is left as an exercise for the reader.
Posted By Mike On Saturday, May 20, 2006
Filed under asp.net datetime |
Comments (1)