1748122 Members
3481 Online
108758 Solutions
New Discussion юеВ

Get Time

 
SOLVED
Go to solution
Ramkumar Devanathan
Honored Contributor

Get Time

Guys, don't know how much this helps, but chancing upon this page (http://www.tummy.com/journals/entries/jafo_20070214_155842), i just wrote up a script that gets time of a different timezone/city given either city name or timezone name.

Here's the script - feel free to give your feedback on this. Tested only on linux as of now.

>>
[ramd@nt14115 ramd]$ cat time.sh
#!/bin/bash

ZDUMP=/usr/sbin/zdump

if [[ $# = 0 ]]; then
echo "Error: $0 "
exit 1
fi

city=$1

if [[ -n $city ]]; then
tzfiles=$(find /usr/share/zoneinfo | grep -i $city)
if [[ -n ${tzfiles} ]]; then
for tzfile in ${tzfiles}
do
${ZDUMP} ${tzfile}
done
fi
fi

exit 0
[ramd@nt14115 ramd]$
<<

Run as follows -
>>
[ramd@nt14115 ramd]$ ./time.sh
Error: ./time.sh
[ramd@nt14115 ramd]$ ./time.sh Sara
/usr/share/zoneinfo/Europe/Sarajevo Thu May 3 09:25:26 2007 CEST
/usr/share/zoneinfo/posix/Europe/Sarajevo Thu May 3 09:25:26 2007 CEST
/usr/share/zoneinfo/right/Europe/Sarajevo Thu May 3 09:25:04 2007 CEST
[ramd@nt14115 ramd]$
<<

Hope this helps.

- ramd.
HPE Software Rocks!
4 REPLIES 4
Ivan Krastev
Honored Contributor

Re: Get Time

I think that you should post it here - http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=51050


regards,
ivan
Matti_Kurkela
Honored Contributor
Solution

Re: Get Time

Sorry, but it looks like you're reinventing the wheel. Consider this:

$ zdump Europe/Stockholm
Europe/Stockholm Thu May 3 10:22:35 2007 CEST

$ TZ=Europe/Stockholm date
Thu May 3 10:22:47 CEST 2007

So, you don't need to use zdump. With "date", you can use format strings to get the date in exactly the way you like, so it's more flexible to use "date".

If I needed a script like this, the essential functionality would be something like:

timezone="$1"
shift
echo -n "${timezone} "
TZ=${timezone} date "$@"

If a check for timezone's existence is required, that could be done by digging around /usr/share/zoneinfo as in your script.

The TZ environment variable is not "special" or "holy" in any way. If you're in a different timezone from the server you're using or you need to use a different timezone for some other reason, you can override the default with the TZ variable. Just remember that things like crontab always use the system-wide default timezone.

In Linux (and some other unixes), there is a way for the sysadmin to choose a default timezone which is automatically used if TZ is unset; in other unixes, the TZ variable is the primary way of setting the timezone.

If the unix variant you're using has time zones specified as "continent/capital", it usually understands that format in the TZ variable too. Other unixes will understand only "CET-1CEST" style TZ values.

In your link, the writer of the webpage was not interested in the current time, but the past and future times of standard <-> DST time transitions. This requires the use of zdump.

MK
MK
Ramkumar Devanathan
Honored Contributor

Re: Get Time

Guys, thanks for all the inputs.

I didn't really mean to re-invent the wheel, and in the script all I do is to reduce the effort for the user to search for a city name within /usr/share/zoneinfo/... and print current time of that city/tz.

MK, really value your inputs. Thanks.

Ivan, i would not venture to post this yet in the other thread - maybe after i make the changes that MK suggests. But thanks for the idea.

- ramd.
'Learning more everyday.'
HPE Software Rocks!
Ramkumar Devanathan
Honored Contributor

Re: Get Time

Thanks for all the inputs.
HPE Software Rocks!