Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Converting dates later than 2038 into epochseconds

by strat (Canon)
on Jan 09, 2003 at 00:33 UTC ( [id://225400]=perlquestion: print w/replies, xml ) Need Help??

strat has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am just writing a module that deals with dates and german holidays, and since this module should run on 32-bit boxes too, I can't use the builtin function localtime and the function timelocal from Time::Local because they don't like years > 2038.

For localtime, I can use the localtime from Time::Piece instead which seems to do a good job, but I still need the other way round (the timelocal-way). I've searched CPAN and my local modules, but haven't found anything. Do you know a CPAN-Module that converts a date into epochseconds and doesn't stop at 2038 or before 1970 at 32-bit-systems? Or do I have to code it by myself?

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

  • Comment on Converting dates later than 2038 into epochseconds

Replies are listed 'Best First'.
Re: Converting dates later than 2038 into epochseconds
by mojotoad (Monsignor) on Jan 09, 2003 at 00:58 UTC
    Normally, with Time::Piece, you would use the strptime() method to parse a date and generate a new Time::Piece object from which you can get the seconds using epoch(). However, it does not appear to handle huge dates.

    Here's the Date::Manip way, which handles rarified dates nicely, from the docs:

    NOTE: One of the most frequently asked questions that I have gotten is how to parse seconds since the epoch. ParseDateString cannot simply parse a number as the seconds since the epoch (it conflicts with some ISO-8601 date formats). There are two ways to get this information. First, you can do the following:
    $secs = ... # seconds since Jan 1, 1970 00:00:00 GMT $date = &DateCalc("Jan 1, 1970 00:00:00 GMT",$secs);
    Second, you can call it directly as:
    $date = &ParseDateString("epoch $secs");
    To go backwards, just use the "%s" format of UnixDate:
    $secs = &UnixDate($date,"%s");

    Also from the docs, the section pertaining to far-flung dates and the algorithm:

    Date::Manip deals with time as it is presented the Gregorian calendar (the one currently in use). The Julian calendar defined leap years as every 4th year. The Gregorian calendar improved this by making every 100th year NOT a leap year, unless it was also the 400th year. The Gregorian calendar has been extrapolated back to the year 0000 AD and forward to the year 9999 AD. Note that in historical context, the Julian calendar was in use until 1582 when the Gregorian calendar was adopted by the Catholic church. Protestant countries did not accept it until later; Germany and Netherlands in 1698, British Empire in 1752, Russia in 1918. Note that the Gregorian calendar is itself imperfect and at some point will need to be corrected. No attempt is made to correct for that, and my great great great grandchildren will be long dead before this even occurs, so it's not an immediate concern. Yes, this is the same type of attitute that caused the great Y2K problem... but I have an excuse: I don't know what the correction will be, so I can't possible implement it. Nobody doubted that the year after 1999 would be known as 2000 :-).

    Hope that helps.
    Matt

Re: Converting dates later than 2038 into epochseconds
by Enlil (Parson) on Jan 09, 2003 at 01:05 UTC
    You can use the ever popular though bulky Date::Manip. For example:
    use Date::Manip; use strict; my $date = "Jan 19 03:14:07 9999"; my $secs = &UnixDate($date,"%s"); print "$secs\n";
    Though I am sure you can find something suited to what you ask (if this is not it), by going through the documentation.

    -enlil

Re: Converting dates later than 2038 into epochseconds
by krujos (Curate) on Jan 09, 2003 at 00:39 UTC
    What about Date::Parse?
    Update: Do you negitive numbers for dates before 1970?
    Update: I did a little testing of this and it won't work for your needs. Sorry for the bum suggestion. Oddly enough it will give you answers for dates before 1970 if that helps.
    #/usr/bin/perl -w use strict; use Date::Parse; my $date; $date = "Aug, 20 1955 11:09:55 +0100"; my $time; $time = str2time($date); print " $date\n"; print " $time\n" # perl tmp.pl Aug, 20 1955 11:09:55 +0100 -453390605
    Josh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-18 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found