Time zone conversions

"Time is constant, we come and go", but the beauty of earth is so, that time varies with space.

I made a simple BASH script to convert between different time zones, as i badly needed one such app, that would give me time in my terminal.

The script majors play with the GNU date utility. date - print or set the system date and time and -d or --date option take a date string to display the current date based on the string.

The script:

#!/usr/bin/bash
# Author  : hemanth [email protected]
# License : GNU GPLv3
# Site : www.h3manth.com
# This script converts given / local time to other time zones

# To echo useage
function useage()
{
    echo "USEAGE : $0 [OPTION] <-z GMT,UTC,EDT,CST,MST,PST,IST> HH:MM"
}

function __print__()
{
date -d "$1" &> /dev/null 
if [[ $? == 0 ]];then
echo "Given time ${1:-00:00} in various time zones:"
echo "IST => $(date -d "$1 IST")"
echo "UTC => $(date -d "$1 UTC")"
echo "EST => $(date -d "$1 EST")"
echo "EDT => $(date -d "$1 EDT")"
echo "PST => $(date -d "$1 PST")"
echo "CST => $(date -d "$1 CST")"
echo "MST => $(date -d "$1 MST")"
else
echo invalid date $1 
fi
}

while [[ $1 == -* ]]; do
    case "$1" in
      -h|--help|-\?) useage; exit 0;;
      -z)[[ $# > 2 ]]&& shift && date -d "$*" &>/dev/null; [[ $? == 0 ]] && 
         date -d "$*" || useage ;shift; exit 0 ;;
      --) shift; break;;
      -*) echo "invalid option: $1" 1>&2; useage; exit 1;;
    esac
done
    
if [[ $1 != -* ]]; then
   __print__ $1
fi

Different scenarios :

$ bash tz.bash 
Given time 00:00 in various time zones:
IST => Mon Sep 27 00:00:00 IST 2010
UTC => Mon Sep 27 05:30:00 IST 2010
EST => Mon Sep 27 10:30:00 IST 2010
EDT => Mon Sep 27 09:30:00 IST 2010
PST => Mon Sep 27 13:30:00 IST 2010
CST => Mon Sep 27 11:30:00 IST 2010
MST => Mon Sep 27 12:30:00 IST 2010
$ bash tz.bash -z 10:00 UTC
Mon Sep 27 15:30:00 IST 2010
$ bash tz.bash -z 10:00 EST
Mon Sep 27 20:30:00 IST 2010
$ bash tz.bash -z 10:00 CST
Mon Sep 27 21:30:00 IST 2010
$ bash tz.bash -h
USEAGE : tz.bash [OPTION] <-z GMT,UTC,EDT,CST,MST,PST,IST> HH:MM

Now trying to make similar code with other different programming languages, to compare and contrast, do share your code in your favorite language

Get the source code from HERE

Share this