<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Laughing Meme &#187; timezone</title>
	<atom:link href="http://laughingmeme.org/tag/timezone/feed/" rel="self" type="application/rss+xml" />
	<link>http://laughingmeme.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 02 Apr 2012 20:12:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A literary appreciation of the Olson/Zoneinfo/tz database</title>
		<link>http://laughingmeme.org/2009/10/25/a-literary-appreciation-of-the-olsonzoneinfotz-database/</link>
		<comments>http://laughingmeme.org/2009/10/25/a-literary-appreciation-of-the-olsonzoneinfotz-database/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 22:28:08 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[Aside]]></category>
		<category><![CDATA[calendaring]]></category>
		<category><![CDATA[olson]]></category>
		<category><![CDATA[timezone]]></category>
		<category><![CDATA[udell]]></category>

		<guid isPermaLink="false">http://laughingmeme.org/?p=4334</guid>
		<description><![CDATA[I&#8217;m a card carrying member of the Olson fan club, but it&#8217;s still great reading see Jon&#8217;s love note. ADO has a posse. (actually I only have 5 posts tagged olson, while I have 7 posts tagged udell )]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a card carrying member of the Olson fan club, but it&#8217;s still great reading see Jon&#8217;s love note. <acronym title="Arthur David Olson">ADO</acronym> has a posse.  (actually I only have 5 posts tagged <a href="http://laughingmeme.org/tag/olson/">olson</a>, while I have 7 posts tagged <a href="http://laughingmeme.org/tag/udell/">udell</a> )</p>
<p><a href='http://blog.jonudell.net/2009/10/23/a-literary-appreciation-of-the-olsonzoneinfotz-database/'>http://blog.jonudell.net/2009/10/23/a-literary-appreciation-of-the-olsonzoneinfotz-database/</a></p>]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2009/10/25/a-literary-appreciation-of-the-olsonzoneinfotz-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looking at PHP5&#8242;s DateTime and DateTimeZone</title>
		<link>http://laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/</link>
		<comments>http://laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/#comments</comments>
		<pubDate>Wed, 28 Feb 2007 00:47:09 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[calendaring]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[Longer]]></category>
		<category><![CDATA[olson]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/</guid>
		<description><![CDATA[Looking over the PHP5.2 changelog I noticed that somewhere along the way PHP5 seems to have picked up a provocatively named pair of classes, DateTime and DateTimeZone. There is something fundamentally brash, brazen even, to releasing a class named DateTime. As a calendar geek I imagine upon seeing &#8220;new DateTime()&#8221; I feel something akin to [...]]]></description>
			<content:encoded><![CDATA[<p>Looking over the <a href="http://www.php.net/ChangeLog-5.php">PHP5.2 changelog</a> I noticed that somewhere along the way PHP5 seems to have picked up a provocatively named pair of classes, DateTime and DateTimeZone.  </p>

<p>There is something fundamentally brash, brazen even, to releasing a class named DateTime.  As a calendar geek I imagine upon seeing &#8220;new DateTime()&#8221; I feel something akin to what an old thespian feels when they see a company putting on a production of the Scottish play &#8212; it&#8217;s a decidedly mixed emotion.  But I&#8217;m going to bump my way through learning how to use this new DateTime lib, bringing <strong>all my preconceptions</strong> about how it should work.  The odds of this being interesting to you is probably nil unless you&#8217;re in one or two very small cliques, feel free to read on, or browse away.</p>

<p>I&#8217;m primarily working in PHP4 right now, so my first step was to grab a copy of <a href="http://www.mamp.info/en/home.php">MAMP 1.5b</a> getting me a nice PHP5.2 sandbox to play with.</p>

<p>The new objects are <a href="http://www.php.net/manual/en/ref.datetime.php">documented here</a>, apparently there are functional equivalents for each of the object methods, and they use the <a href="http://pecl.php.net/package/timezonedb/">PECL timezomedb</a>.  </p>

<p>Hey!  timezonedb!  First fence cleared!  A timezone database compiled into a native format based on Olson is the <em>one true solution</em>, and I can update it independently, the most recent release being based on 2007b.  Sweet.  </p>

<p><a href="http://php.net/manual/en/function.date-create.php">Constructor</a> takes an initialization string that it passes to <a href="http://php.net/manual/en/function.strtotime.php">strtotime()</a>, and an optional DateTimeZone obj.  Defaults to &#8220;now&#8221;</p>

<pre><code>$date = new DateTime();
echo $date . "\n";
&gt; Object of class DateTime could not be converted to string 
</code></pre>

<p>Oops, no <code>__toString()</code> method defined.  You&#8217;ll need to use the <code>format()</code> instance method.  If you end up using the DateTime objects, you&#8217;ll be seeing a lot of <code>format()</code>, more on that in a bit.  </p>

<p><code>format()</code> uses the <a href="http://php.net/manual/en/function.date.php">date() formatting strings</a> (not the <code>strftime</code> format strings).  Also takes a number of useful constants, most usefully your pal and mine RFC3339 (aka W3CDTF aka Dublin Core/Atom date format).</p>

<pre><code>echo $date-&gt;format(DATE_RFC3339) . "\n";
&gt; 2007-02-22T15:23:47-05:00
</code></pre>

<p>Note: thats a constant, if you pass in the string &#8216;DATE_RFC3339&#8242;, and you&#8217;ll get odd looking results.</p>

<p>Here we can see the default constructor sets both the time and a timezone &#8212; correctly, for the moment, identifying my timezone as <code>America/New_York</code>.   That&#8217;s somewhat contentious behaviour, some people will tell you that dates with unspecified timezones should either be in UTC or be &#8220;floating&#8221;, divorced from any timezone.  Why?  At least in part because across platforms and boxes timezone guessing is going to be non-deterministic &#8212; the script that worked when you ran it locally on your Mac laptop in New York, might fail on your ISP&#8217;s servers.  You get a hint of this reading over the timezone guessing rules on <a href="http://php.net/date_default_timezone_get"><code>date_default_timezone_get</code></a>.  There is also the fact that I&#8217;m currently moving at about 400mph and will be in a different timezone real soon now.  However you can set the default to something reasonable in a script, or in the php.ini.  (consider this my recommendation)</p>

<pre><code>date_default_timezone_set('UTC');
$date = new DateTime();
echo $date-&gt;format(DATE_RFC3339);
&gt; 2007-02-22T20:44:49+00:00
</code></pre>

<p>Yay, that worked.  Okay, but lets display that datetime in the local timezone.  (after all the <em>point</em> of this entire exercise will be the ability to work painlessly in multiple timezones).</p>

<pre><code>$date-&gt;setTimezone('America/New_York');
&gt; DateTime::setTimezone() expects parameter 1 to be DateTimeZone
</code></pre>

<p>Siiiigh.  Not smart enough to cast strings into TimeZone objects (holds true for the constructor as well, so no <code>new DateTime('now', 'UTC'))</code>.  Now its time to learn how to use DateTimeZone.</p>

<h3>Working with DateTimeZone, All Hail Olson</h3>

<p>I mentioned briefly earlier that PHP is now shipping with an extension timezonedb, which is a compiled version of the <a href="http://en.wikipedia.org/wiki/Zoneinfo">Olson database</a>.  The Olson database is a massive, largely volunteer effort to catalog the various timezones both in use, and those that have been in the past.  Time is a political issue, <a href="http://laughingmeme.org/2005/04/04/daylight-saving-sucks-war-time/">particularly day light savings</a>, and as such the rules governing it are arbitrary, whimsical, and subject to frequent change.  (p.s. gotten a panicked memo yet about new daylight savings compliance for March 11th?  No?  Where did you say you worked?)</p>

<p>Note: Olson also uses a longer form of the zone names then we usually see in the U.S., this is to combat ambiguity.  See <a href="http://www.php.net/manual/en/timezones.php">Appendix H</a> for a list of timezone names, including some <a href="http://www.php.net/manual/en/timezones.others.php">handy shortcuts</a>.</p>

<pre><code>$tz = new DateTimeZone('America/New_York');
$date-&gt;setTimezone($tz);
echo $date-&gt;format(DATE_RFC3339) . "\n";
&gt; 2007-02-22T16:02:55-05:00
</code></pre>

<p>This is starting to get long winded, but, hey, PHP5 supports object dereferencing on returns.  Maybe this will work.</p>

<pre><code>echo $date-&gt;setTimezone($tz)-&gt;format(DATE_RFC3339) . "\n";
&gt;  Call to a member function format() on a non-object
</code></pre>

<p>Nope. Oh well.</p>

<h3>Date vs Datetime?</h3>

<p>Say I&#8217;ve got a nice platonic date, say November 11th.  There is no time element associated with this, so timezones are kind of irrelevant.  I mean Nov. 11th <em>starts</em> at different times through out the world, but Nov. 11th is universal. (as long as you&#8217;re using the same version of Gregorian as most of the rest of us)  Ideally this date would float above timezone issues, but that isn&#8217;t how PHP does it, 2007-11-11 is treated internally as midnight on the 11th, which is certainly simpler, but disappointing.  You can prove this like so:</p>

<pre><code>$date = new DateTime('2007-11-11');
$date-&gt;setTimeZone($tz);
echo $date-&gt;format(DATE_RFC3339) . "\n";
&gt; 2007-11-10T19:00:00-05:00
</code></pre>

<p>The other useful DateTimeZone method is getOffest() </p>

<pre><code>echo $tz-&gt;getOffset($date); 
&gt; -18000
</code></pre>

<h3>Daylight Saving, March 11th, and Why Programmers Are a Grouchy Lot</h3>

<p>Note: <code>getOffset</code>, which returns a timezone&#8217;s offset in seconds from UTC, takes a DateTime obj because offsets can be date sensitive due to daylight savings.  Really without daylight saving this stuff would all be pretty straightforward.    Let&#8217;s test to make sure the offsets are correct at the boundary.</p>

<pre><code>echo $tz_nyc-&gt;getOffset(new DateTime('2007-03-11 1:00')) . "\n";
echo $tz_nyc-&gt;getOffset(new DateTime('2007-03-11 2:00')) . "\n";
&gt; -18000
&gt; -14400

(-18000/(60*60) == -5 hours) 
(-14400/(60*60) == -4 hours) 
</code></pre>

<p><strong>Yay!</strong> They got the memo about <a href="http://en.wikipedia.org/wiki/Energy_Policy_Act_of_2005">U.S. Energy Policy Act of 2005</a>.</p>

<h3>The Basics:  Accessors and Mutators</h3>

<p>So what are some other basic desires?</p>

<p>Get epoch seconds!  Except for their kind of limited range epoch seconds are great, and have helped a generation of programmers put off worrying about timezones as long as possible.  They&#8217;re also the backbone of PHP&#8217;s traditional date/time methods.</p>

<p>Alas, there isn&#8217;t an accessor method for getting epoch seconds, you&#8217;ll have to use <code>format()</code>. </p>

<p>In fact DateTime doesn&#8217;t expose <strong>any</strong> of the accessors you&#8217;d expect, so you&#8217;ll be using <code>format</code> a <strong>lot</strong> if you want to access pieces of your date. (for you know, display purposes, or manipulation, or building queries, or pretty much doing anything you&#8217;d want to do with a date)</p>

<p>examples of the <code>format()</code> as all purpose accessor pattern:</p>

<pre><code>epoch:  $date-&gt;format('U'); // 1173596400
year:   $date-&gt;format('Y'); // 2007
month:  $date-&gt;format('n'); // 3
day:    $date-&gt;format('j'); // 11
dow:    $date-&gt;format('l'); // Sunday
</code></pre>

<p>&#8230; etc &#8230;</p>

<p>So now you have accessors for the full range of <a href="http://php.net/manual/en/function.date.php">date() formatting strings</a>.  You just have to jump through a hope.</p>

<p>Pretty much the only accessor is <code>getTimezone()</code></p>

<pre><code>echo $date-&gt;getTimeZone();   // hope springs eternal!
&gt; Object of class DateTimeZone could not be converted to string
echo $date-&gt;getTimeZone()-&gt;getName() . "\n";
&gt; America/New_York
</code></pre>

<h3>Mutators</h3>

<p>Speaking of accessors, DateTime is a little sparse on mutators as well: <code>setTime()</code>, <code>setDate()</code>, and the mysteriously named <code>setISODate()</code>.</p>

<pre><code>$date-&gt;setDate('2007', '1', '1')-&gt;format(DATE_RFC3339);  // who am I kidding?
&gt; Call to a member function format() on a non-object 
$date-&gt; setDate('2007', '1', '1');
echo $date-&gt;format(DATE_RFC3339) . "\n";
&gt; 2007-01-01T02:00:00-05:00
</code></pre>

<p>Now what if I want to set just the day?</p>

<p>Maybe</p>

<pre><code>$date-&gt; setDate(null, null, '11');
echo $date-&gt;format(DATE_RFC3339) . "\n";
&gt; -001-12-11T02:00:00-05:00
</code></pre>

<p>Nope.  </p>

<p>Instead you&#8217;ll need to pull out the year and month (using our <code>format()</code> accessors) and pass those back in just to set the day.</p>

<pre><code>$date-&gt; setDate('2007', '1', '1');   // jan 1.  
$date-&gt;setDate($date-&gt;format('Y'), $date-&gt;format('n'), 11);
echo $date-&gt;format(DATE_RFC3339) . "\n"; 
</code></pre>

<p>Clunky.  </p>

<p><code>setTime()</code> works the same, but for time.  </p>

<p>e.g. Setting just the minutes, 33 minutes past, but keep hours, and seconds constant:</p>

<pre><code>$date-&gt;setTime($date-&gt;format('G'), 33, $date-&gt;format('s'));
echo $date-&gt;format(DATE_RFC3339) . "\n"; 
&gt; 2007-01-11T02:33:00-05:00
</code></pre>

<p>So what is an ISODate?  I&#8217;m unclear, and so is PHP&#8217;s documentation.  The docs show the call signature taking a $year, $week, and optional $day, while the description talks about $year, $month, $day.  Looking at the code looks like $week is the proper call, $month is cut and paste error from <code>setDate()</code>.  So I guess this is a method for setting day by the &#8220;week of the year&#8221; a concept more popular in Europe then in the US.  Not sure what ISO has to do with it.   So what is our current week of the year?</p>

<pre><code>echo $date-&gt;format('N') . "\n";  // 'N' is new in 5.1.0
&gt; 4 
</code></pre>

<p>Jan 11th was in the 4th week of the 2007?  Go figure.  </p>

<pre><code>$date-&gt;setISODate(2007, 4, 5);  // fifth day of the 4th week?
echo $date-&gt;format(DATE_RFC3339) . "\n";
&gt; 2007-01-26T02:33:00-05:00
</code></pre>

<p>Um.  You know what?  You&#8217;re on your own with <code>setISODate</code>, sorry.</p>

<h3>Date Math: Adding and Subtracting Deltas aka $date->modify($str)</h3>

<p>PHP5 for better or worse has very limited operator overloading, so no $dt1 + $dt2 * $dt3 / $dt4.  Instead the primary method for doing math is <code>modify()</code></p>

<p>Thankfully PHP&#8217;s <code>strtotime()</code> method is a gem, and one of the things it handles is relative dates.  <code>strtotime()</code> + relative dates is the secret to doing math with PHP5&#8242;s DateTime.</p>

<p>Lets get a basic date to start with:</p>

<pre><code>$date = new DateTime('today');
echo $date-&gt;format(DATE_RFC3339) . "\n";
&gt; 2007-02-22T00:00:00+00:00
</code></pre>

<p><strong>Note:</strong> <code>modify()</code> is destructive. It changes the original datetime object (as the name suggests).  You&#8217;ll need to jump through some hopes to keep a copy of your original date.  More later.</p>

<p>Add/subtract N days:</p>

<pre><code>foreach (range(1,10) as $n) {
   $date-&gt;modify("+1 days");
   echo $date-&gt;format("Y-m-d") . "\n";
}

&gt; 2007-02-23
&gt; 2007-02-24
&gt; 2007-02-25
&gt; 2007-02-26
&gt; 2007-02-27
&gt; 2007-02-28
&gt; 2007-03-01
&gt; 2007-03-02
&gt; 2007-03-03
&gt; 2007-03-04

$date-&gt;modify("-10 days");
echo $date-&gt;format("Y-m-d") . "\n";

&gt; 2007-02-22

$date-&gt;modify("-1 month");
echo $date-&gt;format("Y-m-d") . "\n";
&gt; 2007-01-22
// or alternately
$date-&gt;modify("1 month ago");
echo $date-&gt;format("Y-m-d") . "\n";
&gt; 2006-12-22
</code></pre>

<h3>Cloning DateTime objects to work around <code>modify</code></h3>

<p>Of course you <strong>usually</strong> want to keep the original when doing date math, so <code>modify()</code>&#8216;s lack of idempotentce is annoying.  Lets say I&#8217;m building a SQL query to select events happening in the next 7 days.</p>

<p>In an <strong>ideal</strong> world the code would like this:</p>

<pre><code>$start = new DateTime('today');
$end = $start + 7;
echo "select between " . $start-&gt;format('Y-m-d') . " and " . $end-&gt;format('Y-m-d') . "\n";
</code></pre>

<p>The above of course is just a pipe dream.  But wouldn&#8217;t it be nice?</p>

<p>I&#8217;d settle for:</p>

<pre><code>$end = $start-&gt;calc("+7 days");
</code></pre>

<p>Or even:</p>

<pre><code>$end = $start-&gt;clone-&gt;modify('+7 days');
</code></pre>

<p>None of the above examples remotely work.  </p>

<p>Instead use:</p>

<pre><code>$start = new DateTime('today');
$end = clone $start;
$end-&gt;modify('7 days 3 minutes 42 seconds ago');
</code></pre>

<p>Now format our SQL query for our example:</p>

<pre><code>echo "select between " . $start-&gt;format(DATE_RFC3339) . " and " . $end-&gt;format(DATE_RFC3339) . "\n";
&gt; select between 2007-02-22T00:00:00+00:00 and 2007-02-14T23:56:18+00:00
</code></pre>

<p>Awkward, but it gets the job done.</p>

<p>At least the relative date format is super flexible and expressive.  As far as I know the closest thing to documentation is from the <a href="http://www.gnu.org/software/tar/manual/html_node/tar_109.html">GNU tar manual on date input formats</a>. (just like CVS)  Btw. if you ever want nightmares, take a look at the <code>scan</code> method in PHP&#8217;s parse_date.c and be thankful that isn&#8217;t your job to maintain <img src='http://laughingmeme.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<h3>Date Math: Comparison and Differences</h3>

<p>Beyond adding deltas (&#8220;+7 days&#8221;), the other common date math is comparing two datetimes, to find out which is more recent, and getting the difference between them.  DateTime supports no methods for comparing two datetimes.  The simplest solution for doing comparison is to compare epoch seconds.</p>

<p><strong>Note:</strong>  This method only works for dates that can be represented by epoch seconds. PHP uses a signed int for epoch seconds, so the range is limited by the size of the max int on your platform.  Generally you get approximately 138 years, 1901 to 2038.  There are other schemes besides epoch seconds for mapping dates to an easily comparable number; <a href="http://en.wikipedia.org/wiki/Julian_day">MJDs</a>, and <a href="http://cr.yp.to/proto/utctai.html">Tai time</a> being two.  See also <em>Rheingold &amp; Dershowitz 1997</em></p>

<pre><code>$d1 = new DateTime("today");
$d2 = new DateTime("tomorrow");
if ($d1-&gt;format('U') &lt; $d2-&gt;format('U')) {
   echo "true\n";
} 
&gt; true
</code></pre>

<p>If you&#8217;re going to be comparing a large number of dates you might consider a memoization technique like the <a href="http://en.wikipedia.org/wiki/Schwartzian_transform">Schwartzian transform</a>.</p>

<p>We can get the difference in seconds using the same hack of casting to epochs.</p>

<pre><code>echo $d2-&gt;format('U') - $d1-&gt;format('U') . "\n";
&gt; 86400
</code></pre>

<p>Ideally we&#8217;d then divide the difference seconds to get the difference in hours, days, weeks, or months.  However the following naive solution <strong>won&#8217;t</strong> work.  </p>

<pre><code>$diff / (60*60*24);  // calculate difference in days, **BADLY**
</code></pre>

<p>Why not?  Because days don&#8217;t always have 24 hours.  Sometimes they have 23 hours, sometimes they have 25.  Daylight saving strikes again.  (If you want to be even more pedantic, minutes are also not 60 seconds long, sometimes they&#8217;re 61 seconds long if we have a leap second)</p>

<p>Basically you need to break yourself of thinking of datetime units as being fungible.  You can&#8217;t simply calculate minutes from seconds, or days from hours.  Just like you can&#8217;t divide days by 30 to get an accurate number of months.  There are solutions, but they&#8217;re a bit beyond this blog post.</p>

<h3>new DateTime from Epoch Seconds</h3>

<p>So, <strong>non-fungible</strong>, remember that.</p>

<p>But sometimes you&#8217;ve cast DateTimes down to epochs to do math.  And then you&#8217;ll want to cast back to a DateTime.  </p>

<p>Alas DateTime doesn&#8217;t have a constructor that takes an epoch, and passing a epoch to the default constructor will throw an exception, rather you want:</p>

<pre><code>$from_epoch = new DateTime(date('c', '-568080000'));
echo $from_epoch-&gt;format('Y-m-d') . "\n";
&gt; 1952-01-01   // expected result
</code></pre>

<h3>Conclusions</h3>

<p>DateTime/DateTimeZone get timezones right, and for solving that hard problem they deserve all possibles accolades.  </p>

<p>The rest of the API however is kind of simplistic, awkward to work with, and verbose.</p>

<p>Single most useful change: have DateTime methods actually return the object making it possible to use a slightly more abbreviated calls.</p>

<p>I had thought about writing up a few more recipes, like nth dow of the month, and such.  But we were coming in for descent, and it was time to be done.  Might happen in the future.  </p>

<p>Also if anyone has any power to <strong>enhance</strong> the DateTime object, I hope some of the above can act as a road map for a more expressive and powerful core library.  Or ping me, happy to chat.</p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>The More Things Change</title>
		<link>http://laughingmeme.org/2006/04/03/the-more-things-change/</link>
		<comments>http://laughingmeme.org/2006/04/03/the-more-things-change/#comments</comments>
		<pubDate>Mon, 03 Apr 2006 23:54:00 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[anyday]]></category>
		<category><![CDATA[calendaring]]></category>
		<category><![CDATA[dst]]></category>
		<category><![CDATA[palm]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=3311</guid>
		<description><![CDATA[Corporate culture is a funny thing, certain ideas get ingrained, others get discarded, practices are repeated forever even after everyone who remembers why they were put in places have been laid off, or cashed out. But what I can&#8217;t understand is why, dear god why, Palm, ostensibly a company with calendaring as a core competency, [...]]]></description>
			<content:encoded><![CDATA[<p>Corporate culture is a funny thing, certain ideas get ingrained, others get discarded, practices are repeated forever even after everyone who remembers why they were put in places have been laid off, or cashed out. </p>

<p>But what I can&#8217;t understand is why, <em>dear god why</em>, Palm, ostensibly a company with calendaring as a core competency, has never in its long history been able to get timezones right.  Several operating systems (not to mention web properties) later, they&#8217;re <em>still</em> <a href="http://www.everythingtreo.com/news/hardware/palm-treo-700w-has-daylight-savings-time-bug-20060402249/">screwing them up</a>.</p>

<p>I remember sitting in a conversation at Anyday post-acquisition discussing whether we should break timezone support to make it work work like Palm Desktop.</p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2006/04/03/the-more-things-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dealing with the Time Change</title>
		<link>http://laughingmeme.org/2005/11/01/dealing-with-the-time-change/</link>
		<comments>http://laughingmeme.org/2005/11/01/dealing-with-the-time-change/#comments</comments>
		<pubDate>Tue, 01 Nov 2005 17:09:00 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[calendaring]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[st]]></category>
		<category><![CDATA[timezone]]></category>
		<category><![CDATA[winter]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=3076</guid>
		<description><![CDATA[Les and Russell are talking about how much the lack of light this time of year sucks. (Russell living in the Bay Area has got a lot of nerve, but whatever) Here is my strategy. Rise early. No really, keep your body on daylight savings time as long as possible whatever it takes (cutting the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://decafbad.com/blog/2005/10/31/sundown">Les</a> and <a href="http://www.russellbeattie.com/notebook/1008668.html">Russell</a> are talking about how much the lack of light this time of year sucks.  (Russell living in the Bay Area has got a lot of nerve, but whatever)  Here is my strategy.</p>

<p>Rise early.  No really, keep your body on daylight savings time as long as possible whatever it takes (cutting the caffeine at 3, night caps, exercise) but get to bed early, and get up to catch that early morning light.  Once you&#8217;re up bathe in it, go for walks, open all the curtains, you get the idea.</p>

<p>Then in the afternoon, <strong>while its still light out</strong> pull the curtains, and turn on all the lights.  Cut off all contact with the outside world. (helps if your office is a remodeled closet.)  Crank the music up, turn the screen brightness up, and try to ignore the outside world for as long as possible.  </p>

<p>Doing this I can generally get a full days work in before the &#8220;I  can&#8217;t believe its dark already&#8221; blues kick in, and sap my will to live.</p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2005/11/01/dealing-with-the-time-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TZInfo &#8211; Ruby Timezone Library</title>
		<link>http://laughingmeme.org/2005/09/19/tzinfo-ruby-timezone-library/</link>
		<comments>http://laughingmeme.org/2005/09/19/tzinfo-ruby-timezone-library/#comments</comments>
		<pubDate>Mon, 19 Sep 2005 14:43:41 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[calendaring]]></category>
		<category><![CDATA[olson]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=1132</guid>
		<description><![CDATA[TZInfo is a Ruby library that uses the standard tz (Olson) database to provide daylight-savings aware transformations between times in different timezones. The tz database is compiled into Ruby classes which are packaged in the release. No external zoneinfo files are required at runtime. Sweet! Another item off the todo list I don&#8217;t have to [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
  <p><a href="http://tzinfo.rubyforge.org/">TZInfo</a> is a Ruby library that uses the standard tz (Olson) database to provide daylight-savings aware transformations between times in different timezones. The tz database is compiled into Ruby classes which are packaged in the release. No external zoneinfo files are required at runtime.</p>
</blockquote>

<p>Sweet!  Another item off the todo list I don&#8217;t have to do.  And Scott has written an article about <a href="http://lunchroom.lunchboxsoftware.com/pages/tzinfo_rails">how to use it as a replacement</a> for Rail&#8217;s <strong>&#8220;so-broken-its-negligent&#8221;</strong> <a href="http://rails.rubyonrails.com/classes/TimeZone.html">TimeZone</a> implementation.</p>

<p>As a hacker (is it a universal feeling?) its always an ambiguous feeling to see someone else cross an item off your todo list, a little sense of a loss (I was looking forward to solving that problem) with a bit of glee (Now I get to solve more interesting problems!)</p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2005/09/19/tzinfo-ruby-timezone-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use &#8220;zzz&#8221; [to designate timezone] for locations while uninhabited</title>
		<link>http://laughingmeme.org/2005/04/15/use-zzz-to-designate-timezone-for-locations-while-uninhabited/</link>
		<comments>http://laughingmeme.org/2005/04/15/use-zzz-to-designate-timezone-for-locations-while-uninhabited/#comments</comments>
		<pubDate>Fri, 15 Apr 2005 18:42:28 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[Aside]]></category>
		<category><![CDATA[calendaring]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=2913</guid>
		<description><![CDATA[The mnemonic is that these locations are, in some sense, asleep.]]></description>
			<content:encoded><![CDATA[<p>The mnemonic is that these locations are, in some sense, asleep.</p>
<p><a href='http://gatekeeper.dec.com/pub/BSD/NetBSD/NetBSD-current/src/lib/libc/time/Theory'>http://gatekeeper.dec.com/pub/BSD/NetBSD/NetBSD-current/src/lib/libc/time/Theory</a></p>]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2005/04/15/use-zzz-to-designate-timezone-for-locations-while-uninhabited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Daylight Saving Sucks &#124; War Time</title>
		<link>http://laughingmeme.org/2005/04/04/daylight-saving-sucks-war-time/</link>
		<comments>http://laughingmeme.org/2005/04/04/daylight-saving-sucks-war-time/#comments</comments>
		<pubDate>Mon, 04 Apr 2005 16:01:00 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[calendaring]]></category>
		<category><![CDATA[dst]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=1058</guid>
		<description><![CDATA[Grumph, arggh. I hate daylight saving time. That first Monday morning is so painful, your body wakes slowly, outraged at the duplicity of clocks everywhere. Sometimes known as &#8220;War Time&#8221; for its widespread adoption during WWI, its roots are clearly embedded in the imperial/capitalist agenda. Did you know that this morning is one of the [...]]]></description>
			<content:encoded><![CDATA[<p>Grumph, arggh.  I hate daylight saving time.  That first Monday morning is so painful, your body wakes slowly, outraged at the duplicity of clocks everywhere.  </p>

<p>Sometimes known as &#8220;War Time&#8221; for its widespread adoption during WWI, its roots are clearly embedded in the imperial/capitalist agenda.  Did you know that this morning is one of the most dangerous to drive?  The spike in severe traffic accidents the first 3 days after DST is shocking.  And its a key factor in why implementing a decent timezone library is such a <a href="http://thread.gmane.org/gmane.comp.lang.ruby.rails/6133">pain in the ass</a>.   Not worth it.</p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2005/04/04/daylight-saving-sucks-war-time/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Maintaining Date/Timezone Sanity with DateTime.pm and Class::DBI</title>
		<link>http://laughingmeme.org/2003/09/25/maintaining-datetimezone-sanity-with-datetimepm-and-classdbi/</link>
		<comments>http://laughingmeme.org/2003/09/25/maintaining-datetimezone-sanity-with-datetimepm-and-classdbi/#comments</comments>
		<pubDate>Thu, 25 Sep 2003 23:33:00 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[calendaring]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[Longer]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=601</guid>
		<description><![CDATA[Calendaring can be a fraught and tricky business. Probably doesn&#8217;t compare to building software to do precise robotic control of surgical tools (though I have a friend who worked for a summer putting Windows into operating rooms), or high volume real time transaction management, but its does have its pitfalls and tricks. The Case of [...]]]></description>
			<content:encoded><![CDATA[<p>
Calendaring can be a fraught and tricky business.  Probably doesn&#8217;t compare to building software to do precise robotic control of surgical tools (though I have a friend who worked for a summer putting Windows into operating rooms), or high volume real time transaction management, but its does have its pitfalls and tricks.
</p>

<p><p>
<h3>The Case of the Missing Timezone</h3></p>

<p>A classic mistake when one sets out to write calendar software, particularly web calendar software, is to ignore timezones.  &#8220;Now&#8221; is treated as the current time on the web server, new events are added to the database without reference to the timezone of their creator.  And amazingly, by and large this will work, for a while.  Often your first set of users will be in your timezone, or thereabouts, perhaps you aren&#8217;t displaying hours, so it&#8217;s just a few hours early in the morning, and late at night when people notice your calendar is displaying the wrong date.
</p>
<p>
Problems start to crop up as your audience becomes more international, or you start trying to add more time sensitive services.  So you try to retro-fit timezones, add them on.  And now you&#8217;re living in a world of pain.
</p>
<span id="more-601"></span>
<p>
<h3>A Not So Hypothetical Example</h3></p>

<p>Say you&#8217;re running a web calendar off a server in your closest in Massachusetts, in a timezone affectionately if rather unprecisely known as Eastern time.  You have a user in Sydney who has been happily adding events, timezone free.  Now if you wanted to display the correct date for a given event to your users in Sydney you need to calculate the offset between the two locations.  Simple enough?  Is MA observing day light savings?  Is Sydney?  How about when the event was added? Any one of those questions can be annoying to answer, getting all 3 right, and collated&#8230; well trust me, it gets ugly quick.
</p>
<p>
<h3>Yet Another Problem</h3></p>

<p>What happens when you move that server out of your closest to a colo off the coast of England?  All of a sudden you&#8217;ve switched from adding events in &#8220;Eastern&#8221; time to some British local time.  How do you know how to calculate the offsets now?  One solution I&#8217;ve seen (when I was working on a website for a certain very large PDA manufacturer who shall rename nameless) was to add a switch into the code:
<pre>
 if (date &lt; kDayWeMovedToColo) { ... } else { ... }
</pre>
</p>
<p>
This problem can bite you even if you were clever enough to get your users setup with timezones from day one.
</p>
<p>
<h3>A Simple Solution</h3></p>

<p>After all that hand wringing, and doom saying, you&#8217;ll be happy to know that the solution is simple.  Store all your dates in UTC (sometimes called GMT, sometimes called Zulu time); a zero offset time that doesn&#8217;t observe day light savings.  As added bonus a number of languages even support converting to GMT time, though many of them are broken. (PHP&#8217;s are, rumor has it so are C#&#8217;s, Java got it wrong for its first 3-4 versions)
</p>
<p>
<h3>Some Code!</h3></p>

<p>As Perl hackers we&#8217;ve got an advantage, we&#8217;ve got the best date/time library in any language I&#8217;ve ever seen, <a href="http://perl-date-time.sf.net">DateTime.pm</a>, which just happens to have the most complete TimeZone implementation you&#8217;ve ever dreamed of. (if you&#8217;re a calendar geek, and dream about this stuff)
</p>
<p>
And we&#8217;ve got Class::DBI, which as much as I&#8217;ve said some nasty things about it, has a few nice features in the db-to-object mapping sphere.
</p>
<p>
I use a simple adapter class to handle my conversions to and from GMT, but a more proficient CDBI wizard could probably replace my class with a couple of code refs.
</p>
<p>
In your Class::DBI definitions you would add some code like:</p>

<pre>

MyEvent->has_a(
   start_date => 'DateAdapater',
   inflate => sub { DateAdapter->inflate(shift) },
   deflate => 'deflate'
);

</pre>

<p>Where DateApapater looks like:</p>

<pre>

package DateAdapater;
use base qw(DateTime);
# this is a DateTime::Format::DBI
# which I haven't figured out how to combine with cdbi
#
use DateTime::Format::MySQL; 

sub inflate {
  my ($class, $value) = @_;
   my $dt = $class->parse($value);
   # starts in 'floating' tz
   $dt->set_time_zone('UTC');
   return bless $dt, $class;
}

sub deflate {
    my $dt = shift;
    # convert to UTC
    $dt->set_time_zone('UTC');
    return DateTime::Format::MySQL->format_datetime($dt);
}

sub parse {
    my ($class, $value) = @_;
    my $dt = eval { return DateTime::Format::MySQL->parse_datetime($value); };
    if ($dt) { return $dt; }
    else {
       return DateTime->new(year => 1970);
    }
}

</pre>

<p></p>
<p>
<h3>A Few Things to Note</h3></p>

<p>MySQL&#8217;s datetime fields don&#8217;t maintain a concept of timezone, so DT creates them with a floating timezone.  Therefore casting them to UTC doesn&#8217;t change their settings for year, month, hour, etc.  DateTime objects coming in will presumably be set in the user&#8217;s timezone, therefore casting them to UTC will change their year, month, hour, etc fields, but the object will still refer to the exact same instant in time.
</p>
<p>
Lastly, you know you&#8217;ve been hacking Unix dates too long when you&#8217;re idea of an error message is to return some day in 1970.
</p>
<p>
(Also it&#8217;s strikingly odd how much longer it takes to write a blog entry about some code, then to just write the code.  Noticed it for both this, and the timezone selector.   Need a code to blog adapter.)
</p></p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2003/09/25/maintaining-datetimezone-sanity-with-datetimepm-and-classdbi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Timezone Selector</title>
		<link>http://laughingmeme.org/2003/09/25/timezone-selector/</link>
		<comments>http://laughingmeme.org/2003/09/25/timezone-selector/#comments</comments>
		<pubDate>Thu, 25 Sep 2003 10:42:29 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[olson]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=599</guid>
		<description><![CDATA[Coded up a quick Javascript timezone selector this evening. Uses DateTime.pm to parse the Olson database and then spits out some a Javascript lib that allows you to drill down from region, to the appopiate regional city list. Doesn&#8217;t win many points in the interface, or information design department, does have the advantage of being [...]]]></description>
			<content:encoded><![CDATA[<p>
Coded up a quick Javascript timezone selector this evening.  Uses <a href="http://perl-date-time.sf.net">DateTime.pm</a> to parse the <a href="http://www.twinsun.com/tz/tz-link.htm">Olson database</a> and then spits out some a Javascript lib that allows you to drill down from region, to the appopiate regional city list.  Doesn&#8217;t win many points in the interface, or information design department, does have the advantage of being very, very complete.  None of this &#8220;EST&#8221;, or &#8220;PDT&#8221; junk.
</p>

<p><p>
<a href="/tz_select/example.html">See it in action</a>.  Download the <a href="/code/tz_select.tar.gz">tarball.</a>
</p></p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2003/09/25/timezone-selector/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

