The core Time::Piece module seems to handle most of your dates but anything earlier than the start of the 20th century can't be parsed and causes an error.
use strict;
use warnings;
use feature qw{ say };
use Time::Piece;
my @dates = (
q{1960-12-31 23:59:59},
q{1966-06-24 09:44:00},
q{1968-12-31 23:59:59},
q{1969-01-01 00:00:00},
q{1969-12-31 23:59:59},
q{1970-01-01 00:00:01},
q{2000-01-01 00:00:00},
q{2017-06-24 23:59:59},
q{2018-06-24 09:44:00},
q{2238-06-24 09:44:00},
q{1900-12-31 23:59:59},
q{1901-01-01 00:00:00},
q{1900-01-01 00:00:00},
q{1899-12-31 23:59:59},
);
foreach my $date ( @dates )
{
my $tp = Time::Piece->strptime( $date, q{%Y-%m-%d %T} );
say $date, q{ -> epoch }, $tp->epoch();
}
The output.
1960-12-31 23:59:59 -> epoch -283996801
1966-06-24 09:44:00 -> epoch -111161760
1968-12-31 23:59:59 -> epoch -31536001
1969-01-01 00:00:00 -> epoch -31536000
1969-12-31 23:59:59 -> epoch -1
1970-01-01 00:00:01 -> epoch 1
2000-01-01 00:00:00 -> epoch 946684800
2017-06-24 23:59:59 -> epoch 1498348799
2018-06-24 09:44:00 -> epoch 1529833440
2238-06-24 09:44:00 -> epoch 8472332640
1900-12-31 23:59:59 -> epoch -2177452801
1901-01-01 00:00:00 -> epoch -2177452800
1900-01-01 00:00:00 -> epoch -2208988800
Error parsing time at /usr/lib/x86_64-linux-gnu/perl/5.22/Time/Piece.p
+m line 469.
I hope this is of interest.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.