I'd like to get the timezone of a system. Now some caveats...

  • Im using Linux, and the target systems will be UNIX.
  • I'll be using only system images, ie. not the live system, but the partitions will be mounted.
  • I'll eventually be scripting it.

I was thinking about using /etc/localtime, but sometimes this is a real file, and sometimes a symlink. In either case, I can't seem to get a description of the file format from which to parse.

Anyone have any ideas?



That file is going to be in the tz file format which is a binary format. You can use DateTime::TimeZone::Tzfile to parse it if you know Perl. If it is a symlink it should not mater because if it is mounted you can just follow it.

Lastly, if you can control the image creation I highly recommend you just set the system time to UTC.


Do you know what flavor of UNIX these are?

In addition to /etc/localtime,

  • Redhat variants use /etc/sysconfig/clock
  • Debian variants use /etc/timezone
  • I'd like to stay as general as possible, so if I'm looking in the wrong place, I'd welcome comments. Thanks – Willi Ballenthin Jul 16 '10 at 19:21

The cruel way .... :p If mount is readonly, touch a file and afterwards ... ls -lah ....

then you 'll have the time that the file was created. Meaning ... system's time +/- some seconds.

Or somehow, try to modify something explicitly, or implicitly and see the time :>


A simple way to handle this on Linux systems is to use the file command;

file /etc/timezone


file /etc/sysconfig/clock

However as you may be using images of UNIX systems (I am hoping its Solaris as thats as much as I am familiar with) it may be slightly different.

Without actually writing the script for you, here is my take on it in pseudo code

get mount
if mount is ext3 ... then assume linux
 if exists /etc/timezone
   check if file is ascii or gz
    if ascii then cat the timezone
    if exists /etc/redhat-release
     assume redhat
     TZ = cat /etc/sysconfig/clock

Yes its less than elegant but you shoudl get my drift. So you can make some assumputions on the os based upon the mount.

The process would be same for say a Solaris mount, you could work out where the file is, use the file command to determine the filetype and from there either just get its contents with cat or grep ^TZ

Hope that helps.

  • Forgot to mention, depending on your script fu, you could run something like this either as the mounting systems root/admin user or indeed within the chroot. – AJ01 Jul 16 '10 at 21:00

From the command line you can get it in a safer way than reading /etc/localtime by running:

date +%Z
  • The systems won't be live, so date commands won't work – Zypher Jul 16 '10 at 18:38
  • Sorry if I didn't specifically said to chroot before: chroot /path/to/system date +%Z – bclermont Jul 16 '10 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.