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 thanos1983 (Parson)
on Feb 20, 2018 at 11:37 UTC ( [id://1209580]=note: print w/replies, xml ) Need Help??


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

Hello eniad,

It seems that fellow Monks have already addressed your problem, but I want to add something minor here also. Since there is a minor bug on the module would you consider also of using another module? For example I put together a very simple example on my favorite module Date::Manip regarding date manipulations.

The date formats that this modules can accept are many, just briefly see Date::Manip::Examples. The modules can convert in one step the human readable format date to epoch and vice versa.

I also included a minor comment in case you want to play with different time zone(s).

Sample of code bellow:

#!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my @dates = ( "1899-06-24 09:44:00", "1900-12-31 23:59:59", "1901-01-01 00:00:00", "1960-12-31 23:59:59", "1966-06-24 09:44:00", "1968-12-31 23:59:59", "1969-01-01 00:00:00", "1969-12-31 23:59:59", "1970-01-01 00:00:01", "2000-01-01 00:00:00", "2017-06-24 23:59:59", "2018-06-24 09:44:00", "2238-06-24 09:44:00" ); foreach my $datestr (@dates) { my $epochSecs = UnixDate($datestr,'%s'); my $date = UnixDate( ParseDateString("epoch $epochSecs"), "%Y-%m-% +d %T"); say "Date value = ".$datestr.", epoch = ".$epochSecs.", date = " +.$date; } =timezone my $timezone = UnixDate( Date_ConvTZ( "today", 'CET', 'PST' ), "%Y-%m- +%d %T"); say $timezone; =cut __END__ $ perl test.pl Date value = 1899-06-24 09:44:00, epoch = -2225459760, date = 1899-06 +-24 09:44:00 Date value = 1900-12-31 23:59:59, epoch = -2177456401, date = 1900-12 +-31 23:59:59 Date value = 1901-01-01 00:00:00, epoch = -2177456400, date = 1901-01 +-01 00:00:00 Date value = 1960-12-31 23:59:59, epoch = -284000401, date = 1960-12- +31 23:59:59 Date value = 1966-06-24 09:44:00, epoch = -111165360, date = 1966-06- +24 09:44:00 Date value = 1968-12-31 23:59:59, epoch = -31539601, date = 1968-12-3 +1 23:59:59 Date value = 1969-01-01 00:00:00, epoch = -31539600, date = 1969-01-0 +1 00:00:00 Date value = 1969-12-31 23:59:59, epoch = -3601, date = 1969-12-31 23 +:59:59 Date value = 1970-01-01 00:00:01, epoch = -3599, date = 1970-01-01 00 +:00:01 Date value = 2000-01-01 00:00:00, epoch = 946681200, date = 2000-01-0 +1 00:00:00 Date value = 2017-06-24 23:59:59, epoch = 1498341599, date = 2017-06- +24 23:59:59 Date value = 2018-06-24 09:44:00, epoch = 1529826240, date = 2018-06- +24 09:44:00 Date value = 2238-06-24 09:44:00, epoch = 8472325440, date = 2238-06- +24 09:44:00

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

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 15:30 UTC

    Can Date::Manip parse variable input formats? I have gotten inputs like:

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

      Hello again eniad,

      Well a simple example would answer your question:

      #!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my @dates = ( "2018-02-20 00:00:00", "20180220", "02/20/2018", "02/20/18", # interpreted as 1918-02-20 "2018-02-20", "today"); say UnixDate( ParseDate($_), "%Y-%m-%d") for (@dates); __END__ $ perl test.pl 2018-02-20 2018-02-20 2018-02-20 2018-02-20 2018-02-20 2018-02-20

      So in conclusion, yes the module can parse all the dates that you provided.

      Update: If you want to know which date formats are acceptable from the module read here Date::Manip::Date/VALID DATE FORMATS. On the same link you will find time formats but also date and time formats combined.

      Update 2: A minor similar example on how to parse time and print also time zone if you are interested:

      Hope this helps, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        It even correctly handles the 2-digit year example. Nice!

        I'll take a deeper dive into Date::Manip. Thanks for the example.

        Date::Manip greatly simplified the parsing of the datetimes. See Solution 2 in my answer. Thanks for the help!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found