I have a log file (datetimes.log
) consisting of hundreds of thousands of timestamps of the form:
YYYY-MM-DD HH:mm:ss
For example:
2013-03-28 06:43:51
2013-03-28 06:43:55
2013-03-28 06:44:03
...etc.
I'd like to write a simple Perl script to output a new unix_timestamps.log
file that contains the same entries, but instead of the datetime, to have the corresponding UNIX epoch timestamp. For the example above, the unix_timestamps.log
file would have the following info in it:
1364453031
1364453035
1364453043
...etc.
The only thing I can think of is perl convert_2_timestamps.pl
:
#!/usr/bin/perl
use warnings;
use strict;
grep m/_(\d{4})(\d\d)(\d\d)/ | POSIX::mktime(?, ?, ?, ?, ?, ?) > unix_timestamps.log
But not sure how to transfer the parameters into mktime
, and not sure if this is even the right approach. Thanks in advance.
use strict;
use warnings;
use DateTime::Format::Strptime;
my $parser = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S',
on_error => 'croak',
);
while( <DATA> ) {
my $dt = $parser->parse_datetime($_);
print $dt->epoch, "\n";
}
__DATA__
2013-03-28 06:43:51
2013-03-28 06:43:55
2013-03-28 06:44:03