Ask Your Question
3

How do I determine the current timezone in the format America/Los_Angeles (for example) [closed]

asked 2012-08-31 13:44:42 -0600

sgordon gravatar image

I am writing a bash script which generates a kickstart file, which is in turn used to start a virtual machine hosted on the local system. One of the options specified in the kickstart is the timezone for the guest to use in the format:

timezone America/Toronto

How do I determine what the current timezone of the machine on which the script is run is, in the above format. I have already looked at:

  • date, which has options to return the timezone but only in numeric formats.
  • tzselect, which returns the correct format but requires user interaction to 'tell' it what the desired timezone is.
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Akshay
close date 2013-10-27 10:55:25.384406

4 Answers

Sort by ยป oldest newest most voted
2

answered 2012-08-31 21:52:19 -0600

jmt gravatar image

Take a look at /etc/sysconfig/clock

    # The time zone of the system is defined by the contents of /etc/localtime.
    # This file is only for evaluation by system-config-date, do not rely on its
    # contents elsewhere.
    ZONE="Europe/Helsinki"
edit flag offensive delete link more

Comments

/etc/sysconfig/clock doesn't exist on my fresh Fedora 19 install.

Stewart Smith gravatar imageStewart Smith ( 2013-07-04 07:06:05 -0600 )edit
0

answered 2012-09-02 03:02:09 -0600

FranciscoD_ gravatar image

updated 2013-07-04 09:38:48 -0600

Have you tried

date +%Z

On my machine, it gives this:

[ankur@ankur ~]$ date +%Z
EST

which looks like what you need?

BTW, this answer was given by google using the keywords "Linux find timezone" ;)

EDIT:

Enter timedatectl

[asinha@localhost  ~]$ timedatectl
      Local time: Fri 2013-07-05 00:36:54 EST
  Universal time: Thu 2013-07-04 14:36:54 UTC
        Timezone: Australia/Sydney (EST, +1000)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: no
 Last DST change: DST ended at
                  Sun 2013-04-07 02:59:59 EST
                  Sun 2013-04-07 02:00:00 EST
 Next DST change: DST begins (the clock jumps one hour forward) at
                  Sun 2013-10-06 01:59:59 EST
                  Sun 2013-10-06 03:00:00 EST
[asinha@localhost  ~]$

Does that help? It even shows the next DST change etcetra.

edit flag offensive delete link more

Comments

timedatectl status | grep "zone" | sed -e 's/^[ ]Time zone: (.) (.*)$/\1/g'`

rocketraman gravatar imagerocketraman ( 2016-09-13 16:20:23 -0600 )edit
0

answered 2013-07-04 07:04:36 -0600

Stewart Smith gravatar image

updated 2013-07-04 07:05:14 -0600

This is the snippet of Perl code I've used that should work both on Debian/Ubuntu and Fedora. Well... it works on my Fedora this evening and it worked on my Ubuntu this morning :)

my $SYSTZ="/etc/timezone";
my $local_timezone;
if (-f $SYSTZ)
{
  open(LOCALZONE,"$SYSTZ"); $local_timezone = <LOCALZONE>; chomp
  $local_timezone; close(LOCALZONE);
}
elsif (-f '/etc/localtime')
{
  $local_timezone = readlink '/etc/localtime';
  $local_timezone =~ s/.*zoneinfo\///;
}
else
{
  die "Can't find current local time zone :("
}
edit flag offensive delete link more
-1

answered 2013-10-24 20:17:43 -0600

lsatenstein gravatar image

All of this is most interesting, but does not work. I have a requirement to take the mtime from the stat() function call, and determine the date and time (to the nearest minute) Since I am in EST, with daylight savings in effect, my time is actually -4*3600 seconds too fast from UTC. MY Unix system has the TZ variable that appears to be formatted as: "EST5EDT,M3,2,0,M11.1.2"

EST is our zone, 5 is the hours behind UTC, and EDT says we have daylight saving time as being in effect from month 3, day 2 midnight, and it terminates on month 11, day 1, at 2 am. (DST=March2,midnight -- November11,2am.

What automated Fedora function does this? I am going to ask this as a question, since TZ should be set at boot time. Note: for the beginning string there are commas, and for the closing dates, there are dots.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-08-31 13:44:42 -0600

Seen: 2,325 times

Last updated: Oct 24 '13