Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

Thanks

share|improve this question
add comment

5 Answers 5

up vote 2 down vote accepted

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.

share|improve this answer
    
This is perfect, thanks. –  Willi Ballenthin Jul 16 '10 at 19:20
add comment

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
share|improve this answer
    
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
add comment

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 :>

share|improve this answer
add comment

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

file /etc/timezone

or

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
 then
   check if file is ascii or gz
    if ascii then cat the timezone
    else
    if exists /etc/redhat-release
    then
     assume redhat
     TZ = cat /etc/sysconfig/clock
   fi

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.

share|improve this answer
    
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
add comment

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

date +%Z
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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