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 | cut -c3-4`
logyy=`echo $RAW_TME | cut -c5-8`
logth=`echo $RAW_TME | cut -c9-10`
logtm=`echo $RAW_TME | cut -c11-12`
GLOG_TME="$logyy-$logmm-$logdd $logth:$logtm:00 GMT"
t=$(mktemp)
O_DDD_T=`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/..........$//'
)`
echo $O_DDD_T
Output is :
2017-04-27 10:30:00 CST
Is there a leaner easier way to do this ?
With a recent version of ksh
(on a system that doesn't have a GNU date
utility) in a terminal session where the default timezone is the US Pacific timezone, the following:
Date_GMT=042720170740
TZ=CST6CDT printf '%(%m%d%Y%H%M)T\n' "${Date_GMT:0:8} ${Date_GMT:8:2}:${Date_GMT:10} GMT"
produces the output:
042720170240
So, with a GNU date
utility on a Linux system using a recent bash
, the following might work:
Date_GMT=042720170740
TZ=CST6CDT date -d "${Date_GMT:0:8} ${Date_GMT:8:2}:${Date_GMT:10} GMT" '%m%d%Y%H%M'
If the timezone being used in your terminal session is already set to use US Central time zone, you can drop the TZ=CST6CDT
at the start of the date
command line.
PS: Note that a timestamp of 0740 GMT
will NOT convert to a timestamp of 10:30:00 CST
; with the given date, it would either convert to 2017-04-27 01:40:00 CST
or, if you aren't in the parts of Indiana that don't observe daylight saving time, 2017-04-27 02:40:00 CDT
. Getting this date format would need the date format string:
'%Y-%m-%d %H:%M:%S %Z'
instead of:
'%m%d%Y%H%M'
Getting this error:
Date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
You filed this question in the Linux forum. What operating system (including the distribution and version number) are you using?
My bad. it is solaris 10 that I am using.