2

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

Willi Ballenthin
  • 345
  • 1
  • 2
  • 11

5 Answers5

2

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.

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
0

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
Matt
  • 933
  • 5
  • 12
0

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

Nikolaidis Fotis
  • 1,994
  • 11
  • 13
0

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.

AJ01
  • 131
  • 9
  • 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
-1

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

date +%Z
Treddit
  • 359
  • 2
  • 3