<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>The Dev Blog: A World Time Server In One Line Of Rails</title>
    <link>http://devblog.famundo.com/articles/2006/11/09/a-world-time-server-in-one-line-of-rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Putting Family Management on Rails!</description>
    <item>
      <title>A World Time Server In One Line Of Rails</title>
      <description>&lt;p&gt;First, sorry for the delay in posting more on the XMPP/Jabber serie. I was a bit busy. So until I write the next installment, here's a small piece of rails coolness.&lt;/p&gt;

&lt;p&gt;Want to know the exact time anywhere in the world? Including daylight saving taken into account? How about doing it in one line of rails code?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a new rails application: rails .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the tzinfo plugin: script/plugin install tzinfo_timezone&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edit app/controllers/application.rb&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add to it the following method (ok, it's 3 lines if we count the def and the end and not try to squeeze it into one with ; ):&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;get_time&lt;/span&gt;
   &lt;span class="ident"&gt;render&lt;/span&gt; &lt;span class="symbol"&gt;:text&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;TzinfoTimezone&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;params&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:id&lt;/span&gt;&lt;span class="punct"&gt;]].&lt;/span&gt;&lt;span class="ident"&gt;utc_to_local&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;Time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;now&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;getutc&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;strftime&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;%Y-%m-%d %H:%M:%S&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt; &lt;span class="keyword"&gt;rescue&lt;/span&gt; &lt;span class="ident"&gt;render&lt;/span&gt; &lt;span class="symbol"&gt;:text&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;ERROR - check your time zone&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;,&lt;/span&gt; &lt;span class="symbol"&gt;:status&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;500&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  

&lt;p&gt;Now run it: script/server&lt;/p&gt;

&lt;p&gt;Browse to: http://localhost:3000/application/get_time/Tokyo or http://localhost:3000/application/get_time/London. Check out the tzinfo plugin for the names of the supported time-zones. And you can add more to the mapping there.&lt;/p&gt;

&lt;p&gt;You can improve performance a bit by turning sessions off completely. Do that either in the configuration or by adding session :off to the application controller class.&lt;/p&gt;

&lt;p&gt;If you keep your computer clock accurate with ntp, you will get a pretty accurate time. The request is processed at a really high speed, so that shouldn't be a problem. And if the round trip to the server and back is quick, you will have a 1 second accuracy. Not bad for one line of code.&lt;/p&gt;</description>
      <pubDate>Thu, 09 Nov 2006 10:26:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:bdb7846b-6502-4637-b295-f9e33c9b219d</guid>
      <author>guy.naor@famundo.com (Guy Naor)</author>
      <link>http://devblog.famundo.com/articles/2006/11/09/a-world-time-server-in-one-line-of-rails</link>
      <category>Rails</category>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
