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


In reply to Re: convert iso8601 to epoch time without using additional module by kcott
in thread convert iso8601 to epoch time without using additional module by swissknife

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.