News for hackers. Stuff that matters.

Ask HN: Timezone in web apps

I'd like to share opinions on best practices for dealing with timezones in web apps, both at infrastructure level (how to set up servers) and at application level (server- and client-side), especially distinguishing between registered users and guests.

To make it short, my final solution is:

- store all times in the db in UTC

- display relative timings (e.g., 6m ago) in s/m/h when difference is less than 1 day

- display date (e.g., May 6) when difference is greater than 1 day, adding time only if the user saved his timezone (in particular no time for guests)

- possibly correct the time displayed via js if client-side detection works

At infrastructure level I believe that storing all times in UTC saves a lot of mental sanity. :) Modern dbms support automatic conversion from server local time to UTC (e.g., timestamp data type in mysql). Or one can configure all servers on UTC and be more flexible on data type (e.g., in mysql time/datetime would be ok).

Personally I prefer the second one. However, I've noticed that developing/debugging on my machine (which is not on UTC) may generate confusion.

Moving to application level, the most clever solution is probably to display relative timings (e.g., 6m ago) which is a good solution to avoid the problem :) However for some content, e.g. news/events, it may still be relevant to display the full time… so for the sake of completeness let's discuss it.

To the best of my knowledge from the http request there is no way to know users' timezone. So the only purely-server-side solution I see is to ask users for their timezone and the poor guests can't have timing information.

Adding client-side support, the best open source solution seems to be: https://bitbucket.org/pellepim/jstimezonedetect (there exist also jquery-plugins or other stuffs but all essentially based on this)

With such a tool one can update time information directly at client side. Many solutions I've found on the web display an html tag with time in UTC and translate it in local time via js. This way to do is not perfect when js is not working. It's probably better to use a tag that displays a relative time information and has an attribute with UTC time for correcting the content to local time (this is implemented, e.g., by Facebook).

Finally, it would be useful/nice to use client-side detection to instruct the server. In principle, the detection could be run async immediately after the js code is loaded, and the page content could be already provided by the server with correct time information (imagine the fb page is downloaded with no timeline data, the timezone detected and sent to the server, then the timeline data loaded via ajax with time properly set). However I'm not aware of any implementations and I'm wondering if the result is worth the effort.

( 4 comments | 7 points | on HN )
Re:Ask HN: Timezone in web apps by mryan on Sunday May 06, 2012 @05:11PM (#3936334)
At the infrastructure: yes, all servers should be UTC, (almost) no question.

As for guessing timezones - I use GeoIP to get the user's approximate location, then figure out the timezone based on that. It is not perfect, but I needed something that does not require JS.

Re:Ask HN: Timezone in web apps by zmillman on Sunday May 06, 2012 @07:01PM (#3936706)
If you don't need the user's timezone immediately for their first request (which I assume is why you can't use a javascript solution), using getTimezoneOffset() works pretty well.

Shameless plug: I wrote a gem to handle this in Rails ( https://github.com/zmillman/browser-timezone )

Re:Ask HN: Timezone in web apps by ecesena on Sunday May 06, 2012 @05:58PM (#3936478)
right, geoIP is another interesting solution... the best is to combine several guessing techniques, when available
Re:Ask HN: Timezone in web apps by jmkacz on Sunday May 06, 2012 @05:46PM (#3936440)
There's a potential monkey wrench with storing all times, in the database, as UTC: daylight saving time. If you have, say recurring events, or an event in the future, you need to take into account whether the DST state matches for today and said date. One could account for this before storing the time, but the rules might change on you, as happened a few years ago, here in the states.