What time is it in Indiana?
June 4, 2007 12:31 PM   Subscribe

I would like to write a javascript function that takes the current time and adjusts it based on the timezone of any of our customers around the world. "Easy enough if I know the timezone" I thought......until I started to think about daylight savings time and all of its variations around the world. What is the best way to tackle this? Is there a handy script out there to do this, or should I be looking to call a webservice? I still don't know what time it is in Indiana.
posted by jasondigitized to Computers & Internet (14 answers total)
 
I'm pretty sure the Javascript Date object pulls the time from users' system clocks, so it should be correct for their timezone as is.

(I grew up in Indiana, and unless things have changed, they don't switch for daylight savings.)
posted by grumblebee at 12:35 PM on June 4, 2007


Response by poster: I will be performing the calculation on our computers, so the remote system clock cannot be used.
posted by jasondigitized at 12:45 PM on June 4, 2007


Javascript, if I'm not mistaken, is a client-side scripting language. Which is to say, you don't have a choice about where you're performing the calculation: if the script is on a page being served up in someone's browser, THEIR computer is executing the script, and THEIR computer is doing the calculation, so it'll be based on environment variables on their end.

If you want to do time zone calculation on your end, you'll have to user a server-side scripting language like perl or PHP.
posted by myrrh at 12:59 PM on June 4, 2007


I don't get it. Javascript is a client-side technology, not server-side. That's why it pulls from the users' system clocks, as grumblebee said.

Perhaps you want to make a Javascript function that gets the time from the user's system clock, then reports to you via something Ajaxy? You could then pull from an environment variable to compare to your local server time.
posted by adipocere at 1:00 PM on June 4, 2007


Things have changed in Indiana. All of the state does DST now, though some parts of the state are central and most of the state is eastern.

All the systems I've done let the user pick the timezone they would like to display their client in. So it doesn't matter where they are, they can pick whatever they want. The server stores all times in GMT and whenever you pull a time up, right before you display it to the user you convert from GMT to their timezone preference.
posted by cmm at 1:00 PM on June 4, 2007


How will the javascript run server-side?
posted by jammnrose at 1:00 PM on June 4, 2007


Javascript is just a programming language. You run it where you want to! Just because it's best known as "the thing in your web browser" does not make that the only place where javascript is used.

Java 6 SE even comes with a javascript implementation in it!
posted by cmm at 1:04 PM on June 4, 2007


Best answer: I think jasondigitized means he wants a live listing of client times on his computer. The client never sees it.

....so when he needs to call up Mr. X in Central Indiana, he knows it might not be a good time because they're on Moonlight Savings Time, making it 1 AM.

I think.
posted by niles at 1:14 PM on June 4, 2007


Best answer: Handling timezones is a pain in the arse. The pretty-much definitive source for handling timezones programatically is the tz database; hopefully there'll be a library available in whatever environment you're running this JavaScript that gives access to this database in an easy-to-use fashion.
posted by chrismear at 1:34 PM on June 4, 2007


Why not have some javascript run clientside and use an ajax function to send client time then?
posted by jammnrose at 1:35 PM on June 4, 2007


If you're on a web page, just store offset from GMT with your client and then pass that to the page so it knows how much to offset your current time to make it into client time.

It's 5pm in Indiana. I should go home!
posted by cmm at 2:00 PM on June 4, 2007


Response by poster: I understand the javascript is a client-side programming language. But that does not mean the the client has the right time. Maybe I should not have put 'javascript' in the question. I am just wondering how people handle this. For example, FogBugz or Basecamp logs tickets / requests, presumably using the server time as a time stamp. But the 'client' may be in a different time zone, and therefore the ticket / request timestamp needs to be adjusted based on the user's timezone. How is this done server side?
posted by jasondigitized at 2:21 PM on June 4, 2007


The timestamp is stored in the DB in UTC. Each user has a setting where they can set their timezone. Then, when rendering the page, they use a timezone database/library to convert the UTC time into the appropriate time for the current user's timezone. In Rails, for instance, you might use the TzTime plugin.
posted by chrismear at 2:24 PM on June 4, 2007


Best answer: This may help a little bit. What others have said is correct, always store UTC time in the database. You then can convert the UTC timestamp to a clients preferred timezone, or you could have a little bit of Javascript automagically convert UTC timestamps to the local timezone of the machine.

So instead of converting '1180999878' ("Mon Jun 4 23:31:18 UTC 2007") on the server into "Mon Jun 4 16:31:18 PDT 2007" for me, you could instead send something like:

[script type='javascript']
utc2local('1180999878');
[/script]

Where the utc2local() function would convert from UTC to the local timezone and document.write() the timestamp in the local timezone.

This has the advantage that it works even if the client has not set a timezone preference, it fails when the client would like to see time information in a timezone other than the timezone of the browser/machine (maybe while traveling).
posted by zengargoyle at 4:36 PM on June 4, 2007


« Older The best places to taste wine in and around Paso...   |   Why can't I have Fan - Low? Newer »
This thread is closed to new comments.