Operating System - HP-UX
1833053 Members
2667 Online
110049 Solutions
New Discussion

Re: Display the current time in different timezones

 
SOLVED
Go to solution
Neil Edwards
Advisor

Display the current time in different timezones

Hello Experts,

I need to display the current time in several timezones. Any ideas? I've tried to use the date command but sometimes the seconds don't match.

TIA, Neil
It wasn't me.
11 REPLIES 11
John Poff
Honored Contributor

Re: Display the current time in different timezones

Hi,

You can set the TZ variable before the 'date' command and it will show the time in that time zone. Something like this:

# date
Tue Nov 19 17:28:18 EST 2002
# echo $TZ
EST5EDT
# TZ=CST6EDT date
Tue Nov 19 16:28:32 CST 2002


JP
John Poff
Honored Contributor

Re: Display the current time in different timezones

Actually, I meant to do:

TZ=CST6CDT date

but it worked all the same.

JP
Jeff Schussele
Honored Contributor

Re: Display the current time in different timezones

Hi Neil,

Time display is handled by the TZ environment variable.

For instance
export TZ=CST6CDT

will display the time for the US Central time zone.

Just adjust the TZ value for whatever zone you wish.
You don't need to change the actual system time.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
James R. Ferguson
Acclaimed Contributor

Re: Display the current time in different timezones

Hi:

Simply set the appropriate timezone for the duration of the command only; for examle:

TZ=PST8PDT date

Note that my normal TZ is EST5EDT so the above tells me the Pacific coast time.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Display the current time in different timezones

This is pretty easy. I'll bet your first attempt was something like this:

TZ=EST5CDT date
TZ=CST6CDT date

as you indicated, if your dates commands don't execute during the same epoch second, you're bad.

Here's the way to do it:

NOW=$(perl -e 'print time()')
TIMEZONES="EST5EDT CST6CDT MST7MDT PST8PDT"
for T in ${TIMEZONES}
do
TZ=${T} perl -e "print scalar localtime(${NOW})"
# NOTE THE DOUBLEQUOTES; WE WANT THE SHELL TO EXPAND ${NOW}
echo
done

That should do it, Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Display the current time in different timezones

Hi (again):

This too will work:

# date;TZ=PST8PDT date;TZ=MST7MDT date;TZ=CST6CDT date

...return(ed):

Tue Nov 19 17:42:03 EST 2002
Tue Nov 19 14:42:03 PST 2002
Tue Nov 19 15:42:03 MST 2002
Tue Nov 19 16:42:03 CST 2002

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Display the current time in different timezones

Hi again Neil,

I took your question to mean that you wanted to display the same time (to the second) in multiple timezones. If that is correct then you must somehow use a command that captures that time and then displays that same epoch seconds values in all the timezones. If my interpretation is correct, then the multiple date commands will worl perfectly almost all the time BUT if you truly want to display consistant times then you need the Perl solution or something equivalent.
If it ain't broke, I can fix that.
Neil Edwards
Advisor

Re: Display the current time in different timezones

Sorry guys,

I guess I wasn't very clear. I had already tried the TZ=MST7MDT
export TZ
date
approach but very rarely after doing all nine timezones, the first time displayed would not match the last time by one second. Clay seemed to understand my problem and I think I'll use his method after I understand what it does.

Thanks for all your quick help.

Neil
It wasn't me.
James R. Ferguson
Acclaimed Contributor

Re: Display the current time in different timezones

Hi Clay:

I absolutely agree.

Warmest regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Display the current time in different timezones

Hi Jim:

I suppose the last part of Neil's original post jumped out at me. Of course, the problem with my method is that the time might be stale by one second ("The moving finger ..." - some of that Omar Khayam stuff) but at least the times will all zig or they will all zag.

Neil,

The only tricky part is the call to time to capture the epoch seconds. I intentionally did this as part Perl and part shell for those less familiar with Perl. If I were doing for me, it would be pure Perl.

Regards, Clay

If it ain't broke, I can fix that.
John Poff
Honored Contributor

Re: Display the current time in different timezones

Hi,

Clay has the best solution [as usual :) ]. I got excited and typed in an answer and then read the whole question. Another spot where Perl really shines, since there is no easy way in the shell to grab a particular time and use it for several different 'date' commands.

I love these little types of scripts. Anything that will turn a $$$,$$$ dollar computer into a simple $30 clock. ;)

JP