The Perl You Need to Know Part 9:The Millennium Episode -- Time and Date Manipulation
December 13, 1999
|
With everybody's favorite "Y2K" in mind, it's
probably a little late to be reading this if you're,
say, an air traffic controller. But, as our minds roll around
to millennial -- and, for those with a personal warehouse
of Kraft dinners -- post-millennial matters, it seems a
logical time to consider the representation and manipulation
of calendar dates in
Perl.
This is certainly a useful topic,
but also relatively easy to master, which is good
since we'll all be intoxicated soon anyway. In this article
we'll learn how Perl treats dates, days, months, hours,
minutes, and so forth, and learn date acrobatics (no, not
those kind). Basic experience with Perl is a plus,
including the
first installment
of the Perl You Need to Know
series.
|
The birth of an epoch
The year 1970 is probably not that significant to most of you.
Not quite the revolution of the sixties, yet
not as totally embarrassing as the later 70's, this is a time
when the tie dye was fading and your hair was fully
grown out. Yet, you were probably blissfully unaware that 1970
was in fact the beginning of a new era in civilization,
or "epoch" as it is known. It is the era in which
computers began counting seconds. Yes, it was that
exciting a time.
It may come natural to ask someone on the street "What
time do you have?", but when it comes to Perl
you're better off thinking in terms of "How many seconds
you got?". Because, as far as Perl -- and most
things with
Unix
ancestry -- is concerned, time is counted
simply as the number of seconds that have elapsed since
the fateful day of January 1, 1970. What a day it was.
Right now, as I write this, exactly 944,600,004 seconds have
ticked off since the epoch. Time sure does fly.
I remember second 841,120,002 as if it were just yesterday --
yesterday, of course, being 86,400 seconds ago. Well,
as you can see, dealing with time as a measure of seconds is
certainly simple for the computer, but a real headache
for us humans. Perl's built-in
time
function is the
basis for most of the date-related operations we will
see in this article, and, as this build-up suggests, it merely
returns the number of seconds since the 1970 epoch.
$epochTime=time;
You cannot use an epoch time less than zero, which by
extension means that you can't easily represent a date
earlier than GMT January 1, 1970. Who remembers that far
back anyway? Second, you cannot set an Epoch time greater
than 2147483647, the maximum value that can be represented
in a 32-bit integer. This means that not only did the
Epoch begin on January 1, 1970, it ends on Tuesday, January
19, 2038 at 3:47 AM GMT. Mark your calendars! And,
yes, this means that another software frailty looms much
sooner than the dreaded Y3K.
Contents:
Human Friendly Time
ChoppedTime
Reversing Time
Date::Manip Gymnastics
Calendar Calisthenics
Conclusions
The Perl You Need to Know
Human Friendly Time
|