Operating System - HP-UX
1834935 Members
2089 Online
110071 Solutions
New Discussion

yesterday's date question

 
SOLVED
Go to solution
Unix Administrator_5
Frequent Advisor

yesterday's date question

after perusing faq, list servs etc I cant seem to get the following to work:

sh -c "TZ=$(date +%Z)+24;export TZ;date '+%a %b %e'"

At 8 o'clock last night, it reported that yesterday was Feb 17, and the regular date command showed Feb 17.
My TimeZone it EST5EDT.

Is there anyway to get this to work 24X7?
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: yesterday's date question

Hi:

No. Our European friends living around the Prime Meridian are the "lucky" ones who can use +-24 hours to exactly compute yesterday or tommorrow including the correct time.

If you look at the man pages for environ (5) you will note that offset is the value that must be added to local time to arrive at UTC (GMT). Offset takes the format hh[:mm[:ss]]
where (hh)is any value from 0 through 23. The optional minutes (mm) and seconds (ss) fields are a value from 0 through 59. The hour field is required. If offset is preceded by a -, the time zone is east of the Prime Meridian. A + preceding offset indicates that the time
one is west of the Prime Meridian.

Notice, for example that while it is now 0745 hours on February 18 in the Eastern US, you cannot produce a date *and time* exactly 24-hours ago. To affect this, the computation would need to offset 29 hours (24+5), an invalid offset.

Regards!

...JRF...
Eric Ladner
Trusted Contributor

Re: yesterday's date question

This tiny C program will do exactly what you want.

#include
#include

#define SECONDS_IN_A_DAY 86400

int main ()
{
time_t t;
char buffer[BUFSIZ];

time(&t);

t -= SECONDS_IN_A_DAY;

strftime(buffer, BUFSIZ, "%a %b %e", localtime(&t));
printf("%s\n", buffer);

}
SHABU KHAN
Trusted Contributor

Re: yesterday's date question

Hi,

There is a perl one-liner to calculate yesterday's date

/usr/local/bin/perl -e
'@T=localtime(time-86400);printf("%02d/%02d/%02d",$T[4]+1,$T[3],($T[5]+1900)
%100)'

You could change it to display in whatever format you like..

Hope this helps..

-Shabu