Link to home
Start Free TrialLog in
Avatar of chanikya
chanikya

asked on

How to convert date timezone in unx

Hi Experts,

The below i have script to find the yesterday.

-bash-3.00$ yesterday=`TZ=GMT+28 date +%Y%m%d%H%M`
-bash-3.00$ echo $yesterday
201208271007
-bash-3.00$

But the problem is , The server is on EST zone.  it is GMT -4.00 hr now.. If the day light saving changes it will GMT -2.50 hr only.. somehow dynmically i have to handle it.

=`TZ=GMT+28 date +%Y%m%d%H%M`

Can you please help me in this.

Thanks
Chanikya.
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Which is your OS? If you have GNU date:

yesterday=$(date -d "yesterday" "+%Y%m%d%H%M")
Avatar of chanikya
chanikya

ASKER

Hi Expert

My OS is "SunOS 5.10"

Thanks
Chanikya.
GNU date is in the "coreutils" package, available for Solaris here:

http://www.sunfreeware.com/indexsparc10.html
I am sorry, I can not do anything at OS level installation/upgradation/patch etc..

I have to just make use of existing features.

Thanks
Chanikya.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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
if your locale implements Daylight Saving Time
there may be 2 hours per year when
  time()-86400
is not yesterday
Good point!
Hi  ozo,

My Problem is, i need to find out yesterday date. And it is working for me
yesterday=`TZ=GMT+28 date +%Y%m%d%H%M`.

Here you see GMT+28 =GMT+24+4 . The server is in phildelphia (EST time zone) i.e. in GMT -4.00 hrs.  Hence Adding 4 to 24 hours to get the correct output .

After few months once day light saving changes i have to manually change the script .
yesterday=`TZ=GMT+26.5 date +%Y%m%d%H%M`.   Becuase it will become GMT - 2.50 hrs .

Then it gives the correct output.

But i donot want to change it manually.

Hope it will clear my problem.

Thanks
Chanikya.
Did you try my perl solution?

As long as you don't run your script during those 2 x 1.5 hours of DST transition it will work just fine.
perl -e '@t=localtime;@y=localtime(time()-86400+(12-$t[2])*3600);printf "%04d%02d%02d%02d%02d",$y[5]+1900,$y[4]+1,$y[3],$t[2],$t[1]'
Hi  woolmilkporc,

I have tried your option . but do you think it will work even day light saving changes. Because i can not test now..

Hi ozo,

I have to use that code in my shell script. you mean can i use the above command which you sent.

Thanks
Chanikya.
It will work regardless of DST being in effect or not.
SOLUTION
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
Both are more or less like similar solutions I am accepting both the solutions. But woolmilkporc suggested the first perl solution.

Thanks,
Chanikya.