• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 965
  • 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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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