views:

864

answers:

4

Hi

on my site I need to know what timezone people are from to display messages to them at the right times.

So I am not too sure what to be searching for a tutorial how to do this.

So what I am planning to do is a user will come to my site and they will set their timezone by selecting in from a drop down list. I will store their settings in my database and use it to calculate the times.

So I am not sure what I need.

Like what should be my database time be storing? I read somewhere it should be UTC? So how do I set my db to do this?(Also I will be on a shared hosting site so it has to be set in a way that I can do it? maybe webconfig?).

Next I would need a list of all the timezones either has a html helpr or regular html. I really don't want to make one.

Then like I said I need a tutorial showing me what to do. I think it is adding and subtracting stuff from that time? But I could be wrong though.

Thanks

+11  A: 

For this purpose, you should definitely be storing your timestamps in UTC in the database.

When you need to display a date from the database on your site, you can do this:

DateTime stamp = /* get datetime from the database here, make sure you
                    use the constructor that allows you to specify the 
                    DateTimeKind as UTC. */

//E.g.
//DateTime stamp = new DateTime(2009, 12, 12, 12, 12, 12, DateTimeKind.Utc);

timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(" /* users time zone here */"); 
var convertedTime = TimeZoneInfo.ConvertTime(stamp, timeZoneInfo);

//Print out the date and time
//Console.WriteLine(convertedTime.ToString("yyyy-MM-dd HH-mm-ss"));

The list of timezones is already available in .Net, so you can see this post on how to enumerate them. For ASP.Net MVC, instead of printing the time out, you would want to assign the converted datetime to a property of your model class so your View could use it for display.

womp
Hi Thanks I will start trying to implement this. I have another unknown. Is the user comes to my site and I want to go through the db and see what messages they to display base on their timezone. So would I first figure out what their time is and then convert it to UTC? then do a where with it? So sort of reverse what your doing in your example?
chobo2
Well, initially you mentioned allowing the user to set their timezone. However, there are a number of methods of discovering the user's timezone via javascript, you could search around stackoverflow for more info, for example this post: http://stackoverflow.com/questions/1017053/finding-the-clients-timezone-and-culture-via-web-service
womp
I am fine with them choosing their timezone and don't really want to look into javascript right now. I am just saying would that be how I would start looking for specific dates in my db? By trying to convert back to UTF?
chobo2
I was just trying to get your stuff to run and um "Kind" is readOnly.
chobo2
Ah, correct you are, forgot about that. You have to specify it when you construct your DateTime object initially. For example, DateTime time = new DateTime(2009, 12, 12, 12, 12, 12, DateTimeKind.Utc);. I'll edit my answer.
womp
Hey I been thinking do I even need to know their timezone? I am just wonering if I store everything in UTC in my database and they come to my site can't I just go DateTime.Utc and it will give me the utc time? then I just use that to search my dtabase? Or would this give me the Utc time of the server?
chobo2
+1  A: 

First, check MSDN for DateTime documentation and read up on it. Here is an article on Best Practices with DateTime that covers timezones and UTC.

Yes, you should be storing UTC in your database. Storing DateTimes as UTC will allow you to translate times to (and from) users based on their selected timezone. You're on the right track!

jscharf
+2  A: 

I cannot stress how important it is to use UTC for storage and processing for any application you write. Even if you know the application will ever only be used in a single timezone it is still much easier to work with UTC than a local timezone because of daylight saving time issues.

There's really nothing special you need to do at the database level. Just create a normal datetime column, but make sure it is documented clearly that the column is assumed to be UTC.

I'll be honest, asp.net isn't my expertise, but I'm thinking you could obtain the timezone from the client request somehow. Actually, because the daylight saving time rules can be pretty obscure depending on the region it might be better to use a java script that calculates the UTC offset and use that to make the conversions. Even after the Energy Policy Act of 2005 there are still some exceptions to the DST rules that would rather hard to deal with on the server side. However, I think you're idea of allowing the client to set their own timezone would work well in most cases.

Brian Gideon
A: 

Actually - there is ip geolocation and time conversion software to help avoid all this and simplify your life. I built one that will automatically detect (based on IP Address) and convert your System or UTC DateTime into Local DateTime... - with proper Daylight Savings calculation. And if you like, it will format the DateTime for that Visitors country preference.

What this means is that you can forget asking for the TimeZone from the client... and you can just display (converted) proper times for them.

What this does is (1) detect the IP Address and latitude/longitude of the Visitor. Then (2) it calls a service - if needed - to get the Daylight Savings for that region, so it can perform the calculation.

(3) It caches the Daylight Savings for a while for you - so you don't have to make unnecessary API calls to other services. For example, if you display lots of dates on the same page for the visitor, it only needs to make at most one API call.

This means - you dont need to ask for a timezone. You dont need to figure out different UTC offsets yourself...or write javascript.

Its a simple .dll you can put into the bin directory of ASP.Net projects. The method calls are made very simple... so you have minimal work to integrate.

Robert Fuess