3. Dates and Times
Contents:
It is inappropriate to require that a time represented as seconds since the Epoch precisely represent the number of seconds between the referenced time and the Epoch. - IEEE Std 1003.1b-1993 (POSIX) Section B.2.2.2 3.0. IntroductionTimes and dates are important things to be able to manipulate. "How many users logged in last month?", "How many seconds should I sleep, if I want to wake up at midday?", and "Has this user's password expired yet?" are all common questions whose answers involve surprisingly non-obvious manipulations.
Perl represents points in time as intervals, measuring seconds past a point in time called
the Epoch
. On Unix and many other systems, the Epoch was 00:00 Jan 1, 1970, Greenwich Mean Time (GMT).[
1
] On a Mac, all dates and times are expressed in the local time zone. The
When we talk about dates and times, we often interchange two different concepts: points in time (dates and times) and intervals between points in time (weeks, months, days, etc.). Epoch seconds represent intervals and points in the same units, so you can do basic arithmetic on them. However, people are not used to working with Epoch seconds. We are more used to dealing with individual year, month, day, hour, minute, and second values. Furthermore, the month can be represented by its full name or its abbreviation. The day can precede or follow the month. Because of the difficulty of performing calculations with a variety of formats, we typically convert human-supplied strings or lists to Epoch seconds, calculate, and then convert back to strings or lists for output. For convenience in calculation, Epoch seconds are always calculated in GMT. When converting to or from distinct values, we must always consider whether the time represented is GMT or local. Use different conversion functions depending on whether you need to convert from GMT to local time or vice versa.
Perl's
The values for second range from 0-60 to account for leap seconds; you never know when a spare second will leap into existence at the urging of various standards bodies. From now on, we'll refer to a list of day, month, year, hour, minute, and seconds as DMYHMS, for no better reason than that writing and reading "distinct day, month, year, hour, minute, and seconds values" is wearisome. The abbreviation is not meant to suggest an order of return values.
Perl does
not
return a two-digit year value. It returns the year minus 1900, which just happens to be a two-digit number through 1999. Perl doesn't intrinsically have a
Year 2000 problem, unless you make one yourself. (Your computer, and Perl, may have a 2038 problem, though, if we're still using 32 bits by that time.) Add 1900 to get the full year value instead of using the construct
In scalar context,
The standard
Time::tm module provides objects that give you a named interface to these values. The standard
Time::localtime and Time::gmtime modules override the list-returning # using arrays print "Today is day ", (localtime)[7], " of the current year.\n";
To go
from
a list
to
Epoch seconds, use the standard
Time::Local module. It provides the functions
Epoch seconds values are limited by the size of an integer. If you have a 32-bit signed integer holding your Epoch seconds, you can only represent dates (in GMT) from
The
Date::Calc and Date::Manip modules on CPAN both work from these distinct values, but be warned: years don't necessarily have 1900 subtracted from them the way the year value returned by Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|