date command issue from crontab

Hi Expert,

I am using TZ for extracting yesterday date and day before yesterday date
example :

date_yes=`TZ="GMT+28" date +'%d-%b-%Y'`
date_dbyes=`TZ="GMT+48" date +'%d-%b-%Y'`
 
echo $date_yes $date_dbyes
 
26-May-2010  27-May-2010

I have written a small script for the same named as test.sh

when i ran the script it will give me correct result. Issue is i schduled this script in crontab (it will run for everyday at 1 a.m )

00 01 * * * sh test.sh

But i am getting 25-May-2010 26-May-2010 in two variables, i am stuck for the day. I am looking into this for last 4 hours.

Please help me out.

Mumbai time is UTC/GMT plus 5:30.
01:00 in Mumbai is 19:30 Greenwich Mean Time (GMT) "yesterday".

Assuming that your cron runs in Mumbai Time the definition of "yesterday" and "day-before-yesterday" when expressed in Greenwich Mean Time (GMT) changes according to the time parameter of the crontab entry.

I don't think that changing the TZ variable in this manner is a valid technique outside of the GMT timezone. You need to be working in the same timezone as cron.

Depending on what Operating System and Shell you have there are published techniques for date arithmetic for non-GMT timezones.

Alternatively you could test the cron every hour for 24 hours and use this information to work out a simple adjustment.

Afterthought. You could create a cron for 00:01 to create a file for "today" and age the file daily to produce "yesterday" and "day-before-yesterday". With forward planning you will have all the reference dates available after three days. The content of each file can be the output from "date" in whatever format you require. This will be a lot less hassle than date arithmetic.