Web Development

Manipulate dates and time with these 10 Perl CPAN modules

When it comes to manipulating dates and times in Perl it is often necessary for developers to implement functions found in the Comprehensive Perl Archive Network (CPAN). This brief tutorial explains 10 of the more useful CPAN date and time modules.

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

3 comments
Mark W. Kaelin
Mark W. Kaelin

Besides the CPAN modules listed in the article, what other CPAN modules would you like to see on TechRepublic? How much Perl coding do you?

DanLM
DanLM

Not as much as I want to, how's that for a answer with regard to how much Perl coding do I do. Actually, it's on the increase again. CPAN modules? Right now? Id like to see something on manipulation of ip/broadcast/cidr numbers back and forth. I'm going to be using the Network::IPv4Addr module for a firewall statistical Perl script I am writing at my house. MySql, I'm going to be using this both at home and work soon. You listed off the date module for that. Will this also do conversion with Date Time stamps???? Thank you by the way for a list of those date modules, I see uses for a couple of them already(cleans dates, regex dates from a string). I'll have to go through that bundle install to see what all is there with regard to what you listed here. How about some of the GD modules? These require support libraries to be installed and offer multiple functions with regard to images. Conversion of image formats??? creation of thumbnails, GD::SecurityImage - This is very flexible, but a bear to get to work. You can use any font you want with it and it would be an asset to the web developers here. Just some thoughts... Dan

DanLM
DanLM

Any chance of doing something with the DB module and MySql 5.0 stored procedures? Possibly suggested uses for and examples of? This is something that I think everyone here that does anything with MySql/Perl would appreciate. thanks again. [i]Edited:[/i]What was I thinking with that origional topic Dan

Editor's Picks