More About Dates
From the 10-30-2003 edition of Unix In the Enterpirse newsletter.
More about dates
By Sandra Henry-Stocker
Last week's column dealt with date calculations and the GNU date's
special ability to translate dates into a format that represents the way
dates are stored internally on Unix systems - the number of seconds
since midnight on January 1, 1970. Once we can easily go from this
internal format to calendar dates and back again, it is easy to
calculate the time that elapses between any two dates and times. All
concerns about the number of days in a particular month or whether it
happens to be leap year are moot.
Last week's column also explored other features of the GNU date command
- features that give meaning to words like "yesterday" and "tomorrow"
and allow users to obtain dates some number of days or weeks back or
forward.
Since last week's column, several readers have brought to my attention
another type of date calculation. This date calculation provides a
similar ability to project dates in the past or in the future but,
instead of relying on arguments provided to a version of the date
command equipped to respond to them, it relies on manipulation of the
time zone variable. Before we look at the commands that we can use to
take advantage of this alternate approach to calculating dates, let's
review how time zones work.
Unix and time zones
The date that is displayed by the date command depends not only on how
many seconds have passed since January 1, 1970, but on the time zone
that is set on the system. A system in Boston and a system in Santa
Clara might simultaneously report the time as "Thu Oct 30 14:39:10 EST
2003" and "Thu Oct 30 11:39:10 PST 2003". Both of these strings
represent, of course, the same time. If you're sitting on the East
Coast and want to know what time it is on the West Coast (and just not
in the mood to think about whether the Sun goes from East to West or
vice versa, you can enter a command such as this:
boson> TZ="US/Pacific" date
Thu Oct 30 11:39:10 PST 2003
In this command, you have supplied a time zone to be used by the date
command. There are two ways to unambiguously report the time. One is
to report what time it is on the clock along with the time zone in which
that clock resides. The other is to report time with respect to
Greenwich, England -- that is "Greenwich Mean Time" or GMT. US/Eastern
is going to be four or five hours different (i.e., "earlier") then GMT
depending on whether we're describing the time as "EDT" which always
means GMT-4 or "EST" which always means GMT-5. The definition of
US/Eastern on a Solaris system allows it to track the transitions to and
from daylight savings time.
Looking forward and ahead by manipulating the time zone
Using the time zone to modify the expression of date/time information,
it is not only possible but fairly easy to calculate a date such as
tomorrow, yesterday or two weeks ago. With an alias such as this, for
example, you can always display tomorrow's date:
alias tomorrow="TZ=$TZ-24 date +%D"
This command reports the date from the perspective of a fictitious time
zone that is 24 hours ahead. In that fictitious time zone, the current
date is tomorrow. If you can keep in mind that "-" is used to calculate
dates in the future and "+" is used to calculate dates in the past,
following the "$TZ-" argument with the number of days back or forward
multiplied by 24 will result in the date you need. To determine what
the date will be one week from today, you will type:
boson> TZ=$TZ-168 date "+%Y-%m-%d"
2003-11-06
The following script calculates many such dates.
--------------------------- cut here -----------------------
#!/bin/sh
Yesterday=`TZ=EST+24 date +%Y-%m-%d`
echo "Yesterday was:\t\t\t" $Yesterday
Today=`date +%Y-%m-%d`
echo "Today is:\t\t\t" $Today
Tomorrow=`TZ=$TZ-24 date +%Y-%m-%d`
echo "Tomorrow is:\t\t\t" $Tomorrow
Back2Days=`TZ=$TZ-48 date "+%Y-%m-%d"`
echo "Two days from today will be:\t" $Back2Days
Back1Week=`TZ=$TZ+168 date "+%Y-%m-%d"`
echo "A week ago was:\t\t\t" $Back1Week
Forw1Week=`TZ=$TZ-168 date "+%Y-%m-%d"`
echo "One week from today will be:\t" $Forw1Week
--------------------------- cut here -----------------------
Calculating the end of the month
None of the date calculations shown thus far will give you the date for
the last day of the month. However, the time zone "trick" does provide
a workaround that make it possible to determine, within a script,
whether the current day is the end of the month. This trick involves
the use of our "tomorrow" calculation; basically, if tomorrow is the
first day of the month, then today must be the last day of the month.
--------------------------- cut here -----------------------
#!/usr/bin/bash
if [ `TZ=$TZ-24 date +%d` = "01" ]; then
echo today is the day
# add commands here
fi
--------------------------- cut here -----------------------
If you were to run the script shown above every day, the commands you
inserted in place of the "add commands here" comment would only be run
on the last day of each month.
For those of you who found this column in your inbox, you can expect my
next column on TZ=$TZ-168 date "+%Y-%m-%d".
________________________________________________________________________________
SPONSORED LINK
Have you developed Security Investment Metrics? Start now.
How visible is your corporation? How competitive is your industry? Whose
interested in hacking into your systems? The more outwardly looking and
facing applications and organizations need greater security. Learn more.
http://ui.unixinsider.com/GoNow/a14724a88968a76264127a0
________________________________________________________________________________
About the author(s)
-------------------
Sandra Henry-Stocker has been administering Unix systems for nearly 18
years. She describes herself as "USL" (Unix as a second language) but
remembers enough English to write books and buy groceries. She
currently works for TeleCommunication Systems, a wireless
communications company, in Annapolis, Maryland, where no one else
necessarily shares any of her opinions. She lives with her second
family on a small farm on Maryland's Eastern Shore. Send comments and
suggestions to mailto:sstocker@itworld.com.
________________________________________________________________________________
ITWORLD.COM SERVICES
Catch the Next Wave: Virtualizing Storage for Automation
Enterprise data storage needs and requirements are growing at an
ever-inflating rate. Throw in the need for cost savings, and you'll find
yourself asking, "Can network storage help me cut costs today?" Learn
more.
http://ui.unixinsider.com/GoNow/a14724a88968a76264127a1
Next Generation eSupport for First Contact Resolution
How well do you respond to customer demand for immediate and complete
call resolution? Explore how to instantly streamline your support of
remote customers and employees with web based technology that will
consistently deliver high-touch quality of service, reduce call-handling
time and resolve technical problems on the spot.
http://ui.unixinsider.com/GoNow/a14724a88968a76264127a3
________________________________________________________________________________
CUSTOMER SERVICE
VIEW YOUR NEWSLETTERS
http://reg.itworld.com/servlet/Frs.frs?Script=reg_script_v4&Context=LOGIN
UNSUBSCRIBE
For instruction on how to unsubscribe, go to:
http://www.itworld.com/response/site_support.html#unsubnl
CHANGE YOUR E-MAIL ADDRESS
To change your e-mail, go to:
http://reg.itworld.com/servlet/Frs.frs?Script=change_email_v4&Context=LOGIN
For instruction on how to change your e-mail address, go to:
http://www.itworld.com/response/site_support.html#email
NEWSLETTER ARCHIVES
http://www.itworld.com/nl/archive.html
NEWSLETTER FAQS
For commonly asked newsletter questions, go to:
http://www.itworld.com/response/site_support.html
________________________________________________________________________________
CONTACTS
* Editorial: Jodie Naze, Director, ITworld.com Site Network,
jodie_naze@accelacommunications.com
* Advertising: Clare O'Brien, Vice President of Sales,
clare_obrien@accelacommunications.com
________________________________________________________________________________
PRIVACY POLICY
ITworld.com has been TRUSTe certified
http://www.itworld.com/Privacy/
ITworld.com is a product of Accela Communications, Inc.
Copyright 2003 Accela Communications, Inc., All Rights Reserved.
http://www.accelacommunications.com
________________________________________________________________________
VISIT OUR SITE NETWORK
http://security.itworld.com
http://smallbusiness.itworld.com
http://storage.itworld.com
http://utilitycomputing.itworld.com
http://wireless.itworld.com
http://www.itworld.com