Dear Monks, I am having fun with timestamps
I am extracting a lot of data from a data warehouse and have to convert the legacy timestamps (see code) to human readable. Of course the requirement has changed and I now need epoch time too. This code runs on every row returned for a lot of data so I want to be a little efficient.
Any optimisations of the following code, or other ways to do it welcome.
use strict; use warnings;
use POSIX qw(mktime);
for my $t (<DATA>) {
# Handle time, this is stored as a 'Candle' timestamp
# "CYYMMDDhhmmssSSS" where C is the century (0 for 20th, 1 for 21st,
+ etc).
chomp ($t);
print $t;
my @bits = split //, $t; # use these later
# This is how I convert it to human readable now
my $c=substr $t,1,1,''; # grab the century
substr $t, 2,0,'-';substr $t, 5,0,'-';substr $t, 8,0,' '; # do the
+ date
substr $t,11,0,':';substr $t,14,0,':';substr $t,17,0,'.'; # do the t
+ime
$t = 19+$c.$t; # add back the century
print " = $t\n";
# This is what I am thinking to go to epoch seconds
# of course I would grab my human version from here too
my $C = $bits[0] + 19;
my $Y = $C . (join "", @bits[ 1,2]);
my $M = join "", @bits[ 3,4 ];
my $D = join "", @bits[ 5,6 ];
my $h = join "", @bits[ 7,8 ];
my $m = join "", @bits[ 9,10];
my $s = join "", @bits[11,12];
print "c:$C y:$Y M:$M D:$D h:$h m:$m s:$s\n";
my $epoch = mktime($s, $m, $h, $D, $M-1, $Y-1900,);
print scalar localtime $epoch;
print $/;
}
__DATA__
1130508154533613
1130508160033800
1130508161534113
1130508163034519
1130508164535019
1130508164535019
1130508170035191
1130508171533160
1130508173033660
1130508174534097
1130508180034535
1130424070116695
1130425070118790
1130426070118834
1130427070122888
1130428070123115
1130429070126161
1130430070127538
1130501070128195
1130502070131210
1130503070131969
1130504070135198
Cheers,
R.
Pereant, qui ante nos nostra dixerunt!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.