The Many Dates and Times of Perl
by Dave Rolsky
|
Pages: 1, 2, 3
The Big Stars
These are the modules that have the longest history, and are the most widely used.
- TimeDate distribution
This distribution, maintained and mostly written by Graham Barr, includes three modules.
Date::Formatmodule provides a few functions for formatting datetime output, including astrftime()similar to the one in the standard C library. It can work with either epoch times, or the array of components returned by Perl'slocaltime()function.Date::Parseparses a limited set of common datetime formats, returning either an epoch time or an array of components.The distribution also includes a number of language modules which can be used to localize both parsing and formatting.
Finally,
Time::Zoneprovides an interface to time zone offsets, based on short time zone names like "EST" or "GMT". As was mentioned before, these names are not official or standardized, so they are of limited usefulness.All of these modules are limited by their use of epoch time internally, but they are fairly quick and light weight. For complex datetime problems, these modules probably don't automate enough of the dirty work.
- Time::Piece
Written and maintained by Matt Sergeant, this module is based on an interface designed by Larry Wall. It provides a convenient object API for datetimes, though the API is a bit confusing. For example,
$time->monreturns the month number (1-12) while$time->monthreturns the abbreviated name of the month.It also implements basic parsing and formatting via the use of the C-level
strptime()andstrftime()functions. The includedTime::Secondsmodule allows for basic date math, such as$tomorrow = $time + ONE_DAY.The implementation is fairly lightweight, but is limited by its use of epoch time as the internal representation. It is certainly useful, but like the TimeDate modules, it doesn't go far enough for more complex uses.
As of this writing, Matt Sergeant has released an experimental version of
Time::Piecebased on myDateTime.pmmodule. This leaves theTime::PieceAPI unchanged, but allows it to handle dates that cannot be represented by a given system's epoch. - Date::Manip
Sullivan Beck's
Date::Manipis really huge, weighing in at about 3,000 lines of code. As might be expected of that much code, there is something for everyone here. It handles parsing, formatting, date math, as well as more esoteric things like recurring datetimes and business day calculations. It should be noted that it's time zone support is pretty much the same that provided byTime::Zone.This module's most unique feature is its very flexible parser, which is capable of handling things like "3 weeks ago Monday" or "next Sunday". It also provides some parsing for specifying recurrences, like "every 3rd Monday in 2003".
Unlike everything else we've covered so far, this module is not limited to epoch times. It has an entirely functional interface, and in my opinion the API could be cleaner. I dislike the fact that some functions do many different things, with the output depending either on the argument type(s), explicit flags, or both.
But the biggest problem with this module, which is acknowledged by its author, is its size. It uses lots of memory (about 3MB on my system), and is fairly slow to load. The former makes it problematic for mod_perl, and the latter causes problems with CGI scripts. You can find most of its features elsewhere, in slimmer modules, but if size and speed are not an issue, this module almost certainly does everything you want.
- Date::Calc
Steffen Beyer's
Date::Calcdistribution is where you go when you need functionality combined with speed. This modules offers much of the functionality provided byDate::Manip, but the coreDate::Calcmodule has a much smaller memory footprint thanDate::Manip(about 1MB on my box), and much greater speed. This is based its core implementation is in C.This module provides functions for calculating all sorts of date-related information, as well some minimal parsing and formatting operations. The interface requires some hand-holding to use, as every function returns one or more elements, never a data structure such as a hash, so you have to constantly deal with passing and receiving arrays of values.
The distribution also includes a class called
Date::Calc::Object, which can represent either a datetime or a "delta", the difference between two datetimes. This dual nature is odd, since many of the methods applicable to one will not work for the other. The class supports overloading for date math and comparison, so you can do things like$date + [1, 2, 3], which adds one year, two months, and three days to the given date.Finally, there is a
Date::Calendarobject, which can be used to set up a calendar "profile" defining holidays, work days, and partial work days in a variety of ways. This is quite useful if you need to calculate a day X business days in the future, while taking account of a particular organization's holidays.None of the modules in this distribution rely on epoch time, though they only support positive-numbered years. Time zone support is extremely minimal, and is done only as offsets, without support for daylight saving rules. Localization for a variety of languages is implemented for parsing and formatting.
And a Cast of Thousands ...
It wouldn't be Perl if there weren't at least a dozen other modules with overlapping functionality, right? In this case, there's more than two dozen! For sanity's sake, I've excluded more than a few modules, in particular those that either appeared to be unmaintained, or those without enough comprehensible documentation for me to figure out what the heck they do. In alphabetical order, those remaining are:
- Astro::Sunrise - Ron Hill
This module provides sunrise and sunset times, given a specific date and place.
- Astro::Time - Chris Phillips
Contains a number of functions useful when dealing with datetimes as they are used in astronomy.
- Class::Date - Bal&0xE1;zs Szab&0xF3;
This is basically the
Time::PieceAPI plus more stuff. It appears to provide complete time zone support based on the Olson database, by way ofPOSIX.pm. It is epoch-limited. - Date::Business - Richard DeSimine
A simple interface for doing date math with business dates. In other words, weekends don't count, and you can define your own holidays. This module is epoch-limited.
- Date::Convert - Mordechai Abzug
Converts dates between the Gregorian, Hebrew, and Julian calendars.
- Date::Convert::French_Rev - Jean Forget
Converts to and from the French Revolutionary calendar.
- Date::Day - John Von Essen
Tells you the day of the week for a given date.
- Date::DayofWeek - Rich Bowen
Does exactly the same thing as
Date::Daybut only for years 1500-2699. On the flip side, it uses a cuter algorithm. - Date::Decade - Michael Diekmann
Provides three decade calculation functions with an API similar to that of
Date::Calc. - Date::Discordian - Rich Bowen
Converts to and from the Discordian calendar.
- Date::Easter - Rich Bowen
Calculates the date Easter falls on in a given year.
- Date::Handler - Benoit Beausejour
A date object comparable to
Time::Piece, but with a more consistent interface. It implements localization, a variety of date math operations, and includes an object for representing datetime spans. It provides time zone support based on the native OS implementation, which on some systems means support for the Olson database. It is epoch-limited. - Date::ICal - Rich Bowen
A date object that provides a simple API, date math, and iCal formatting and parsing (see RFC 2445). It only support time zones as offsets, but it is not epoch-limited.
- Date::ISO - Rich Bowen
A subclass of Date::ICal that adds ISO 8601 format parsing and formatting.
- Date::Japanese::Era - Tatsuhiko Miyagawa
Converts to and from Japanese Era dates.
- Date::Japanese::Holiday - Tomohiro Ikebe
Tells you whether or not a given date is a Japanese holiday.
- Date::Leapsecond - Fl&0xE1;vio Soibelmann Glock
Converts between UT1 and UTC times.
- Date::Leapyear - Rich Bowen
One function to tell you whether or not a given year is a leap year.
- Date::Maya - Abigail
Converts between Julian days and the Mayan calendar.
- Date::Passover - Rich Bowen
Calculates the dates of Passover and Rosh Hashanah for a given year.

