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

Hi,

1900 + nonsense is the entire reason for modules like DateTime to exist

From DateTime

my $formatter = DateTime::Format::Strptime->new(...);

So DateTime::Format::Strptime, DateTime:: Strptime

my $dt = $formatter->parse_datetime( $timestampstring );

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

    The input can be in variable formats:

    my @dates = ( "2018-02-20 00:00:00", "20180220", "02/20/2018", "02/20/18", # interpreted as 1918-02-20 "2018-02-20" );

    Date::Parse handles those (with some massaging). DateTime::Format::Strptime requires well formatted dates to begin with.

    Am I missing a module of DateTime or Time::Piece that can handle the different input formats?

      #!/usr/bin/perl -- use strict; use warnings; use DateTime; use DateTime::Format::Natural; my @dates = ( "2018-02-20 00:00:00", "20180220", "02/20/2018", "02/20/18", # interpreted as 1918-02-20 "2018-02-20", "today", "now", ); my $dfn = DateTime::Format::Natural->new; print $dfn->parse_datetime($_),$/ for @dates; __END__ 2018-02-20T00:00:00 2018-02-20T21:12:19 2018-02-20T21:12:19 2018-02-20T21:12:19 2018-02-20T00:00:00 2018-02-20T00:00:00 2018-02-20T21:12:19
      So you simply make up a pattern for each format ... ... Could try ..Format::Natural