
Ask HN: Timezone in web apps  - ecesena
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.<p>To make it short, my final solution is:<p>- store all times in the db in UTC<p>- display relative timings (e.g., 6m ago) in s/m/h when difference is less than 1 day<p>- 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)<p>- possibly correct the time displayed via js if client-side detection works<p>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).<p>Personally I prefer the second one. However, I've noticed that developing/debugging on my machine (which is not on UTC) may generate confusion.<p>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.<p>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.<p>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)<p>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).<p>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.
======
mryan
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.

~~~
zmillman
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> )

------
jmkacz
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.

