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!

In reply to Candle Time by Random_Walk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.