<?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>NewPush &#187; Epoch</title> <atom:link href="http://newpush.com/tag/epoch/feed/" rel="self" type="application/rss+xml" /><link>http://newpush.com</link> <description>Server Hosting, Data Warehouse Hosting, Collaboration</description> <lastBuildDate>Mon, 30 Jan 2012 15:40:45 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>DB2 Epoch Conversion</title><link>http://newpush.com/2008/12/db2-epoch-conversion/</link> <comments>http://newpush.com/2008/12/db2-epoch-conversion/#comments</comments> <pubDate>Mon, 22 Dec 2008 11:46:18 +0000</pubDate> <dc:creator>Balazs</dc:creator> <category><![CDATA[DB2]]></category> <category><![CDATA[Conversion]]></category> <category><![CDATA[DateTime]]></category> <category><![CDATA[Epoch]]></category> <category><![CDATA[IBM]]></category> <category><![CDATA[Unix]]></category><guid isPermaLink="false">http://www.wdream.com/archives/16</guid> <description><![CDATA[As we have progresses with our move from PostgreSQL to DB2, we discovered that DB2 doesn&#8217;t have internal Epoch functions to deal with the epoch time format.  Luckily, we are running DB2 on AIX, and most Unix tools are readily &#8230; <a href="http://newpush.com/2008/12/db2-epoch-conversion/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>As we have progresses with our move from PostgreSQL to DB2, we discovered that DB2 doesn&#8217;t have internal Epoch functions to deal with the epoch time format.  Luckily, we are running DB2 on AIX, and most Unix tools are readily available.  We tried many different variations, including writing internal Epoch conversion in SQL.  Most appeared too slow, so we ended up preprocessing the data externally.  The most useful page I found to work with Epoch was <a href="http://www.epochconverter.com/">Epoch Time Converter</a>.   There are perl specific examples here: <a href="http://www.epochconverter.com/epoch/functions-perl.php">Perl Epoch Routines</a></p><p>Here is how to create a DB2 compatible timestamp in Perl:</p><pre>use DateTime;
sub epoch_to_db2 {
($tsp) = @_; # receive one parameter: the epoch time
$dt = DateTime-&gt;from_epoch( epoch =&gt; $tsp );
$ymd = $dt-&gt;ymd; # 1974-11-30
$hms = $dt-&gt;hms('.'); # 13.30.00
$tsp = "$ymd-$hms"; # DB2 format YYYY-MM-DD-hh.mm.ss
return $tsp;
}</pre><p>Here are the key paragraphs from the referenced pages:</p><ul><li>What is epoch time?<br /> The <strong>Unix epoch</strong> (or <strong>Unix time</strong> or <strong>POSIX time</strong> or <strong>Unix timestamp</strong>) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds.<br /> The epoch timestamp 0 can be written in ISO 8601 format as: 1970-01-01T00:00:00Z. One epoch hour is 3600 seconds, one epoch day is 86400 seconds long, leap seconds are not calculated.<br /> Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).</p><table style="border-color: #eeeeee;" border="1"><tbody><tr><td><strong>Human readable time</strong></td><td align="right"><strong>Seconds</strong></td></tr><tr><td>1 minute</td><td align="right">60 seconds</td></tr><tr><td>1 hour</td><td align="right">3600 seconds</td></tr><tr><td>1 day</td><td align="right">86400 seconds</td></tr><tr><td>1 week</td><td align="right">604800 seconds</td></tr><tr><td>1 month (30.44 days)</td><td align="right">2629743 seconds</td></tr><tr><td>1 year (365.24 days)</td><td align="right">31556926 seconds</td></tr></tbody></table></li><li>How to get the current epoch time in &#8230;<br /></p><table border="0" cellspacing="0" cellpadding="2"><tbody><tr><td class="proglang">Perl</td><td class="progcode"><code>time</code></td></tr><tr><td class="proglang">PHP</td><td class="progcode"><code>time()</code></td></tr><tr><td class="proglang">Ruby</td><td class="progcode"><code>Time.now</code> (or <code>Time.new</code>). To display the epoch: <code>Time.now.to_i</code></td></tr><tr><td class="proglang">Python</td><td class="progcode"><code>import time</code> first, then <code>time.time()</code></td></tr><tr><td class="proglang">Java</td><td class="progcode"><code>long epoch = System.currentTimeMillis()/1000;</code></td></tr><tr><td class="proglang">Microsoft .NET C#</td><td class="progcode"><code>epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;</code></td></tr><tr><td class="proglang">VBScript/ASP</td><td class="progcode"><code>DateDiff("s", "01/01/1970 00:00:00", Now())</code></td></tr><tr><td class="proglang">MySQL</td><td class="progcode"><code>SELECT unix_timestamp(now())</code> <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html">More information</a></td></tr><tr><td class="proglang">PostgreSQL</td><td class="progcode"><code>SELECT extract(epoch FROM now());</code></td></tr><tr><td class="proglang">SQL Server</td><td class="progcode"><code>SELECT DATEDIFF(s, '19700101', GETDATE())</code></td></tr><tr><td class="proglang">JavaScript</td><td class="progcode"><code>Math.round(new Date().getTime()/1000.0)</code> getTime() returns time in milliseconds.</td></tr><tr><td class="proglang">Unix/Linux</td><td class="progcode"><code>date +%s</code></td></tr><tr><td class="proglang">Other OS&#8217;s</td><td class="progcode">Command line: <code>perl -e "print time"</code> (If Perl is installed on your system)</td></tr></tbody></table></li><li>Convert from human readable date to epoch<br /><table border="0" cellspacing="0" cellpadding="2"><tbody><tr><td class="proglang">Perl</td><td class="progcode">Use these <a href="http://www.epochconverter.com/epoch/functions-perl.php">Perl Epoch routines</a></td></tr><tr><td class="proglang">PHP</td><td class="progcode"><code>mktime(<em>hour</em>, <em>minute</em>, <em>second</em>, <em>month</em>, <em>day</em>, <em>year</em>)</code> <a rel="nofollow" href="http://www.php.net/mktime">More information</a></td></tr><tr><td class="proglang">Ruby</td><td class="progcode"><code>Time.local(<em>year</em>, <em>month</em>, <em>day</em>, <em>hour</em>, <em>minute</em>, <em>second</em>, <em>usec</em> )</code> (or <code>Time.gm</code> for GMT/UTC input). To display add <code>.to_i</code></td></tr><tr><td class="proglang">Python</td><td class="progcode"><code>import time</code> first, then <code>int(time.mktime(time.strptime('2000-01-01 12:34:00', '%Y-%m-%d %H:%M:%S')))</code></td></tr><tr><td class="proglang">Java</td><td class="progcode"><code>long epoch = new java.text.SimpleDateFormat ("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 01:00:00");</code></td></tr><tr><td class="proglang">VBScript/ASP</td><td class="progcode"><code>DateDiff("s", "01/01/1970 00:00:00", <em>time field</em>)</code> <a href="http://www.epochconverter.com/epoch/functions.php#asp">More information</a></td></tr><tr><td class="proglang">MySQL</td><td class="progcode"><code>SELECT unix_timestamp(<em>time</em>)</code> Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD<br /> <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html">More on using Epoch timestamps with MySQL</a></td></tr><tr><td class="proglang">PostgreSQL</td><td class="progcode"><code>SELECT extract(epoch FROM date('2000-01-01 12:34'));</code><br /> With timestamp: <code>SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');</code><br /> With interval: <code>SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');</code></td></tr><tr><td class="proglang">SQL Server</td><td class="progcode"><code>SELECT DATEDIFF(s, '19700101', <em>time field</em>)</code></td></tr><tr><td class="proglang">JavaScript</td><td class="progcode">use the <a href="http://www.epochconverter.com/epoch/functions.php#javascript">JavaScript Date object</a></td></tr><tr><td class="proglang">Unix/Linux</td><td class="progcode"><code>date +%s -d"Jan 1, 1980 00:00:01"</code></td></tr></tbody></table></li><li>Convert from epoch to human readable date<br /><table border="0" cellspacing="0" cellpadding="2"><tbody><tr><td class="proglang">Perl</td><td class="progcode">Use these <a href="http://www.epochconverter.com/epoch/functions-perl.php">Perl Epoch routines</a></td></tr><tr><td class="proglang">PHP</td><td class="progcode"><code>date(<em>output format</em>, <em>epoch</em>);</code> Output format example: &#8216;r&#8217; = RFC 2822 date <a rel="nofollow" href="http://nl2.php.net/date">More information</a></td></tr><tr><td class="proglang">Ruby</td><td class="progcode"><code>Time.at(<em>epoch</em>)</code></td></tr><tr><td class="proglang">Python</td><td class="progcode"><code>import time</code> first, then <code>time.gmtime(<em>epoch</em>)</code> time is an array of year, month, day, hour, min, sec, day of week, day of year, DST <a rel="nofollow" href="http://docs.python.org/lib/module-time.html">More information</a></td></tr><tr><td class="proglang">Java</td><td class="progcode"><code>String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date (<em>epoch</em>*1000));</code></td></tr><tr><td class="proglang">VBScript/ASP</td><td class="progcode"><code>DateAdd("s", <em>epoch</em>, "01/01/1970 00:00:00")</code> <a href="http://www.epochconverter.com/epoch/functions.php#asp">More information</a></td></tr><tr><td class="proglang">PostgreSQL</td><td class="progcode"><code>SELECT TIMESTAMP WITH TIME ZONE 'epoch' + <em>epoch</em> * INTERVAL '1 second';</code></td></tr><tr><td class="proglang">MySQL</td><td class="progcode"><code>from_unixtime(<em>epoch</em>, <em>optional output format</em>)</code> The default output format is YYY-MM-DD HH:MM:SS <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html">more &#8230;</a></td></tr><tr><td class="proglang">SQL Server</td><td class="progcode"><code>DATEADD(s, <em>epoch</em>, '19700101')</code></td></tr><tr><td class="proglang">Microsoft Excel</td><td class="progcode"><code>=(A1 / 86400) + 25569</code> Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number). For other timezones: =((A1 +/- timezone adjustment) / 86400) + 25569.</td></tr><tr><td class="proglang">JavaScript</td><td class="progcode">use the <a href="http://www.epochconverter.com/epoch/functions.php#javascript">JavaScript Date object</a></td></tr><tr><td class="proglang">Linux</td><td class="progcode"><code>date -d @1190000000</code> (replace 1190000000 with your epoch, needs newer version of date)</td></tr><tr><td class="proglang">Other OS&#8217;s</td><td class="progcode">Command line: <code>perl -e "print scalar(localtime(<em>epoch</em>))"</code> (If Perl is installed) Replace &#8216;localtime&#8217; with &#8216;gmtime&#8217; for GMT/UTC time.</td></tr></tbody></table></li><li>Converting from epoch to normal date in Perl<br /> Using the internal <strong>localtime</strong> or <strong>gmtime</strong> functions,<br /> localtime and gmtime return an array:</p><pre>my $time = time;	# or any other epoch timestamp
my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
my ($sec, $min, $hour, $day,$month,$year) = (localtime($time))[0,1,2,3,4,5,6];
# You can use 'gmtime' for GMT/UTC dates instead of 'localtime'
print "Unix time ".$time." converts to ".$months[$month]." ".$day.", ".($year+1900);
print " ".$hour.":".$min.":".$sec."n";</pre><p>But you can also use the scalar function to display your date with far less code:<br /> <code>print scalar localtime(946684800);</code><br /> returns <code>Sat Jan  1 01:00:00 2000</code> (in my timezone).</p><p>For more advanced date manipulation, try using the <strong>DataTime</strong> module:</p><pre>use DateTime;
$dt = DateTime-&gt;from_epoch( epoch =&gt; $epoch );
$year   = $dt-&gt;year;
$month  = $dt-&gt;month; # 1-12 - also mon
$day    = $dt-&gt;day; # 1-31 - also day_of_month, mday
$dow    = $dt-&gt;day_of_week; # 1-7 (Monday is 1) - also dow, wday
$hour   = $dt-&gt;hour; # 0-23
$minute = $dt-&gt;minute; # 0-59 - also min
$second = $dt-&gt;second; # 0-61 (leap seconds!) - also sec
$doy    = $dt-&gt;day_of_year; # 1-366 (leap years) - also doy
$doq    = $dt-&gt;day_of_quarter; # 1.. - also doq
$qtr    = $dt-&gt;quarter; # 1-4
$ymd    = $dt-&gt;ymd; # 1974-11-30
$ymd    = $dt-&gt;ymd('/'); # 1974/11/30 - also date
$hms    = $dt-&gt;hms; # 13:30:00
$hms    = $dt-&gt;hms('|'); # 13!30!00 - also time</pre></li></ul> ]]></content:encoded> <wfw:commentRss>http://newpush.com/2008/12/db2-epoch-conversion/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Served from: newpush.com @ 2012-02-09 22:10:38 by W3 Total Cache -->
