Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-04-2012, 08:47 PM   #1
Registered: Sep 2003
Posts: 93

Rep: Reputation: 0
Display different timezones in command line??

Hey Guys

Using date command.. How can I dispaly time of different timezones in command line

I am doing this on mac.. but I think the date command is universal for unix right?

Any other command will also do

I basically want to display different timezone time on command line

Is this correct?? Just as an example? Will it mess with my system time??

TZ=IST/UTC-5:30 date +"India: %I:%M %p %a"

Last edited by khandu; 02-04-2012 at 08:52 PM.
Old 02-04-2012, 10:43 PM   #2
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Originally Posted by khandu View Post
I think the date command is universal for unix right?
They don't all have the same features, but a basic date command should be available in all Unix and POSIX systems, including Linux, BSDs, and Mac OS X, yes.

Originally Posted by khandu View Post
I basically want to display different timezone time on command line. Is this correct?? Just as an example? Will it mess with my system time??
No, it will not mess with your system time. Use
env TZ=IST-5:30 date +'India: %I:%M %p %a'
to make sure you actually set the TZ environment variable for the date command (and the one command only), though. (I do not think the IST/UTC-5:30 format works, use TZ=IST-5:30 or TZ=UTC-05:30 or TZ=:Asia/Calcutta instead.)

If you want to make sure the month and date names are in a specific language, you can use
env TZ=IST-5:30 LANG=en_US LC_ALL=en_US date +'India: %I:%M %p %a'
There is a specific locale called C or POSIX locale, LANG=C LC_ALL=C, which is available on all systems, and pretty much matches the en_US locale.

POSIX systems, especially Linux and BSD (and Mac, as it is a BSD derivative I believe), are designed to work with multiple simultaneous users. The system time is in UTC, and is only adjusted when displayed to the user. The /etc/timezone file is usually a symlink to the default timezone on the machine, and all the utilities read it to determine how the local time is derived from UTC. The TZ environment variable can be set, independently for each process, to override that. Remember: it does not change any system values at all, it just controls how the system values are interpreted and displayed by these utilities.

There is no danger of messing up your system time, when fooling around with these time utilities as a normal user. It's not like these systems are as braindead as Windows is. Even if you happen to set your TZ variable to a funny value, all you do is see the wrong local time in the utilities that see the wrong TZ value. Reset it -- it is empty by default on most systems -- and its fixed. Or close that shell, and start a new one; that'll also work.

Last edited by Nominal Animal; 02-04-2012 at 10:48 PM.
1 members found this post helpful.
Old 02-05-2012, 03:27 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
You don't even need the env command here. If you directly precede a command with a variable setting, then bash and probably most other shells will execute the command with that variable in its environment. The environment of the main shell will not be affected unless you insert a command separator of some kind.

You can also run a set of commands in a subshell if you need to do stuff without altering the main environment.

$ echo $TZ
				#variable unset

$ date
Sun Feb  5 18:15:16 JST 2012

$ TZ=UTC date
Sun Feb  5 09:15:51 UTC 2012

$ echo $TZ
				#still unset

$ TZ=UTC ; date			#run the commands with a ";" command separator
Sun Feb  5 18:16:39 JST 2012
$ echo $TZ
UTC				#now TZ is set (in the current shell only)
				#but it doesn't affect subprocesses
				# because it hasn't been exported.

$ export TZ
$ date
Sun Feb  5 09:18:20 UTC 2012	#now it affects subprocesses

$ ( TZ=JST ; date )		#run the commands in a subshell
Sun Feb  5 09:19:59 JST 2012

$ echo $TZ
UTC				#no effect in the main shell

$ unset TZ


date, mac, time, timezone

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
A Command to display a specific line?? pheardotcom Linux - Newbie 7 11-02-2009 11:15 PM
Turn Display Off in Command-Line frzburn Linux - Laptop and Netbook 5 01-24-2007 03:14 PM
linux command : read/display the line n xeebeeeeeee Programming 3 03-26-2006 05:08 AM
Shell command line won't display Jbernoski Slackware 2 03-15-2006 06:31 PM
Command to display whole filestructure hierarchy f/ command line? mjewell Linux - Newbie 10 01-19-2004 10:48 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration