168

Is there a way to check which time zone I'm currently in on Linux?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90

16 Answers16

287

If you mean from the console, just type:

date +%Z
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Rasman
  • 2,971
  • 2
  • 12
  • 5
  • And to set the timezone from the command line in Ubuntu, the following link provides the trivial, 1-line command to do so: http://askubuntu.com/a/524362/182454 – Dan Nissenbaum Dec 15 '16 at 06:30
  • 2
    This gives `+07` for me. To get `Region/City` on a Debian / Arch based Linux, see [this answer](https://superuser.com/a/1334239/365890). – Tom Hale Jun 26 '18 at 06:55
  • 1
    Use `date +%z` for `+hhmm` numeric time zone (e.g., `-0400`). – meridius Jun 17 '22 at 14:02
74

If you want the numeric timezone:

date +'%:z %Z'

Sample output:

-05:00 EST
Paul Vargas
  • 841
  • 6
  • 6
  • 9
    On OS X this outputs `:z EST` – suspectus Feb 13 '15 at 17:01
  • 8
    This if for [tag:linux]. – Paul Vargas Feb 13 '15 at 17:18
  • 1
    More precisely, this applies to GNU `date`. The BusyBox version prints something non-sensical, "%:z CEST" in my case. – Léo Lam Jun 03 '15 at 18:42
  • However, date +%z works fine on OS X. I suggest you modify the answer. – Neil Mayhew Aug 14 '15 at 17:22
  • 1
    Hi, @NeilMayhew Thanks! Humm... but the question has the tags `linux` `ubuntu-10.04 ` – Paul Vargas Aug 14 '15 at 17:29
  • True, but %z works just as well for this purpose. It prints -0500 instead of -05:00. I think it's best to avoid Linux-only (or, in this case, GNU-only) things just for the sake of finesse. – Neil Mayhew Aug 14 '15 at 17:49
  • @NeilMayhew The question is titled *"How to check which timezone in **Linux**?"*, why would anyone (who reads it or comes into this seeking for help) care for an OSX or an OS-agnostic answer? – ypercubeᵀᴹ Jun 02 '16 at 20:51
  • @ypercubeᵀᴹ it's the first result for "get timezone unix" – Rahat Ahmed Sep 13 '16 at 18:17
  • I realize this thread is a linux question/answer, but it is clear that many Google searches by OSX users are coming to this thread, so here is an OSX 10.6.8 command line example (without the colon): `date +'%z %Z' ` – lawlist Mar 14 '17 at 02:40
  • @lawlist That's great! I will add it to my answer. – Paul Vargas Mar 14 '17 at 16:36
  • I am writing a software where I don't trust users. I wanted to get the system timezone. But this will let me change my timezone via the `TZ` environment variable. For example `TZ=Arctic/Longyearbyen date +%:z` shows `+01:00` but not `+05:30` where I am currently in... – 15 Volts Dec 16 '20 at 14:25
29

I wanted to find the timezone in "US/Eastern" or "Europe/London" form instead. You can find this in:

  • /etc/timezone (present on Ubuntu and Red Hat? but not e.g. Amazon Linux)
  • (on Red Hat style systems) as ZONE="US/Eastern" in /etc/sysconfig/clock
  • or you can try and match /etc/localtime to one of the files under /usr/share/zoneinfo; annoyingly this doesn't seem to be a symlink, but you can e.g.

    cd /usr/share/zoneinfo
    find * -type f -exec sh -c "diff -q /etc/localtime '{}' > /dev/null && echo {}" \;

    to find matching files - there's probably better ways to do that, but that works. There will be multiple matches.

Rup
  • 992
  • 1
  • 12
  • 17
  • 8
    `find /usr/share/zoneinfo/ -type f| xargs md5sum | grep $(md5sum /etc/localtime | cut -d' ' -f1)` – freiheit Jul 04 '16 at 18:25
25

Usually, the TZ environment variable will tell you something useful. However, it is best to use functions such as mktime() and localtime() to convert between time_t and a local timezone representation. That is, don't try to do the conversion yourself.

Greg Hewgill
  • 5,499
  • 2
  • 29
  • 30
  • 3
    is there any Command that tells about timezone the user is in? –  Jul 11 '11 at 05:00
  • 30
    Sure, `date` will tell you. – Greg Hewgill Jul 11 '11 at 05:01
  • i have two instance of server , if i give "date" command in both server it shows different time. time zone for both server is IST, time of one server: Mon Jul 11 10:39:31 IST 2011 and other server Mon Jul 11 10:35:31 IST 2011 .. there is a difference of 4 minutes –  Jul 11 '11 at 05:06
  • Well that's an entirely different problem than "What timezone am I in?". I suggest you install NTP on your servers. – Greg Hewgill Jul 11 '11 at 05:13
  • 12
    On Precise Pangolin `echo $TZ` returns nothing for me. – Iain Samuel McLean Elder Oct 05 '13 at 19:48
  • @IainElder: That just means that processes without a specific `TZ` set will use the system default (whatever that is). Setting `TZ` can be used to override the system default on a per-process basis. – Greg Hewgill Oct 05 '13 at 20:25
  • 3
    @GregHewgill your answer is not helpful at first view. Consider editing your answer please. See the most voted answer below. I will downvote your answer for now. Also there is no TZ env on default on my machines. – therealmarv Jun 21 '16 at 15:52
  • unfortunately "date" at least in it's default output only displays an ambiguious three letter abreviation, not the actual timezone selected. – plugwash Nov 11 '19 at 18:30
16

For ubuntu try this :

$ cat /etc/timezone

Sample output :

Asia/Kolkata

For other distro Reference : https://unix.stackexchange.com/questions/110522/timezone-setting-in-linux

Shankar
  • 179
  • 1
  • 2
  • Sometimes [`timedatectl set-timezone` doesn't update `/etc/timezone`](https://unix.stackexchange.com/q/451709/143394). See [this answer](https://superuser.com/a/1334239/365890) for a solution based on `/etc/localtime`. – Tom Hale Jun 26 '18 at 06:56
15

Sometimes you may be looking for the canonical timezone rather than the short form as produced by date %Z e.g. US/Eastern. On systems with timedatectl e.g. Fedora, timedatectl outputs lots of useful information, including the current zone:

# timedatectl
      Local time: Tue 2016-09-13 17:10:26 EDT
  Universal time: Tue 2016-09-13 21:10:26 UTC
        RTC time: Tue 2016-09-13 21:10:26
       Time zone: US/Eastern (EDT, -0400)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

Unfortunately, timedatectl takes set-timezone as a command, but has no corresponding get-timezone. Parse it as follows:

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

For systemd >= 241, see @BurninateSE's answer (TLDR: timedatectl show --va -p Timezone).

Raman
  • 313
  • 2
  • 8
8

For the time zone, you can use geolocation:

$ curl https://ipapi.co/timezone
America/Chicago

Or:

$ curl http://ip-api.com/line?fields=timezone
America/Chicago

http://wiki.archlinux.org/index.php/time#Time_zone

Zombo
  • 1
  • 24
  • 120
  • 163
  • 3
    Just want to point out that although this will _likely_ return the correct timezone the server is located at it does not necessarily return the timezone the server is configured for. – Art Geigel Sep 03 '20 at 12:11
5

All of the following solutions will give you an IANA compatible Olson timezone ID (e.g. America/Los_Angeles). However, some may return extra whitespace at the beginning and/or end - trim if required.

Using the environment

The environment variable TZ (if set) specifies the timezone. Checking this first is essential.

$ echo $TZ

I can personally confirm that this works on ALL of my modern Linux hosts.

Using Systemd version >= 241

This is the preferred method for all those who believe Systemd shall rule the world.

$ timedatectl show --va -p Timezone

If you get an error similar to timedatectl: invalid option -- 'p' that means your Systemd isn't ready to rule the world. Instead...

Using Systemd and sed

Improvement upon Raman's answer, but without the useless use of grep. This solution is optimal on systems utilizing older versions of Systemd.

$ timedatectl | sed -n 's/^\s*Time zone: \(.*\) (.*/\1/p'

Using /etc/timezone

This is a simple readable file, but doesn't seem to be available on as many distros anymore. IIRC, this is something those Debian devs came up with a while ago that fell out of favour.

$ cat /etc/timezone

Using GNU realpath because /etc/localtime is a symlink

This solution is optimal if you know that /etc/localtime is a symlink, which is something that both a lot of older software and guides accomplish and even modern SystemD promises; and you also have GNU realpath available, which should also be available on every GNU OS distro.

$ realpath --relative-to /usr/share/zoneinfo /etc/localtime

D'oh! /etc/localtime isn't a symlink; using find, sed, md5sum

If you find yourself with a non-symlink /etc/localtime (the command shown above will either error or provide a path that starts with ../), you can try to locate its md5 match in your local timezone database. This may return multiple matches as there are quite a few aliases in the Olson database.

$ find /usr/share/zoneinfo -type f -exec md5sum {} + |\
  sed /$(printf $(md5sum /etc/localtime))/'!d;s/.\{54\}//;/^posix/d'

If you'd only like a single timezone ID returned (whichever one is returned first by the system's readdir()), you can use sed's q command to quit after the first match. This is much more performant than starting up head for instance. However, this causes find to break the pipe with md5sum early, causing an error to print to fd 2. You may ignore this error by piping it to /dev/null as the following complete command demonstrates:

$ find /usr/share/zoneinfo -type f -exec md5sum {} + 2> /dev/null |\
  sed /$(printf $(md5sum /etc/localtime))/'!d;s/.\{54\}//;/^posix/d;q'

Using curl and geolocation

If for some reason you don't want or don't trust the host's configuration (or just hate their guts) but you're certain that it can connect to the wider world web, there are several gratis online services that will geolocate you (whilst they last). Below are some examples:

$ curl -Lsf https://ipapi.co/timezone # Defunct?

$ curl -Lsf http://ip-api.com/line?fields=timezone

$ curl -Lsf https://ipwhois.app/line/?objects=timezone

$ curl -Lsf http://api.ipgeolocation.io/timezone -e ';auto' | jq -r .timezone

First two services shamelessly stolen from Steven Penny's answer. Additionally I implore the SE community to please keep this list up to date with working links thanks!

CW: politics:

Finally, I beg of you to please don't use this method to bind your users and steal their agency - only use this method if they ask you to do so!

Also note that while these services are gratis, please consider visiting their respective web sites to learn about and accept their terms and conditions before using.

BurninateSE
  • 51
  • 1
  • 3
4
$ strings /etc/localtime | tail -n 1
MST7MDT,M3.2.0,M11.1.0

So I'm on Mountain Time. Although advices above on using environment variable or just date command output sometimes work better, depending how you want to use that.

Tagar
  • 230
  • 2
  • 8
3

I have linux mint Cinnamon. timedatectl gave the information I needed:

                  Local time: Mon 2020-06-29 11:04:27 PDT
              Universal time: Mon 2020-06-29 18:04:27 UTC
                    RTC time: Mon 2020-06-29 18:04:27
                   Time zone: America/Vancouver (PDT, -0700)
   System clock synchronized: yes

systemd-timesyncd.service active: yes RTC in local TZ: no

2

Using TZ or date IS NOT RELIABLE because it tells you the USER's timezone, not the default system timezone.

The default system timezone is stored in /etc/timezone (which is often a symbolic link to the timezone data file specific to the timezone). If you do not have an /etc/timezone, look at /etc/localtime. Generally that is the "server's" timezone. /etc/localtime is often a symlink to a timezone file in /usr/share/zoneinfo. That path to the correct timezone file will often give you geography information as well.

Newer linux have "timedatectl" which gives you tons of info when the command is run.

(as a side-node, if you have an ancient system that still uses the OLD hard-coded timezones, you can probably copy a modern timezone file onto it and it will work. I have had to do this many times to resolve changing timezones on older equipment).

TekOps
  • 71
  • 2
2

Systemd added more options, so I don't have to grep, sed or awk a lot:

$ timedatectl show -p Timezone --value
Europe/Berlin

This works on Debian Buster (systemd 241) but not on Ubuntu 18.04 Bionic (systemd 237). So I use this in scripts now to keep it simple and clean:

$ timedatectl show -p Timezone --value || cat "/etc/timezone"
Europe/Berlin
LiveWireBT
  • 904
  • 2
  • 7
  • 34
2
  • /etc/sysconfig/clock sets whether the hardware clock is stored as UTC or local time.
  • Symlink /etc/localtime to /usr/share/zoneinfo/... to set your timezone.
  • Type /sbin/hwclock --systohc [--utc] to set the hardware clock.

The Linux kernel always stores and calculates time as the number of seconds since midnight of the 1st of January 1970 UTC regardless of whether your hardware clock is stored as UTC or not. Conversions to your local time are done at run-time. One neat thing about this is that if someone is using your computer from a different timezone, they can set the TZ environment variable and all dates and times will appear correct for their timezone.

If the number of seconds since the 1st of January 1970 UTC is stored as an signed 32-bit integer (as it is on your Linux/Intel system), your clock will stop working sometime on the year 2038. Linux has no inherent Y2K problem, but it does have a year 2038 problem. Hopefully we'll all be running Linux on 64-bit systems by then. 64-bit integers will keep our clocks running quite well until aproximately the year 292271-million.

Rup
  • 992
  • 1
  • 12
  • 17
1

A couple of solutions:

date +"%Z %z"
timedatectl | grep "Time zone"
cat /etc/timezone

gave me (respectively):

UTC +0000
Time zone: Etc/UTC (UTC, +0000)
Etc/UTC

My computer is on UTC.

tika
  • 121
  • 3
1

Sometimes timedatectl set-timezone doesn't update /etc/timezone, so it's best to get the tiemzone from the name of the file that the symlink /etc/timezone points to:

#!/bin/bash
set -euo pipefail

if filename=$(readlink /etc/localtime); then
    # /etc/localtime is a symlink as expected
    timezone=${filename#*zoneinfo/}
    if [[ $timezone = "$filename" || ! $timezone =~ ^[^/]+/[^/]+$ ]]; then
        # not pointing to expected location or not Region/City
        >&2 echo "$filename points to an unexpected location"
        exit 1
    fi
    echo "$timezone"
else  # compare files by contents
    # https://stackoverflow.com/questions/12521114/getting-the-canonical-time-zone-name-in-shell-script#comment88637393_12523283
    find /usr/share/zoneinfo -type f ! -regex ".*/Etc/.*" -exec \
        cmp -s {} /etc/localtime \; -print | sed -e 's@.*/zoneinfo/@@' | head -n1
fi

References: in this answer.

Tom Hale
  • 2,274
  • 2
  • 24
  • 35
1

You can show date and the timezone concurrently:

date +'%d/%m/%Y  %H:%M:%S [%:z  %Z]'
Mohsen Abasi
  • 173
  • 2