Operating System - HP-UX
1834771 Members
3190 Online
110070 Solutions
New Discussion

Re: Script using TZ variable

 
A.Vishwanath_2
Advisor

Script using TZ variable

Dear Script Gurus,

I had a script which uses "TZ" variable in order to backdate and check the condition of the backup status and then delete the dump from Disk. After the recent TZ zones the script errors out as there is chaneg in "TZ" variable.

Before DST Changes:
TZ = MET
After DST Changes:
TZ = MET-1METDST

Can some one help me in to backdate the script using the new TZ varaiable.

FYI, In my script in oredr to backdate i was using.

TZ_OLD="$TZ+24"

TIA,
Vishwanath
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Script using TZ variable

Hi:

The use of the following is commonly seen to compute tomorrow's date:

# TZ=GMT-24 date +%m/%d/%y

...and correspondingly, yesterdays's date:

# TZ=GMT+24 date +%m/%d/%y

Eurpopeans,living around the Prime Meridian are the "lucky" ones who can use +-24 hours to exactly compute yesterday or tommorrow *including* the correct time.

As noted by the man pages for 'environ(5)', the offset is the value that must be added to local time to arrive at UTC (GMT). The 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 is one that is west of the Prime Meridian.

Thus, you should beware when trying to perform such trickery as you describe.

A rigorous, safe method to find the day as it was 24-hours ago in your local timezone is:

# perl -MPOSIX -le 'print strftime(%A,localtime(time-(24*60*60)))'

You can use any of the 'strftime(3C)' formatting directives you want.

If you want the full date for yesterday, simply do:

# perl -le 'print scalar localtime(time-24*60*60))'

Regards!

...JRF...
A.Vishwanath_2
Advisor

Re: Script using TZ variable

Dear James,

Thanks for the quick solution.
I wanted to know is there any posibility of going backdated by two days using "TZ" variable.

For Ex:
Today 06/Nov/2006
I have to go to 04/Nov/06.

The above solution from you solves the problem.

TIA,
Vishwanath
Bill Hassell
Honored Contributor

Re: Script using TZ variable

You wrote:

> Before DST Changes:
> TZ = MET
> After DST Changes:
> TZ = MET-1METDST

This does not make any sense. TZ=MET means there is no offset from UTC (or GMT) time. So the value TZ=GMT0 and TZ=MET produce the same result. And because there is no daylight saving extension, TZ=MET never changes time. It looks like someone misunderstood the purpose of the TZ variable and the /usr/lib/tztab file. If your timezone is 1 hour to the east of GMT and 2 hours east of GMT during daylight saving, then the only value for TZ should be MET-1METDST. The man page for environ and the comments in tztab will help. The global value for TZ in in /etc/TIMEZONE.


Bill Hassell, sysadmin
Hein van den Heuvel
Honored Contributor

Re: Script using TZ variable

Vishwanath,

Do yourself a favor and forget about using TZ as a method to manipulate dates. It's a hack! It's error prone. Learn to do it properly

Google the itrc with something like:

+site:itrc.hp.com +yesterday +script
or
+site:itrc.hp.com +calculate +date +script +hpux
or...
+site:itrc.hp.com +caljd

That caljd is a very universal script.

Personally I use short perl scripts and time/localtime adding or subtrating 86400 seconds for each day.

Enjoy!
Hein.
A.Vishwanath_2
Advisor

Re: Script using TZ variable

Thanks to all Gurus.
As mentioned i have first solution provided by James. I am currently in process of learning PERL scripting. Hope my life will be easier once i am done.