{"id":22,"date":"2007-09-19T03:34:20","date_gmt":"2007-09-19T00:34:20","guid":{"rendered":"http:\/\/www.held.org.il\/blog\/?p=22"},"modified":"2007-09-19T03:34:20","modified_gmt":"2007-09-19T00:34:20","slug":"time-zones-how-to-do-it-right","status":"publish","type":"post","link":"http:\/\/www.held.org.il\/blog\/2007\/09\/time-zones-how-to-do-it-right\/","title":{"rendered":"Time zones &#8211; how to do it right"},"content":{"rendered":"<p>After some struggling with how the <a title=\"Just the wikipedia article\" href=\"http:\/\/en.wikipedia.org\/wiki\/Daylight_Saving_Time\">Daylight Saving Time<\/a> 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:<\/p>\n<ul>\n<li><strong>Absolute time: <\/strong>Store dates only relative to <a href=\"http:\/\/en.wikipedia.org\/wiki\/UTC\">UTC<\/a> (usually [milli]seconds since <a href=\"http:\/\/en.wikipedia.org\/wiki\/Unix_epoch\">Unix epoch<\/a> or another reference date. The familiar Timestamps are measured as UTC and not as Local Time!<\/li>\n<li><strong>Absolute-to-Local: <\/strong>When <strong>local <\/strong>date matters, convert UTC-to-local (and vice versa)  using the already-existing methods:\n<ul>\n<li>PHP: <a href=\"http:\/\/www.php.net\/date\">date()<\/a> and many, many other <a href=\"http:\/\/www.php.net\/DateTime\">PHP date functions<\/a><\/li>\n<li>JavaScript: <a href=\"http:\/\/w3schools.com\/jsref\/jsref_obj_date.asp\">Date object methods<\/a><\/li>\n<li>Libc and Perl: <a href=\"http:\/\/linux.die.net\/man\/3\/localtime\">localtime()<\/a> and friends<\/li>\n<li>MFC: <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/78zb0ese(VS.80).aspx\">CTime object<\/a> (Don't trust me too much for Windows tips though)<\/li>\n<\/ul>\n<\/li>\n<li><strong>Absolute-to-somewhere-on-the-globe:<\/strong> instead of the OS-defined <strong>local<\/strong> time zone settings, PHP seems to <a href=\"http:\/\/www.php.net\/manual\/en\/function.date-default-timezone-set.php\">have a way<\/a>, but for JavaScript I haven't found anything unfortunately.<\/li>\n<\/ul>\n<p>Bottom line: stick to UTC as much as you can, it's good for you.<\/p>\n<p>Trivia line: Morocco is GMT+0 and has no Daylight Saving Time (so it's identical to UTC). Good for them, eh?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After some struggling with how the Daylight Saving Time change affected my code, I realized that one shouldn&#8217;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 &#8211; and probably better (yes, including DST changes, works flawlessly!). Here are [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[66,87,91,202],"_links":{"self":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/22"}],"collection":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":0,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}