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

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:

printf '%(%Y-%m-%d %H:%M:%S %Z\n)T' "2016-12-30 23:50:33 GMT"

produces the output:

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).

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:

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:

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

Ksh, bash, all posix-sh you can use builtin substr:

logmm=${NLOG_TME:0:2}
logdd=${NLOG_TME:2:2}
# ...
# no need echo, simply:
NLOG_TME="$logyy-$logmm-$logdd $logth:$logtm:$logts"

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 ?

Using ksh93:

#!/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

It does not work:

I get this error "

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

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:

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:

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

Hi.

Running:

date -d "2016-12-30 23:50:33 GMT"

produces:

Fri Dec 30 17:50:33 CST 2016

For my time zone (Central).

In your case you would probably do something like:

date -d "$( cat $DATA/filein.xml )"

Looking at the man page for date should allow you to format it any way you want.

This was done in the environment:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
date (GNU coreutils) 8.23

There is also:

dateutils.dconv Convert DATE/TIMEs between calendrical systems. (man)
Path    : /usr/bin/dateutils.dconv
Version : 0.3.1
Type    : ELF 64-bit LSB shared object, x86-64, version 1 (S ...)
Help    : probably available with -h,--help
Home    : https://github.com/hroptatyr/dateutils

As well as on a Solaris system:

gdate -d "2016-12-30 23:50:33 GMT"

producing:

Fri Dec 30 17:50:33 CST 2016

For a system like:

OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Solaris 11.3 X86
gdate date (GNU coreutils) 8.16

Best wishes ... cheers, drl

1 Like

Solaris 10 ksh was ksh88 variant, not ksh93. If I remember, dtksh is also available - a sort of proto-version of ksh93. (corrected - thanks jlliagre)

If you have installed gnu coreutils:

date +'%Y-%m-%d %H:%M:%S %Z'  --date='TZ="America/Chicago" 2016-12-30 23:50:33 GMT'
2016-12-30 15:50:33 CST

1 Like

Dtksh has no relationship with zsh.
It is actually the original version of ksh93 (M-12/28/93d) with added builtin support for X11 and Motif.

1 Like

Hi mrn6430,
There are so many things wrong here that I don't know where to start...

In post #3 you said:

Note that your code sets a variable named LOG_TIME to a value containing four strings with each string separated by a <space> character which you then describe (in the following comment) as a single string containing no <space>s???

Then you use cut to extract six fields from a variable named NLOG_TIME (which has not yet been set) and use the values you extracted from that unset variable to assign a value to NLOG_TIME which the following comment says is now in the form ccyy-mm-dd hh:mm:ss , but in all likelihood is instead the fixed string -- :: .

Note also that at this point, you still hadn't answered any of my questions in post #2 in this thread:

Since you chose not to answer those questions, kshji tried twice to help you with suggestions that would work just fine with a 1993 or later version of the Korn shell, but will not work at all on the 1988 version of the Korn shell you're using.

Hi kshji,
The substring variable substitutions are present in many shells that meet POSIX shell requirements, but they are not required by the POSIX standards. (And, they are not present in ksh versions before 1993.)

Hi again mrn6430,
Of course it fails. As I said in post #2, the printf statement I suggested only works with a 1993 or later version of ksh . The @(#)Version M-11/16/88i version string from your Korn shell, shows that you are using a November 1988 version of ksh which does not know how to process printf '%(format)T' format conversions. (In fact, just knowing that ksh --version gives you no output tells us that your version of ksh is a 1988 or earlier version.)

You can try running your script with /usr/dt/bin/dtksh instead of ksh and see if that works. (Note that /usr/dt/bin/dtksh probably won't be present unless your sysadmin loaded the CDE option when they installed the Solaris System on your server.) Or, you can look for gdate . /usr/gnubin/date , or /usr/gnu/bin/date and use the suggestions provided by drl and jim mcnamara. But, none of these will work until you correctly set NLOG_TIME . (All three of the names listed above were used in various Solaris 10 installation options and your sysadmin might not have loaded any of them.) If you don't have a GNU date and don't have dtksh , you can write an easy C program to do the conversion for you that will work on any Solaris 10 system.

I've already explained why NLOG_TIME is not being set correctly, but there are probably ways to much more simply set NLOG_TIME than the code you have shown us so far. If you show us the contents of the file named by pathname named by the expansion of $DATA/filein.xml , we can probably give you a way to set NLOG_TIME much simpler and more efficient than what your script is currently doing.

1 Like

Actually not, the original ksh93 (thus dtksh ) didn't implement --version yet.

The original ksh93 wasn't supporting either printf as a builtin. It was introduced, including the %T format specification, in ksh93h released in 1999.

CDE is likely there, as it is installed by default under most Solaris 10 instances. Only minimized, core system/reduced network rare installation are missing it.

/usr/gnu is a Solaris 11 directory. On Solaris 10, gnu utilities are under /usr/sfw/bin but unfortunately, GNU date is not part of them. It might have been installed by the administrator from a freeware repository and in such case, might be under /usr/local/bin or /usr/csw/bin .

Here is one way to do it using Solaris 10 standard tools:

#!/bin/ksh
d="2016-12-30 23:50:33 GMT"
t=$(mktemp)
TZ=Z touch -t $(echo "$d" | sed -e 's/ GMT$//' -e 's/[ :-]//g' -e 's/\(..\)$/.\1/') $t
(
  set -- $(ls -E $t)
  rm $t
  echo $6 $7 | sed 's/..........$//'
)

Output (in CET timezone):

2016-12-31 00:50:33
1 Like

@jlliagre - thanks for the correction, I typed zsh in error.

1 Like

Here is the final code that works with my standard ksh version. It is working great now. Thank you all for your help.

RAW_TME=`perl -nle '/<TIMESTAMP>([\d-]*)</ and print $1' $DATA/MY_PRA.xml`
logmm=`echo $RAW_TME  | cut -c1-2`
logdd=`echo $RAW_TME  | cut -c3-4`
logyy=`echo $RAW_TME  | cut -c5-8`
logth=`echo $RAW_TME  | cut -c9-10`
logtm=`echo $RAW_TME  | cut -c11-12`
logts=`echo $RAW_TME  | cut -c13-14`
GLOG_TME="$logyy-$logmm-$logdd $logth:$logtm:$logts GMT"
t=$(mktemp)
NLOG_TME=`TZ=Z touch -t $(echo "$GLOG_TME" | sed -e 's/ GMT$//' -e 's/[ :-]//g' -e 's/\(..\)$/.\1/') $t
(
  set -- $(ls -E $t)
  rm $t
  echo $6 $7 | sed 's/..........$//'
)`

Output this time:

GLOG_TME = 2016-12-30 16:20:11 GMT
LocalTime = 2016-12-30 10:20:11