I have a log file from where I get above CDT time 'Input', want to convert it to below format EDT time
Required output: 8/6/16 11:45 PM
#I want to get "23:45:36" in below HMS variable but it has issue
HMS=$(awk -v val=$Input '{print $2,val})
CT=`date +%Z`
if [ $CT -eq "EST" ]; then
#if EST then it is same CST time, so just formate change
F2=$(date +%r --date="$HMS" |cut -c 1-5,9-11)
F1=$(awk -F"-" -v val1=Input '{print $3"/"$2"/"$1,val1}')
echo $F1 $F2> $ET
else
#if CDT then one hour to be added to match EDT time
F1=$(awk -F"-" -v val1=Input '{print $3"/"$2"/"$1,val1}')
F2=$(date +%r --date="$HMS" "+1 hour" | cut -c 1-5,9-11)
echo $F1 $F2 >$ET
fi
The time increment and formatting not working properly, need help to get the output: 8/6/16 11:45 PM.
First, why did you change LC_ALL in RudiC's suggestion to
CL_ALL
?
Second, why did you change:
+"%-m/%-d/%y %I:%M %p"
to:
+"%-m/%-d/%y/ %I:%M %p"
? Do you really want to print a <slash> character after the year in your output?
Third, telling us that you use bash does not tell us whether or not the date utility on your operating system supports a -d option that will behave this way. The date -d option on my system does not behave this way on the system I'm using so I can't verify this, but I would think you want something more like:
LC_ALL=C date -d"$Input +1 hour" '+%-m/%-d/%y %I:%M %p'
Fourth, the test operator for string comparison is = , not -eq (which is for numeric comparisons) and, even if it did work, your test would only work when daylight saving time is not in effect. Maybe:
if [ $CT -eq "EST" ]; then
would come closer to doing what you want if you used: