in reply to convert iso8601 to epoch time without using additional module

G'day swissknife,

Welcome to the monastery.

Firstly, and this applies to any post you make here, please show the full version number (e.g. 5.8.8). Just type perl -v on the command line to get this information. You've told us that your version is "5.8.x" — there were 10 of those 5.8.0 - 5.8.9.

[The following is based on the documentation for 5.8.8, which is the earliest available from http://perldoc.perl.org/.]

This example script shows the basic code you'll need:

#!/usr/bin/env perl -l use strict; use warnings; use Time::Local; my $iso_date = '1970-01-01T01:01:01'; my $expected_epoch = 1 * 60 * 60 + 1 * 60 + 1; my ($date, $time) = split /T/ => $iso_date; my ($year, $mon, $mday) = split /-/ => $date; $year -= 1900; $mon -= 1; my ($hour, $min, $sec) = split /:/ => $time; print "Expected Epoch: $expected_epoch"; print 'Calculated Epoch: ', timegm($sec, $min, $hour, $mday, $mon, $ye +ar);

Output:

Expected Epoch: 3661 Calculated Epoch: 3661

See Time::Local (v5.8.8) and gmtime (v5.8.8) for an explanation of that code.

If your ISO 8601 dates don't exactly match the format I've used (e.g. they could include time zone information), you'll need to tweak the code to deal with that. If you don't know how to do that, I'd recommend starting with the basics: "perlintro -- a brief introduction and overview of Perl (v5.8.8)".

-- Ken

Replies are listed 'Best First'.
Re^2: convert iso8601 to epoch time without using additional module
by swissknife (Sexton) on Mar 18, 2014 at 10:04 UTC

    Thanks Ken. Finally i was able to get what i was looking. format of time is different in my case and i was able to deal with that.