Feb 25, 2014, 2:53 PM
Post #2 of 5
Don't ever try to calculate dates yourself, it is an real mess. Perhaps one of the most complicated problems of applied IT (as opposed to theoretical CS), together with rounding issues.
Re: [PapaGeek] Working with Dates
[In reply to]
There are a number of Perl functions and modules to deal with dates and times. First some general pointers.
Starting point: the time internal function of Perl returns the number of seconds elapsed since "the Epoch", usually Jan. 1, 1970.
This number of seconds can be transformed into a date and time using the gmtime and localtime Perl internal functions. The Time::gmtime and Time::localtime add some further funtionnalities.
Then, you also have the Time::timegm and Time::Local which do conversions the other way around.
The POSIX module has also a number of possibilities, including the POSIX::strptime functions.
Now, for the precise problems that you discuss, you really have to look at the Date::Calc module, which offers more than 65 date calculation functions abiding to all conventions of the Gregorian Calendar (the calendar used in all Western countries and a large part of the rest of the world), including leap years and so on.
Some other modules that might have some interest for you:
There are many other modules related to times and dates on the CPAN, but I would think that those I mentioned are the main ones, especially Date::Calc for the precise problem you are mentionning.
You'll find many other date and time modules, including Julian calendar (the predecessor of the Gregorian one, associated with Julius Caesar), Maya calendar, various religious calendars (Christian, Jewish, Muslim, etc.), and so on, and, just as an anectotical point, a friend of mine is even maintaining a French Revolutionary Calendar module, a calendar that was used in France only for about only ten years (about 1793 to 1802) -- but the months of Brumaire (see e.g. http://en.wikipedia.org/wiki/18_Brumaire) and Thermidor, just to name two of them, might ring a bell to you if you have any interest in this historical period.