Tag Archives: programming

Time zones – how to do it right

After some struggling with how the Daylight Saving Time change affected my code, I realized that one shouldn't mess with the time zone offsets on his own (i.e. Add or Subtract hours manually from the UTC time), because underlying libs already implement it - and probably better (yes, including DST changes, works flawlessly!). Here are the new simple rules I set to myself:

  • Absolute time: Store dates only relative to UTC (usually [milli]seconds since Unix epoch or another reference date. The familiar Timestamps are measured as UTC and not as Local Time!
  • Absolute-to-Local: When local date matters, convert UTC-to-local (and vice versa) using the already-existing methods:
  • Absolute-to-somewhere-on-the-globe: instead of the OS-defined local time zone settings, PHP seems to have a way, but for JavaScript I haven't found anything unfortunately.

Bottom line: stick to UTC as much as you can, it's good for you.

Trivia line: Morocco is GMT+0 and has no Daylight Saving Time (so it's identical to UTC). Good for them, eh?