Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Candle Time
by hdb (Monsignor) on May 16, 2013 at 13:57 UTC | |
by Anonymous Monk on May 16, 2013 at 14:19 UTC | |
|
Re: Candle Time
by johngg (Canon) on May 16, 2013 at 21:24 UTC | |
by Random_Walk (Prior) on May 16, 2013 at 21:54 UTC | |
|
Re: Candle Time
by Anonymous Monk on May 16, 2013 at 14:12 UTC |