Eron Anon Eron Anon - 6 months ago 30
Perl Question

Error when dealing with old and historical dates in Perl Time::Piece

It seems

Time:Piece
gives me this error:
Error parsing time at /usr/lib/perl5/site_perl/Time/Piece.pm line 481.

after the line where I use
strptime
with some old dates.

My code contains this:

my $ddate = "$month / $day / $year";
my $tmp = Time::Piece->strptime( $ddate, "%m / %d / %Y");


and $$date takes dates from a database using DBI that contains historical and old dates (dates back to the 10th and 9th centuries AD). How can I deal with this if there is any solution?

Answer

You'll have to use something other than Time::Piece if you want to deal with timestamps before 1970. DateTime supports those (though time zone data might be sketchy if you go too far back).


Based on this historical calendar site, the Julian calendar was used prior to 1582. DateTime::Calendar::Julian provides better results for these dates.

$ perl -MDateTime -E'
   say
      DateTime->new(year => 1013, month => 2, day => 22)
         ->strftime("%a");
'
Mon

$ perl -MDateTime::Calendar::Julian -E'
   say
      DateTime::Calendar::Julian->new(year => 1013, month => 2, day => 22)
         ->strftime("%a");
'
Sun