Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Date::Parse - how to correctly parse dates between 1901 and 1969

by johngg (Abbot)
on Feb 19, 2018 at 23:27 UTC ( #1209517=note: print w/replies, xml ) Need Help??


in reply to Date::Parse - how to correctly parse dates between 1901 and 1969

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.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Date::Parse - how to correctly parse dates between 1901 and 1969
by eniad (Acolyte) on Feb 20, 2018 at 00:10 UTC

    I am using Date::Parse to parse datetimes that may be in different formats. The dates have come in like YYYYMMDD, YYYY-MM-DD, YYYYMMDD HH:MM, etc. Time::Piece->strptime() requires a defined input format. Time::Piece would certainly work once the datetime is well formatted. Is there and advantage to Time::Piece over DateTime?

      The only advantage is that it has been a core module since Perl 5.10 so it doesn't have to be installed from CPAN. I don't have much experience with Time::Piece and none at all with Date::Parse so can't really comment on usability.

      Cheers,

      JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1209517]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-07-15 23:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (327 votes). Check out past polls.

    Notices?