Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I convert a value that is in GMT time to EST? 1

Status
Not open for further replies.

dgoph

Programmer
Jan 7, 2002
3
0
0
US
I am trying to format a status command from a vendor into a more readable form. Part of the output gives the time of a last transaction in GMT, which will do our operators/programmers no help whatsoever if when the last transaction was last received. How do I convert this time into EST time, especially taking into consideration Daylight Savings?

Example of date/time format received:

01/30/2002 16:18:25

The current time is actually 10:18 A.M.

Thank you for your help.
 
Take a look at the man pages relating to the date command, take note of the environment variable TZ (time zone), not to sure it will help as date and time calculation always beat me!!
 
have a look at the FAQ on this subject in the General Unix forum Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
gnudate is the best command for date manipulation. It has many built in functions for date modifications. Much more robust then the built in date command.
 
My impression is that the date command cannot accept input, so I am not sure how I can utilize that to convert a value given to me by a vendor program. I may be missing something.

Instead, I have managed to write a script that seems to work and I will post it here for either future reference for others, or input from others (some parts aren't very elegant - such as finding the last day of the previous month if the conversion puts you back a day on the first day of a new month).

I had written this script before seeing the gnudate suggestion, so I am not sure if that is also a viable option. Does that accept an input date for conversion?

Anyway, here's my script. Thanks for all who had input.

Remember the input comes in the format of
"03/01/2000 22:12:37"

## timezone.sh
## -------------
##
## This program converts the input date/time (in MM/DD/CCYY hh:mm:ss format)
## from GMT to the local timezone based on the difference of the output from
## the date command for GMT and the local timezone.
##
## Usage:
## timezone.conv [MM/DD/CCYY hh:mm:ss]
## ---------------------------------------------------------------------------
## Modification History:
## ---------------------
## DMG 02/05/2002 Creation
##
## ---------------------------------------------------------------------------
##
## Load input variables
##

PDATE=$1
PTIME=$2

##
## Breakdown date and time into subfields
##

PMONTH=`echo $PDATE | awk -F "/" '{print $1}'`
PDAY=`echo $PDATE | awk -F "/" '{print $2}'`
PYEAR=`echo $PDATE | awk -F "/" '{print $3}'`
PHOUR=`echo $PTIME | awk -F ":" '{print $1}'`
PMINUTE=`echo $PTIME | awk -F ":" '{print $2}'`
PSECOND=`echo $PTIME | awk -F ":" '{print $3}'`

##
## Determine the difference between GMT and local time on the system
##

HOUR=`date +"%H"`
HOURGMT=`date -u +"%H"`

HOURDIFF=`expr $HOURGMT - $HOUR`

if [ $HOURDIFF -lt 0 ]
then
HOURDIFF=`expr $HOURDIFF + 24`
fi

##
## Calculate the last transaction time based on the HOURDIFF variable
##

PHOUR=`expr $PHOUR - $HOURDIFF`

##
## If converted hour is less than zero, it is the previous day. Adjust hour
## value and day value.
##

if [ $PHOUR -lt 0 ]
then

PHOUR=`expr $PHOUR + 24`
PDAY=`expr $PDAY - 1`

##
## If converted day is 0, then it is the last day of previous month.
## Adjust day value and month value.
##

if [ $PDAY = 0 ]
then
PMONTH=`expr $PMONTH - 1`

##
## If converted month is 0, then it is December of previous
## year. Adjust month value and year value.
##

if [ $PMONTH = 0 ]
then
PMONTH=12
PYEAR=`expr $PYEAR - 1`
fi

##
## Find last day of previous month.
##

CALDAY=`cal $PMONTH $PYEAR`
for MONTHDAY in $CALDAY; do
LASTDAY=$MONTHDAY
done

PDAY=$LASTDAY

fi

fi

##
## Parse converted time back together into original format
##

CONVTIME=`echo $PMONTH"/"$PDAY"/"$PYEAR" "$PHOUR":"$PMINUTE":"$PSECOND`

##
## Output converted time
##

echo $CONVTIME
 
Nice - can I include this in the FAQ on the subject? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks. Feel free to add to the FAQ.

FYI: I have since found (elsewhere on this site), a better way to find the last day of the previous month.

Instead of:

##
## Find last day of previous month.
##

CALDAY=`cal $PMONTH $PYEAR`
for MONTHDAY in $CALDAY; do
LASTDAY=$MONTHDAY
done

PDAY=$LASTDAY

...it can be done in one line:

PDAY=`cal $PMONTH $PYEAR | xargs | awk '{print $NF}'`

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top