Link to home
Start Free TrialLog in
Avatar of bganoush
bganoush

asked on

Timeshifting dates in AIX...


I wrote this little script that is supposed to return the date 7 days ago from the current date and time but for some reason, it ignores the timezone and only returns the time in GMT... what am I doing wrong???

timezone=`date +%Z`
echo $timezone
lastweek=`TZ=$timezone+168 date +"%Y-%m-%d-%H.%M.%S"`
echo Last Week: $lastweek

output looks like this:

EDT
Last Week: 2004-09-27-13.02.25

(The time should be 9:02 a.m.)

-- Bubba
Avatar of HamdyHassan
HamdyHassan

If the time from seven days ago should be exactly the same as the time now

then , use the current time

To get the current time, use date command




Avatar of bganoush

ASKER


When did I say I want to get the time for seven days ago????  I want to get the DATE for seven days ago...

If you were familiar with dates and times, you would know that the date seven days before say... 11:00 p.m. on the 8th is 11:00 p.m. on the 1st... but when the date is taken from GMT, seven days before 11:00 p.m. on the 8th becomes 3:00 a.m. on the 2nd... The 2nd is NOT seven days before the 8th... If I do as you say, by getting the current time, I have the exact same problem... because 11:00 p.m. (current time) on the 2nd is exactly 6 days before 11:00 p.m. on the 8th. and please don't suggest that I just subtract 1 from the date...

What I am trying to get is the first case without any GMT conversions...

-- Bubba
Ok,

I solved it using this code...

edt=`date +%H`
if [ $cmt -lt $edt ]; then
   cmt=`expr $cmt + 24`
fi
timediff=`expr $cmt - $edt`
echo $timediff
timezone=`expr $timediff + 168`
lastweek=`TZ=+$timediff date +"%Y-%m-%d-%H.%M.%S"`
echo Last Week: $lastweek

The way it works is that I calculate the time offset by getting the CMT hour and the EDT (my default time zone) hour and then I subtract the two. The only exception is when the CMT bumps over midnight when I need to add 24 to the CMT so that my subtraction is not negative and so that the result makes sense.  Then when I get the date, I add that offset to the offset for a week's worth of hours and then I sit back and listen to the fat lady sing!

Who wants the points?

-- Bubba

I missed copying one line at the beginning:

cmt=`date -u +%H`
edt=`date +%H`
if [ $cmt -lt $edt ]; then
   cmt=`expr $cmt + 24`
fi
timediff=`expr $cmt - $edt`
echo $timediff
timezone=`expr $timediff + 168`
lastweek=`TZ=+$timediff date +"%Y-%m-%d-%H.%M.%S"`
echo Last Week: $lastweek


ASKER CERTIFIED SOLUTION
Avatar of HamdyHassan
HamdyHassan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Nah... I don't do refunds!

-- Bubba