Time::localtime

use Time::localtime;
printf "Year is %d\n", localtime->year() + 1900;

$now = ctime();

use Time::localtime;
use File::stat;
$date_string = ctime(stat($file)->mtime);

This module's overrides the core localtime function, replacing it with a version that returns a Time::tm object (or undef on failure). The Time::gmtime module does the same thing, except it replaces the core gmtime function, instead. The returned object has methods that access the like-named structure field names from the C library's struct tm out of time.h; namely sec, min, hour, mday, mon, year, wday, yday, and isdst. The ctime function provides a way of getting at (the scalar sense of) the original CORE::localtime function. Note that the values returned are straight out of a struct tm, so they have the same ranges found there; see the example above for the correct way to produce a four-digit year. The POSIX::strftime function is even more useful for formatting dates and times in a variety of appealing styles.

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.