Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

change the time format into milisecond

by hyans.milis (Novice)
on Mar 14, 2013 at 16:01 UTC ( [id://1023496]=perlquestion: print w/replies, xml ) Need Help??

hyans.milis has asked for the wisdom of the Perl Monks concerning the following question:

hi,

any idea how to change the time format from below data on 3rd & 4th column into milisecond (epoch), bcoz currently Datetime package is not installed in my machine.

source ALL30 2191967708 201211020915 201303020923
expected result ALL30 2191967708 1351822500000 1362190980000
#!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::Format::Strptime; my $file = $ARGV[0] or die "Need to get CSV file on the command line\n +"; my $outfile = "output_date.txt"; my $sum = 0; open(my $data, '<', $file) or die "Could not open '$file' $!\n"; open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n"; while (my $line = <$data>) { chomp $line; my @fields = split "\t" , $line, -1;; my $formatter= new DateTime::Format::Strptime(pattern => '%Y%m +%d%T'); my $sum1 = "$formatter->parse_datetime($fields[2])"; my $sum2 = "$formatter->parse_datetime($fields[3])"; $fields[2] = "$sum1"; $fields[3] = "$sum2"; # $line= print join( "\t", @fields ) . "\n" ; print OUTFILE "@fields\n"; } close ($data); close (OUTFILE);
thanks

Replies are listed 'Best First'.
Re: change the time format into milisecond
by CountZero (Bishop) on Mar 14, 2013 at 16:12 UTC
    Why don't you install the DateTime package?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: change the time format into milisecond
by Not_a_Number (Prior) on Mar 14, 2013 at 21:20 UTC

    Time::Local is a core module. Consider:

    use feature 'say'; use Time::Local; $_ = 'ALL30 2191967708 201211020915 201303020923'; say 1000 * timelocal( 0, reverse unpack 'A4(A2)*' ) for ( split )[ 2, +3 ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1023496]
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-19 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found