Abbey Workshop

Perl: Date and printf

This howto reviews how to display a zero padded ISO 8601 date using sprintf in Perl. Most Perl documentation on printf points you to the man page for your particular shell for details. But this is less than useful especially if you are not a UNIX hacker. However, if you search around enough you will find the sprintf example page in the Perl documentation. The page provides a number of useful examples for using printf.

Next, the date information is obtained using the localtime function. Since all the date values produced by this function start with a value of 0 (except for year), one must be added to each value. The code is shown below.

Comments

The expression %02d creates a zero padded two digit number. So if a single digit is passed, a zero is inserted before the number.

The following is the ouput of this script:

$ printf.pl
Example date: 2005-10-17T17:53:44Z