Code to convert CDT to EDT


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code to convert CDT to EDT
# 1  
Old 06-27-2016
Code to convert CDT to EDT

Code:
Input=$(awk -F="," '/pattern/ {print $1}' logfile | tail -1)
Input=2016-06-08 23:45:36

I have a log file from where I get above CDT time 'Input', want to convert it to below format EDT time
Code:
Required output: 8/6/16 11:45 PM

#I want to get "23:45:36" in below HMS variable but it has issue
Code:
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.
# 2  
Old 06-27-2016
It's difficult to tell if your date version allows for the -d, --date= argument or not. If it does, try
Code:
LC_ALL=C date -d"$Input" +"%-m/%-d/%y %I:%M %p"
6/8/16 11:45 PM

For your HMS problem, try
Code:
HMS=${Input#* }

# 3  
Old 06-27-2016
I am using bash

When we get CDT time from log, the time difference to EDT will be one hour. During that time increment, can we do the below?
Code:
CL_ALL=C date -d"$Input" +"%-m/%-d/%y %I:%M %p" "+1 hour"


Last edited by rozee; 06-27-2016 at 11:13 PM..
# 4  
Old 06-27-2016
Quote:
Originally Posted by rozee
I am using bash

When we get CST time from log, the time difference to EST will be one hour. During that time increment, can we do the below?
Code:
CL_ALL=C date -d"$Input" +"%-m/%-d/%y/ %I:%M %p" "+1 hour"

First, why did you change LC_ALL in RudiC's suggestion to
Code:
CL_ALL

?

Second, why did you change:
Code:
+"%-m/%-d/%y %I:%M %p"

to:
Code:
+"%-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:
Code:
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:
Code:
if [ $CT -eq "EST" ]; then

would come closer to doing what you want if you used:
Code:
if [ "$CT" = "EST" ] || [ "$CT" = "EDT" ]; then

unless you live in some parts of Indiana.
# 5  
Old 06-28-2016
Thanks RudiC and Don.... Solution worked... Also I had another time conversation to take care in this below formate
Code:
CT="Wed Jun 22 22:20:45 CDT 2016

And used this code and it converted to my system EDT time, with addition of 1hour
Code:
 date --date="$CT" +"%-m/%-d/%y %I:%M %p"

Just want to know above code is fine in all 365 days or any other better way to handle this?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to convert LDIF to CSV

Team , I am new to perl. Can someone help me out to convert LDIF file to csv format using perl logic ? LDIF file format contains values : ID : name/Bose-IND/ORD,cu-150,ou=ends Connection_Time: 02:12:54 EST LDAP: yes Link: ABC Home Page - ABC.com Expected outcome should with column... (0 Replies)
Discussion started by: Perlbaby
0 Replies

2. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

3. Shell Programming and Scripting

How to convert a file containing hex code to decimal using script?

The file contains code like the below and need to convert each one into a decimal 00 00 00 04 17 03 06 01 So the output should come as 0 0 0 4 23 3 6 1 (24 Replies)
Discussion started by: necro98
24 Replies

4. Shell Programming and Scripting

Convert decimal notation to ANSI point code notation

wondering if anyone has any thoughts to convert the below thru a shell script Convert decimal signalling point notation to ANSI point code notation There is a site that does that conversion but i need to implement the solution in a shell script.....Thoughts.... OS: Solaris 9 ... (4 Replies)
Discussion started by: aavam
4 Replies

5. Shell Programming and Scripting

convert perl code to shell script

This is about how to Monitoring folder for new files using shell script im doing a project using smsserver tools 3. i have used a perl script to handle incoming messages. the content of each message must be directed to a java program. this program generates the answer to reply to the user... (2 Replies)
Discussion started by: x34
2 Replies

6. Shell Programming and Scripting

python code...convert to ksh or sh

Helloo... I am not much familiar with python..I found this small script in python I even do not have python on my computer...can anyone help me out to convert this into ksh or sh.. PLEASE any help I will appreciate.. here is python code.. #!/usr/bin/env python import random # Get a... (3 Replies)
Discussion started by: amon
3 Replies

7. Shell Programming and Scripting

how to convert from korn shell to normal shell with this code?

well i have this code here..and it works fine in kornshell.. #!/bin/ksh home=c:/..../ input=$1 sed '1,3d' $input > $1.out line="" cat $1.out | while read a do line="$line $a" done echo $line > $1 rm $1.out however...now i want it just in normal sh mode..how to convert this?... (21 Replies)
Discussion started by: forevercalz
21 Replies

8. Shell Programming and Scripting

here-doc convert 2 script convert to single script?

I have my main script calling another script to retrive a "ls -alt" of a directory that's located in a remote location I'm sftping into. main.sh #!/bin/ksh getLS.sh > output.txt getLS.sh #!/bin/sh /home<..>/sftp <host@ip> <<! cd /some/dir/Log ls -alt quit ! Basically I'd like to be... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question