[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 21880
  • Last Modified:

Convert GMT to EST or local time

Is there an easy way to convert GMT to say eastern within a shell script?  Can someone provide an example?

thanks
0
jhughes4
Asked:
jhughes4
  • 4
  • 3
1 Solution
 
TintinCommented:
TZ=EST date
0
 
jhughes4Author Commented:
ok so within a shell script how would I conver say the GMT date of 14:09 to EST?

thanks
0
 
TintinCommented:
Do you have an existing date you want to convert, or just convert the current date?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jhughes4Author Commented:
I'll be pulling the date from another application that could be current or several hours past.  The date is in a String format and I was wondering if I could take that String and pass it to something like date to have date convert it to EST or MTN, or whatever the system's time zone is.
0
 
TintinCommented:
Can you please give an example of the format?  Do you want to retain the same format for the converted date?
0
 
jhughes4Author Commented:
06292006:23:31:57:580
mmddyyyy:HH:MM:SS:MS

thanks again
0
 
TintinCommented:
Assuming you have GNU date, and if you don't, you should get it as it is incredibly useful for all sorts of date calculations, you can do

#!/bin/sh
# Example to convert from GMT to EST
old_date='06292006:23:31:57:580'
fold_date=`echo $old_date|sed 's/\(..\)\(..\)\(....\):\(.*\):...$/\3\1\2 \4/'`  # Convert date to a sensible non-ambigious format
new_date=`TZ=EST date --date "$fold_date GMT" +%m%d%Y:%T`
echo $new_date

The above example looses the milliseconds, but it's easy enough to put them back on if required.
0
 
ahoffmannCommented:
do you expect dates from yesterday or tomorrow (seen from your current date)?
Do you expect dates more than +/-24 hours difference?
If so I highly recommend that you use perl with Date::Manip or one of the modules from http://datetime.perl.org/
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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