Solved

Timeshifting dates in AIX...

Posted on 2004-10-04
6
2,466 Views
Last Modified: 2013-12-26

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
0
Comment
Question by:bganoush
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 12217291
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




0
 

Author Comment

by:bganoush
ID: 12218285

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
0
 

Author Comment

by:bganoush
ID: 12219017
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bganoush
ID: 12219410

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


0
 
LVL 9

Accepted Solution

by:
HamdyHassan earned 125 total points
ID: 12221510
good work, ask for refund
0
 

Author Comment

by:bganoush
ID: 12225813

Nah... I don't do refunds!

-- Bubba
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question