Skip to Main Content
Programming Perl, 3rd Edition
book

Programming Perl, 3rd Edition

by Larry Wall, Tom Christiansen, Jon Orwant
July 2000
Intermediate to advanced content levelIntermediate to advanced
1104 pages
35h 1m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 3rd Edition

Time::Local

use Time::Local;
$time = timelocal($sec,$min,$hours,$mday,$mon,$year);
$time = timegm($sec,$min,$hours,$mday,$mon,$year);

$time = timelocal(50, 45, 3, 18, 0, 73);
print "Scalar localtime gives: ", scalar(localtime($time)), "\n";
$time += 28 * 365.2425 * 24 * 60 * 60;
print "Twenty-eight years of seconds later, it's now\n\t",
    scalar(localtime($time)), "\n";

This prints:

Scalar localtime gives: Thu Jan 18 03:45:50 1973
Twenty-eight years of seconds later, it's now
        Wed Jan 17 22:43:26 2001

The Time::Local module provides two functions, timelocal and timegm, that work like inverse functions for the standard localtime and gmtime functions, respectively. That is, they take a list of numeric values for the various components of what localtime returns in list context and figure out what input to localtime would produce those values. You might do this if you wanted to compare or run calculations on two different dates. Although these are not general-purpose functions for parsing dates and times, if you can arrange to have your input in the right format, they often suffice. As you can see from the example above, however, time has its oddities, and even simple calculations often fail to do the job intended due to leap years, leap seconds, and the phase of the moon. Two large but fully featured CPAN modules address these issues and more: Date::Calc and Date::Manip.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Perl in a Nutshell, 2nd Edition

Perl in a Nutshell, 2nd Edition

Nathan Patwardhan, Ellen Siever, Stephen Spainhour

Publisher Resources

ISBN: 0596000278Supplemental ContentErrata