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