Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The TZ trick for finding yesterday's date is flawed 1

Status
Not open for further replies.

Daedelus

Technical User
Aug 14, 2002
70
US
I am posting this in both the General UNIX forum and the UNIX scripting forum since I have seen so many people suggest using the TZ trick described in [faq80-953] to find yesterday's date. Unfortunately the trick is flawed, unless you are using Greenwich Mean Time. To see the problem, try this using ksh or a similarly capable shell (that's a capable shell, not csh!)

$ date
Thu Oct 3 19:30:00 CDT 2002
$ TZ="$TZ+24" date
Thu Oct 3 00:30:17 CDT 2002

As you can see, appending "+24" to TZ did not yield yesterday's date. Why? because of where I am: the USA Central Timezone (in daylight savings time), as is indicated by the "CDT". Between May and October we are 5 hours behind CUT (Coordinated Universal Time), but the "+24" appended to TZ is taken in relation to CUT and not local time. For me, the "+24" only moves the time back 19 hours. After 7:00 p.m. I no longer get yesterday's date.

For a full description of the TZ variable, see [], (or any of many other sights).

Reading this will make several other pitfalls inherent in the TZ trick obvious.

So, the question is, who can come up with the simplest technique to correctly adjust TZ to get yesterday's date?

It needs to work for any legal value of TZ, and not be harder that directly calculating yesterdays's date. Any volunteers?
 
Hi,
We had this problem a few years ago. I finally decided just to run a cron job ever day before midnight and create a file, i.e. date > yesterday.date. Now we just use the file for comparision or archiving purposes on the following day.
 
There's always gnu's version of date.

date -d "yesterday"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top