• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 955
  • Last Modified:

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.
0
chanikya
Asked:
chanikya
  • 6
  • 5
  • 3
2 Solutions
 
woolmilkporcCommented:
Which is your OS? If you have GNU date:

yesterday=$(date -d "yesterday" "+%Y%m%d%H%M")
0
 
chanikyaAuthor Commented:
Hi Expert

My OS is "SunOS 5.10"

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

http://www.sunfreeware.com/indexsparc10.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
chanikyaAuthor Commented:
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.
0
 
woolmilkporcCommented:
Can you try perl?
yesterday=$(perl -e '@y=localtime(time()-86400);printf "%04d%02d%02d%02d%02d",$y[5]+1900,$y[4]+1,$y[3],$y[2],$y[1];')

Open in new window

Found it on the Web ...
0
 
ozoCommented:
if your locale implements Daylight Saving Time
there may be 2 hours per year when
  time()-86400
is not yesterday
0
 
woolmilkporcCommented:
Good point!
0
 
chanikyaAuthor Commented:
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.
0
 
woolmilkporcCommented:
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.
0
 
ozoCommented:
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]'
0
 
chanikyaAuthor Commented:
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.
0
 
woolmilkporcCommented:
It will work regardless of DST being in effect or not.
0
 
ozoCommented:
yesterday=`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]'`
will work at all times, unless your locale shifts DST by more than 10 hours from standard times
or unless the command takes more than 10 hours to execute
0
 
chanikyaAuthor Commented:
Both are more or less like similar solutions I am accepting both the solutions. But woolmilkporc suggested the first perl solution.

Thanks,
Chanikya.
0

Featured Post

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!

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now