Is there a way to check which time zone I'm currently in on Linux?
16 Answers
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.
If you mean from the console, just type:
date +%Z
If you want the numeric timezone:
date +'%:z %Z'
Sample output:
-05:00 EST
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.
For ubuntu try this :
$ cat /etc/timezone
Sample output :
Asia/Kolkata
For other distro Reference :
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 the time zone, you can use geolocation:
$ curl
America/Chicago
Or:
$ curl
America/Chicago
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.