Date Time Change for a particular target Zone

Hi

Summary:

  • Script will take 2 arguments
  • Argument 1: Date/Time value in a particular format
  • Argument 2: TimeZone value
Examples:
Argument 1: 
a. "May 11, 2012 08:00:00 AM"
b. "Dec 21, 2012 12:21:12 PM"
c. "Oct 2, 2012 05:00:00 PM"

Argument 2:
a. MT or MST or MDT
b. ET
c. PT or PCT
d. GMT / UTC
e. CT 
d. IST

Where MT is Mountain time, ET is Eastern Time, PT Pacific Time, GMT is Greenwich Mean Time, CT Central time, IST Indian Standard Time... etc etc.

-----------

Now,
What I need is a BASH script to do the following:

The script will take Argument 1 and Argument 2 and convert the Date/Time in the same Date/Time format to GMT time zone.

i.e.

A.
If I'm entering:
$ Script.sh "May 12, 2012 10:00:00 AM" MT
then, 
script should return me GMT value as:
"May 12, 2012 04:00:00 PM"

NOTE: In Daylight/Standard time period, this value can 
change.

B.
If I'm entering: 
$ Script.sh "May 12, 2012 10:00:00 AM" ET
then, 
script should return me GMT value as:
"May 12, 2012 02:00:00 PM" or 

-- whatever is the correct value for this ET is for GMT 
time zone.



C.
If I'm entering: 
$ Script.sh "May 12, 2012 11:00:00 PM" IST
then, 
script should return me GMT value as:
"May 12, 2012 05:30:00 PM"

as IST is +5h:30m ahead of GMT.

GNU date has an -u option to return UTC (GMT) time. Then just specify the time zone with the date and time:

$ date -u -d 'May 12, 2012 11:00:00 PM IST'
Sat May 12 17:30:00 UTC 2012

Hi,

I can achieve it without -u option.Please let me know if I am doing wrong

[stpuser@VHLDV001 ~]$ date "+%B %e, %Y %X" -u -d 'May 12, 2012 11:00:00 PM CST'
May 13, 2012 05:00:00 AM
[stpuser@VHLDV001 ~]$ date "+%B %e, %Y %X"  -d 'May 12, 2012 11:00:00 PM CST'
May 13, 2012 05:00:00 AM
[stpuser@VHLDV001 ~]$ date "+%B %e, %Y %X" -d 'May 12, 2012 11:00:00 PM CST'
May 13, 2012 05:00:00 AM
[stpuser@VHLDV001 ~]$ date "+%B %e, %Y %X" -d 'May 12, 2012 11:00:00 PM IST'
May 12, 2012 05:30:00 PM
[stpuser@VHLDV001 ~]$ date "+%B %e, %Y %X" -d 'May 12, 2012 11:00:00 AM IST'
May 12, 2012 05:30:00 AM
[stpuser@VHLDV001 ~]$ date "+%B %e, %Y %X" -d 'May 12, 2012 11:00:00 AM MDT'
May 12, 2012 05:00:00 PM

That happens to work only because your local timezone is UTC. I am in PST, and so I get

$ date "+%B %e, %Y %X" -d 'May 12, 2012 11:00:00 AM MDT'
May 12, 2012 10:00:00 AM

If you use -u, it will be independent on your local timezone.

Thanks for your support and valuable advise