This article is also available as a TechRepublic download.

The ability to manipulate dates and times is fundamental to
most programming languages, and Perl is no exception. However, when it comes to more
advanced temporal processing, the bare-bones capabilities built into Perl may
often seem insufficient. In such situations, it’s a good idea to head on over
to the Comprehensive Perl Archive Network (CPAN), which is stocked to the brim
with ready-made Perl modules to enable developers to efficiently perform date
and time manipulation tasks. It’s worthwhile spending some time browsing this
collection to see if you can locate something that will help you in your next
project.

By listing ten of CPAN’s most
interesting modules in this category (Table
A
), this document provides a starting point for the next time you need to
convert a timezone or calculate the number of seconds
between two timestamps. Take a look.

Note: You can
install CPAN modules directly from the Internet, by following the instructions provided.

Table A

Package Name

Description

URL

Date::Manip

This module is described as the “Swiss Army
knife” of date manipulation modules. It provides routines to parse and
compare dates, extract date information, determine date/time offsets, work
with recurring dates and times, and work with dates in different languages.
However, it is slower than other, more specialized modules.

Use this
module when you need a single general-purpose date manipulation module and
are not particularly concerned about performance.

Date::Manip

DateTime

This module, which is part of the Perl DateTime
project, provides a base class for “the representation of date/time
combinations”. It is the most convenient way to create custom Date
objects, and includes methods for parsing, formatting, duration calculation
and time zone manipulation.

Use this module as the foundation to create new date-related
objects, typically when localizing or defining custom storage containers for
temporal information.

DateTime

Time::Format

When it comes to re-formatting date and time values, this
module has no equal — it supports a wide range of formatting codes to
drastically change the appearance of your dates and times. It accepts both DateTime
objects and Date::Manip
strings and also provides interfaces to POSIX’s strftime() function
for added control.

Use this module when you need to re-format your date and
time values, usually before displaying them to application users.

Time::Format

Time::Interval

This module is useful for dealing with time intervals. It
provides routines to calculate the total number of days, hours, minutes and
seconds elapsed between two timestamps. It also allows easy conversion of
durations into different time units such as seconds, hours or minutes.

Use this module when you need to calculate the time
interval elapsed between two date values.

Time::Interval

Date::Convert

This module provides routines to convert between different
calendar formats (Gregorian, Julian, Hebrew, Islamic and Absolute). It also
provides hooks for easy extension to new calendar formats.

Use this module when you need to convert from one calendar
format to another.

Date::Convert

Benchmark

This module allows you to run a piece of code multiple
times, timing the execution of each run and returning the average time taken.
It can also be used to see how many times a code block can run in a fixed
time window.

Use this module for performance benchmarking, to measure
how fast or slow a particular block of code runs and to collect accurate
readings to guide you in optimization.

Benchmark

Time::Normalize

This module provides routines to normalize arbitrary date
and time values into a consistent and standardized representation, which can
then be used for calculations or storage. It performs error checks on its
inputs and then returns individual components of the time and/or date in a
sanitized manner.

Use this module when you suspect that your date input has
errors and need to “clean” it before using it in a database or
application.

Time::Normalize

Regexp::Common::time

This module creates regular expressions that can be used
for parsing dates and times. It supports the use of both precise rules and
sophisticated fuzzy logic to recognize and capture date patterns from string
values.

Use this module to recognize and extract date information
from human-readable strings and convert it into machine-readable format (eg: ISO 8601).

Regexp::Common::time

MySQL::DateFormat

This module provides routines to convert dates and times
back and forth between MySQL’sYYYY-MM-DD format and
human-readable strings. It is most useful when retrieving/adding records
containing date information from/to a MySQL database.

Use this module to reformat MySQL date/time strings for
more readable display, or to modify user-supplied date values prior to
inserting it into a MySQL database.

MySQL::DateFormat

Net::Time

This module provides a client interface to retrieve date
and time information from a remote host. It is particularly useful if your
application needs to always securely know the current date and time, to
insure against hackers spoofing the system.

Use this module for date/time retrieval from a remote host
over a TCP network.

Net::Time