GMT to local Time conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting GMT to local Time conversion
# 1  
Old 12-30-2016
GMT to local Time conversion

How can I convert the following date format:

New Log Date = 2016-12-30 23:50:33 GMT

from GMT time to local time?


Thanks
# 2  
Old 12-31-2016
What operating system are you using?

What output do you get from the command: date -"?"?

What shell are you using?

If you are not using a 1993 or later version of the Korn shell, does your system have one that you can use? (What output do you get from the command: ksh93 --version || ksh --version?)

On the system I'm using, with ksh --version returning 93u+, the command:
Code:
printf '%(%Y-%m-%d %H:%M:%S %Z\n)T' "2016-12-30 23:50:33 GMT"

produces the output:
Code:
2016-12-30 15:50:33 PST

which seems to be what you would want if you were located in the United States Pacific Standard timezone (which is where I'm located).
# 3  
Old 12-31-2016
It is stored in a file originally in this format:
mmddccyyHHMMSS in a GMT format.
I storing the value from the file into a field:

Code:
LOG_TME=`perl -nle '/<TIMESTAMP>([\d-]*)</ and print $1' $DATA/MY_PRA.xml`
## Out put format is as follows: mmddccyyHHMMSS
## Then I am reformatting the it to be as follows:
logmm=`echo $NLOG_TME  | cut -c1-2`
logdd=`echo $NLOG_TME  | cut -c3-4`
logyy=`echo $NLOG_TME  | cut -c5-8`
logth=`echo $NLOG_TME  | cut -c9-10`
logtm=`echo $NLOG_TME  | cut -c11-12`
logts=`echo $NLOG_TME  | cut -c13-14`
NLOG_TME=`echo $logyy-$logmm-$logdd $logth:$logtm:$logts`
## Output is as follows:  ccyy-mm-dd hh:mm:ss

At this point the output value is still in GMT. I tried your code as follows:
Code:
NLOG_TME2=`printf '%(%Y-%m-%d %H:%M:%S %Z\n)T' "$LOG_TME GMT"`

I get a value stored in NLOG_TM2 = (%m-0 %M:%Z)T


Thanks

Last edited by mrn6430; 01-01-2017 at 03:33 AM.. Reason: Pasted the incorrect LOG_TME= line.
# 4  
Old 12-31-2016
Ksh, bash, all posix-sh you can use builtin substr:
Code:
logmm=${NLOG_TME:0:2}
logdd=${NLOG_TME:2:2}
# ...
# no need echo, simply:
NLOG_TME="$logyy-$logmm-$logdd $logth:$logtm:$logts"

# 5  
Old 12-31-2016
But how do I convert that GMT time mmddccyyhhmmss format stored in variable LOG_TME from my code above to CST time or local time and store that it NLOG_TME variable ?
# 6  
Old 12-31-2016
Using ksh93:
Code:
#!/usr/bin/ksh
# 2016-12-30 16:20:11
LOG_TME="12302016162011"
NLOG_TME="${LOG_TME:4:4}-${LOG_TME:0:2}-${LOG_TME:2:2} ${LOG_TME:8:2}:${LOG_TME:10:2}:${LOG_TME:12:2}"
# or
logmm=${LOG_TME:0:2}
logdd=${LOG_TME:2:2}
logyy=${LOG_TME:4:4}
logth=${LOG_TME:8:2}
logtm=${LOG_TME:10:2}
logts=${LOG_TME:12:2}
NLOG_TME="$logyy-$logmm-$logdd $logth:$logtm:$logts"
#

echo "$NLOG_TME"
# 2016-12-30 16:20:11
NLOG_TME2=$(printf "%(%Y-%m-%d %H:%M:%S %Z)T" "$NLOG_TME GMT")
echo "$NLOG_TME2"
#2016-12-30 18:20:11 STD

# 7  
Old 12-31-2016
It does not work:

I get this error "
Code:
testzone.ksh[4]: NLOG_TME="${LOG_TME:4:4}-${LOG_TME:0:2}-${LOG_TME:2:2} ${LOG_TME:8:2}:${LOG_TME:10:2}:${LOG_TME:12:2}": bad substitution

"

Note: When I do ksh --version nothing is returned. So ran this command to find out the ksh version
Code:
strings /bin/ksh | grep Version | tail -2

on my solris 10 unix server and the version is : @(#)Version M-11/16/88i


To bypass this issue I did this:

Code:
LOG_TME="12302016162011"
logmm=`echo $LOG_TME  | cut -c1-2`
logdd=`echo $LOG_TME  | cut -c3-4`
logyy=`echo $LOG_TME  | cut -c5-8`
logth=`echo $LOG_TME  | cut -c9-10`
logtm=`echo $LOG_TME  | cut -c11-12`
logts=`echo $LOG_TME  | cut -c13-14`
NLOG_TME="$logyy-$logmm-$logdd $logth:$logtm:$logts"

NLOG_TME2=$(printf "%(%Y-%m-%d %H:%M:%S %Z)T" "$NLOG_TME GMT")
echo "$NLOG_TME2"

However, when I run it it does not work with the printf provided:

Here is the output:

Code:
2016-12-30 16:20:11
(%m-0 %M:%Z)T


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-01-2017 at 06:39 AM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Convert GMT date and time to CST

I need away to convert the following GMT date and time value RAW_TME= 042720171530 "mmddccyyhhmm" to Localhost time. In this case it is in central time. Here is what I came up with but it does not look efficient: RAW_TME=042720171530 logmm=`echo $RAW_TME | cut -c1-2` logdd=`echo $RAW_TME |... (4 Replies)
Discussion started by: mrn6430
4 Replies

2. Shell Programming and Scripting

Local time to GMT time

Gents, Please can help with this. the column in red is the local time the column in blue is the GPStime 4153152529951 2/12/17 12:00:04.980 951 2960 41531.0 52529.0 1170882022980002 4108153261942 2/12/17 12:00:07.944 942 2959 41081.0 53261.0 1170882025944002 41511523611660... (7 Replies)
Discussion started by: jiam912
7 Replies

3. Shell Programming and Scripting

GMT to MST timestamp conversion

Hi Team, We have written a perl script to perform the GMT to MST timestamp conversion. Input: 2013-12-01T05:23:19.374 Output: need the given timestamp in MT (MST/MDT) #!/usr/bin/perl use strict; use warnings; use Time::Local; #always gmt #my $tval = '2013-12-01T05:23:19.374'; ... (4 Replies)
Discussion started by: kmanivan82
4 Replies

4. Post Here to Contact Site Administrators and Moderators

Server Upgrade Scheduled for Today - 7:30 GMT (2:30 PM Eastern Time)

Dear All, Today, somewhere around 7:30 GMT (2:30 PM Eastern Time) www.unix.com will go down for what we hope is around 15 - 20 minutes as we change out some hardware on the server. Thank you for your patience and support. Neo (0 Replies)
Discussion started by: Neo
0 Replies

5. UNIX for Dummies Questions & Answers

UTC time and Local time

Hi, A few days ago I changed my CentOS box's timezone to -07:00. Now the date commands output look like this (run almost simultaneously, less than 1 second delay).. # date Mon Sep 5 20:23:40 PDT 2011 # date -u Tue Sep 6 03:24:05 UTC 2011 The hours difference seems correct, but why is... (2 Replies)
Discussion started by: forte712
2 Replies

6. AIX

Convert UTC time to local time ?

Hello, Using AIX6.1 box. I have UTC time value and need to convert it to local time value - I mean time zone and DST should be taken into consideration. I hope it could be done using shell environment - I don't want to write a program. thanks Vilius ---------- Post updated at 02:30 PM... (2 Replies)
Discussion started by: vilius
2 Replies

7. HP-UX

change time mode from BST to GMT

I want to know how to change the time zone from BST to GMT avoid the daylight savings in hp-ux (3 Replies)
Discussion started by: tomjones
3 Replies

8. Shell Programming and Scripting

Script to convert GMT to Asia/Hong Kong time

hi friends, this is my first time with this type of script so please pardon my ignorance. i have this script in which a piece of code needs to be added which can have the Asia/Hong kong time as well. system date and format is GMT so no problem with GMT and even EST is covered..i have ato add new... (5 Replies)
Discussion started by: xejatt
5 Replies

9. Shell Programming and Scripting

conversion from EPOCH timestamp to local time zone

hello gurus, i want a perl/shell script which once invoked should convert a set of EPOCH timestamps to local time ( IST..i want) . how does it work ,i have an idea on that..but writing a perl/shell script for it is not possible for me...so i need help for the same. my exact requirement is... (2 Replies)
Discussion started by: abhijeetkul
2 Replies
Login or Register to Ask a Question