views:

252

answers:

2

Hi,

Is there a way to define the timezone for an application in ASP.NET such that all times read from/compared to current server time are implicitly converted, or do I need to put in conversion statements as each and every DateTime.Now call?

+1  A: 

Internally use DateTime.UtcNow. For .NET 3.5, check out TimeZoneInfo.ConvertTime.

eed3si9n
+2  A: 

I am not sure about the latest evolutions of ASP, but this article from 2006 provides an interesting answer:

The problem is that timezone information is not directly available through the web browser. You could use heuristics to determine the correct time zone or you will have to store the timezone settings for a user based on their selection.

What follows may not be need with .Net 3.5 and its TimeZoneInfo..ConvertTime method, but still, for completeness sake:

Once you have the timezone settings you now need to translate the times. You should always store your date/time values in the DB in UTC. This eliminates many conversion issues. If you store the information in UTC then you need only translate from UTC to the user's local timezone when you display the data to them and you need to convert from their time zone to UTC when you get date/time values from them.

What makes this more difficult is the fact that the TimeZone class is not all that useful. Fortunately in v2.0 DateTime was updated to support an indicator on whether the time is in UTC or not. You can convert a DateTime to UTC using the DateTime.ToUniversalTime method. Unfortunately however you can't convert it back.

The ToLocalTime method uses the local time zone which, when run on the server, uses the server's time zone which isn't what you wanted. Even worse however is that you can't simply create a TimeZone object and use it as that support doesn't exist.

Michael Brumm created a nice little class for being able to create and use time zones easily. I personally use a heavily modified version of that code in my own apps and it works nicely. Here are the steps to convert from a DB UTC value to the local user's time zone.

1) Get the stored timezone value for the user
2) Create a SimpleTimeZone class to wrap it (using some mapping scheme that maps a DB value to the underlying Windows registry version)
3) Use the SimpleTimeZone.ToLocalTime method to convert the DateTime value to the local time.

For performance reasons you should probably get and initialize the SimpleTimeZone instance and cache it in the Items property for the length of the request so you don't have to keep creating it.

For converting from the user's local timezone to UTC do the reverse:

1) Get the stored timezone value from the user
2) Create a SimpleTimeZone class to wrap it
3) Use SimpleTimeZone.ToUniversalTime method to convert the DateTime to UTC.

VonC
Thanks for that - I'm not looking to display as "user time", just a different time (statically) from the server. Still this answers my question regardless.
Graphain