135
27
Is there a way to check which time zone I'm currently in on Linux?
135
27
Is there a way to check which time zone I'm currently in on Linux?
21
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.
252
If you mean from the console, just type:
date +%Z
good point... changed the answer to that effect – Rasman – 2014-06-19T15:44:46.047
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 – 2016-12-15T06:30:58.630This gives +07
for me. To get Region/City
on a Debian / Arch based Linux, see this answer.
2good, works well. – Silver Moon – 2013-11-18T07:18:18.157
15this should be the accepted answer – KalenGi – 2014-03-24T10:04:17.477
58
If you want the numeric timezone:
date +'%:z %Z'
Sample output:
-05:00 EST
7On OS X this outputs :z EST
– suspectus – 2015-02-13T17:01:52.167
7This if for [tag:linux]. – Paul Vargas – 2015-02-13T17:18:40.247
1More precisely, this applies to GNU date
. The BusyBox version prints something non-sensical, "%:z CEST" in my case. – Léo Lam – 2015-06-03T18:42:55.673
However, date +%z works fine on OS X. I suggest you modify the answer. – Neil Mayhew – 2015-08-14T17:22:59.080
1Hi, @NeilMayhew Thanks! Humm... but the question has the tags linux
ubuntu-10.04
– Paul Vargas – 2015-08-14T17:29:47.307
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 – 2015-08-14T17:49:55.313
@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ᵀᴹ – 2016-06-02T20:51:56.173
@ypercubeᵀᴹ it's the first result for "get timezone unix" – Rahat Ahmed – 2016-09-13T18:17:32.150
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 – 2017-03-14T02:40:52.763
@lawlist That's great! I will add it to my answer. – Paul Vargas – 2017-03-14T16:36:22.330
26
I wanted to find the timezone in "US/Eastern" or "Europe/London" form instead. You can find this in:
ZONE="US/Eastern"
in /etc/sysconfig/clockor 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.
4find /usr/share/zoneinfo/ -type f| xargs md5sum | grep $(md5sum /etc/localtime | cut -d' ' -f1)
– freiheit – 2016-07-04T18:25:32.930
13
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
Sometimes timedatectl set-timezone
doesn't update /etc/timezone
. See this answer for a solution based on /etc/localtime
.
7
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
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.
3
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
2
/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.
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.
0
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).
0
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.
0
You can show date and the timezone concurrently:
date +'%d/%m/%Y %H:%M:%S [%:z %Z]'
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 – 2016-06-21T15:52:28.610
unfortunately "date" at least in it's default output only displays an ambiguious three letter abreviation, not the actual timezone selected. – plugwash – 2019-11-11T18:30:37.820
2is there any Command that tells about timezone the user is in? – None – 2011-07-11T05:00:56.190
26Sure,
date
will tell you. – Greg Hewgill – 2011-07-11T05:01:34.800i 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 – None – 2011-07-11T05:06:02.530
Well that's an entirely different problem than "What timezone am I in?". I suggest you install NTP on your servers. – Greg Hewgill – 2011-07-11T05:13:26.020
8On Precise Pangolin
echo $TZ
returns nothing for me. – Iain Samuel McLean Elder – 2013-10-05T19:48:28.567@IainElder: That just means that processes without a specific
TZ
set will use the system default (whatever that is). SettingTZ
can be used to override the system default on a per-process basis. – Greg Hewgill – 2013-10-05T20:25:05.530