<?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>Made of Everything You&#039;re Not &#187; relative time</title>
	<atom:link href="http://blog.ericlamb.net/tag/relative-time/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ericlamb.net</link>
	<description>Thoughts on programming, people and life</description>
	<lastBuildDate>Thu, 27 Oct 2011 01:29:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Shifting Requirements = Better Coder</title>
		<link>http://blog.ericlamb.net/2009/03/shifting-requirements-equals-better-coder/</link>
		<comments>http://blog.ericlamb.net/2009/03/shifting-requirements-equals-better-coder/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 14:00:51 +0000</pubDate>
		<dc:creator>Eric Lamb</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[friendfeed]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[relative time]]></category>

		<guid isPermaLink="false">http://blog.ericlamb.net/?p=971</guid>
		<description><![CDATA[So often in the development process shifting requirements can wreak havoc on the entire project. But sometimes, every once in a while, if the moon is in just the right place and the wind is blowing in the proper direction, you can walk away a better coder for it. Take for example my recent experience [...]]]></description>
			<content:encoded><![CDATA[<p>So often in the development process shifting requirements can wreak havoc on the entire project. But sometimes, every once in a while, if the moon is in just the right place and the wind is blowing in the proper direction, you can walk away a better coder for it.</p>
<div id="attachment_998" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.ericlamb.net/wp-content/uploads/2009/03/rhino.jpg" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F03%2Frhino.jpg','Rhino')"><img src="http://blog.ericlamb.net/wp-content/uploads/2009/03/rhino-300x199.jpg" alt="Rhino" title="Rhino" width="300" height="199" class="size-medium wp-image-998" /></a><p class="wp-caption-text">Rhino</p></div>
<p>Take for example my recent experience integrating the FriendFeed API into a client&#8217;s site.</p>
<h3>Pop Quiz</h3>
<p>The client has a website that utilizes the FriendFeed API, on the homepage only, to display information about their online activities. Basically, they want the site to be updated automatically whenever they update their MySpace or YouTube pages and the like. They&#8217;ve specified the layout to be something like the below:</p>
<p>&#8220;ClientName&#8221; just published a piece titled &#8220;Title&#8221; on &#8220;Site&#8221;. Published &#8220;X hours ago&#8221;</p>
<p>Pretty simple right? An obvious design would go something like this:<br />
1. Connect to FriendFeed API.<br />
2. Make request for data.<br />
3. Display on the page.</p>
<p>The only issue is the formatting of the date / time difference. No worries though; there&#8217;s php code all over the place for that:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> RelativeTime<span style="color: #000;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
	<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$timestamp</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$periods</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;sec&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;min&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;hour&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;day&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;week&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;month&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;years&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;decade&quot;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$lengths</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;60&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;60&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;24&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;4.35&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;12&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #000088;">$difference</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span> <span style="color: #666666; font-style: italic;">// this was in the past</span>
		<span style="color: #000088;">$ending</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ago&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span> <span style="color: #666666; font-style: italic;">// this was in the future</span>
		<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #000088;">$difference</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ending</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;to go&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
	<span style="color: #22f;">for</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$difference</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$lengths</span><span style="color: #000;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #000;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
		<span style="color: #000088;">$difference</span> <span style="color: #339933;">/=</span> <span style="color: #000088;">$lengths</span><span style="color: #000;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #000;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$difference</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$difference</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">1</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
		<span style="color: #000088;">$periods</span><span style="color: #000;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #000;">&#93;</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$difference</span> <span style="color: #006699; font-weight: bold;">$periods</span>[<span style="color: #006699; font-weight: bold;">$j</span>] <span style="color: #006699; font-weight: bold;">$ending</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">return</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span></pre></div></div>

<p>Using the above you can just execute the output like the below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #22f;">echo</span> <span style="color: #000088;">$me</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' just published a piece titled '</span><span style="color: #339933;">.</span><span style="color: #000088;">$post_title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' on '</span><span style="color: #339933;">.</span><span style="color: #000088;">$site</span><span style="color: #339933;">;</span>
<span style="color: #22f;">echo</span> <span style="color: #0000ff;">' Published '</span><span style="color: #339933;">.</span>RelativeTime<span style="color: #000;">&#40;</span><span style="color: #000088;">$relative_time</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Feeling pretty proud of yourself you show it off to the Producer in charge of the program and they come back with, &#8220;Nice! But how will it hold up after caching is enabled?&#8221;.</p>
<p>Well, I hadn&#8217;t thought of that (my bad fully)&#8230;</p>
<h3>The Oh Shit Moment</h3>
<p>Turns out, they&#8217;re expecting a lot of traffic (or the website is pretty resource intensive; take your pick) so full page caching is going to be required.</p>
<p>The problem is the timestamp; since all output is going to be cached the generated time, &#8220;5 minutes ago&#8221; or &#8220;2 days ago&#8221;, etc, isn&#8217;t going to change until the cache expires. This is not good&#8230;</p>
<h3>How I Fixed This (The Wrong Way)</h3>
<p>The first thing I did was make the conclusion that if php couldn&#8217;t be used then the job of formatting the string is up to JavaScript. &#8220;OK&#8221;, I thought, &#8220;fine, I&#8217;ll just port the php function to JavaScript and go about my day.</p>
<p>Here&#8217;s what I came up with:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> getUnixTimestamp<span style="color: #000;">&#40;</span>timeObj<span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> dateObj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span>timeObj <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
		dateObj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #000;">&#40;</span>timeObj<span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span>
		dateObj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
	<span style="color: #22f;">return</span> <span style="color: #000;">&#40;</span>parseInt<span style="color: #000;">&#40;</span>dateObj.<span style="color: #660066;">getTime</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span>.<span style="color: #660066;">substring</span><span style="color: #000;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> RelativeTime<span style="color: #000;">&#40;</span>timestamp<span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
&nbsp;
	difference <span style="color: #339933;">=</span> getUnixTimestamp<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span> <span style="color: #339933;">-</span> timestamp<span style="color: #339933;">;</span>
	periodArr <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	periods <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;sec,min,hour,day,week,month,years,decade&quot;</span><span style="color: #339933;">;</span>
	periodArr <span style="color: #339933;">=</span> periods.<span style="color: #660066;">split</span><span style="color: #000;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	TimeLengths <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span>
	timelength <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;60,60,24,7,4.35,12,10&quot;</span><span style="color: #339933;">;</span>
	TimeLengths <span style="color: #339933;">=</span> timelength.<span style="color: #660066;">split</span><span style="color: #000;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span>difference <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span> <span style="color: #006600; font-style: italic;">// this was in the past</span>
		ending <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ago&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span> <span style="color: #006600; font-style: italic;">// this was in the future</span>
		difference <span style="color: #339933;">=</span> <span style="color: #339933;">-</span> difference<span style="color: #339933;">;</span>
		ending <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;to go&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
	<span style="color: #22f;">for</span><span style="color: #000;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> difference <span style="color: #339933;">&gt;=</span> TimeLengths<span style="color: #000;">&#91;</span>j<span style="color: #000;">&#93;</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
		difference <span style="color: #339933;">/=</span> TimeLengths<span style="color: #000;">&#91;</span>j<span style="color: #000;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
	difference <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #000;">&#40;</span>difference<span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span>difference <span style="color: #339933;">!=</span> <span style="color: #CC0000;">1</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
		periodArr<span style="color: #000;">&#91;</span>j<span style="color: #000;">&#93;</span> <span style="color: #339933;">=</span> periodArr<span style="color: #000;">&#91;</span>j<span style="color: #000;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;s&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
	text <span style="color: #339933;">=</span> difference<span style="color: #339933;">+</span><span style="color: #3366CC;">' '</span><span style="color: #339933;">+</span>periodArr<span style="color: #000;">&#91;</span>j<span style="color: #000;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' '</span><span style="color: #339933;">+</span>ending<span style="color: #339933;">;</span>
	document.<span style="color: #22f;">write</span><span style="color: #000;">&#40;</span>text<span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span></pre></div></div>

<p>Feeling sure of myself, <em>again</em>, a thought occurs to me that this still doesn&#8217;t fix the issue 100%. Sure, the date format will be consistent with the relative requirement, but the feed won&#8217;t be updated in &#8220;Real Time&#8221; so the full requirement isn&#8217;t there.</p>
<h3>The Real Solution</h3>
<p>A better idea would be to use AJAX to populate the area of the site where the blurb will be displayed. Since the AJAX request is independent of the rest of the site caching won&#8217;t be an issue. Sure, it&#8217;s a little more complicated but by doing an AJAX call the client gets exactly what they want.</p>
<h3>The Lesson</h3>
<p>Now, I&#8217;m not going to go into detail about the AJAX solution, mostly because I haven&#8217;t written it yet <img src='http://blog.ericlamb.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , but, to me, this is the far superior solution. </p>
<p>Admittedly, I was a little frustrated that I didn&#8217;t think of the AJAX solution sooner. That is, until I realized how much I had learned going in the &#8220;wrong&#8221; direction. </p>
<p>I had no idea that generating a UNIX timestamp in JavaScript was &#8220;complicated&#8221;.<br />
I learned a lot about JavaScript string handling.<br />
I got a quick primer in how JavaScript&#8217;s builtin Split function works.<br />
I got a little humility out of the process too.</p>
<p>Sometimes, it&#8217;s good to go astray on a project. </p>
<div><a class="addthis_button" href="http://blog.ericlamb.net//addthis.com/bookmark.php?v=250" addthis:url='http://blog.ericlamb.net/2009/03/shifting-requirements-equals-better-coder/' addthis:title='Shifting Requirements = Better Coder '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.ericlamb.net/2009/03/shifting-requirements-equals-better-coder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

