Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I am parsing dates and datetimes input by users who aren't too careful with their formatting. Date::Parse seems great because it handles most cases I need to handle.

Except datetimes between 1901-01-01 00:00:00 and 1968-12-31 23:59:59, as I found out today. For those datetimes, Date::Parse str2time adds an extra 100 years when it parses the datetime to epoch time.

Here is the code I am using to parse the datetimes:

#!/usr/bin/perl #--------------------------------------------------------------------- # format_date.pl # # format variable date inputs #--------------------------------------------------------------------- use strict; use warnings; use Date::Parse; use DateTime; my $DEFAULT_TIME_ZONE = "GMT"; 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 $string (@dates) { # format datetime field from any valid datetime input # default time zone is used if timezone is not included in string my $epoch = str2time( $string, $DEFAULT_TIME_ZONE ); # error if date is not correctly parsed if ( !$epoch ) { die("ERROR ====> invalid datetime ($string), " . "datetime format should be YYYY-MM-DD HH:MM:SS"); } my $date = DateTime->from_epoch( epoch => $epoch ); printf( "formatting datetime: value = %20s, epoch = %20u, " . "date = %20s\n", $string, $epoch, $date ); } exit 0;

Side note: I need to improve my error handling because the valid date 1970-01-01 00:00:00 will throw an error.

The additional 100 years for dates between 1901 and 1969 can be seen in the output:

formatting datetime: value = 1899-06-24 09:44:00, epoch = 18446744071 +484095456, date = 1899-06-24T09:44:00 formatting datetime: value = 1900-12-31 23:59:59, epoch = 18446744071 +532098815, date = 1900-12-31T23:59:59 formatting datetime: value = 1901-01-01 00:00:00, epoch = +978307200, date = 2001-01-01T00:00:00 formatting datetime: value = 1960-12-31 23:59:59, epoch = 2 +871763199, date = 2060-12-31T23:59:59 formatting datetime: value = 1966-06-24 09:44:00, epoch = 3 +044598240, date = 2066-06-24T09:44:00 formatting datetime: value = 1968-12-31 23:59:59, epoch = 3 +124223999, date = 2068-12-31T23:59:59 formatting datetime: value = 1969-01-01 00:00:00, epoch = 18446744073 +678015616, date = 1969-01-01T00:00:00 formatting datetime: value = 1969-12-31 23:59:59, epoch = 18446744073 +709551615, date = 1969-12-31T23:59:59 formatting datetime: value = 1970-01-01 00:00:01, epoch = + 1, date = 1970-01-01T00:00:01 formatting datetime: value = 2000-01-01 00:00:00, epoch = +946684800, date = 2000-01-01T00:00:00 formatting datetime: value = 2017-06-24 23:59:59, epoch = 1 +498348799, date = 2017-06-24T23:59:59 formatting datetime: value = 2018-06-24 09:44:00, epoch = 1 +529833440, date = 2018-06-24T09:44:00 formatting datetime: value = 2238-06-24 09:44:00, epoch = 8 +472332640, date = 2238-06-24T09:44:00

The Date::Parse documentation suggests it can handle dates at least as old at 1901-01-01. The Time::Local documentation suggest it should be able handle dates even older.

How should I handle this oddity? Is there a better way to parse variable input formats?


In reply to Date::Parse - how to correctly parse dates between 1901 and 1969 by eniad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found