- How do I find the current system timezone?
- 13 Answers 13
- How to Get Current System Time Zone in Linux
- How to Find Linux System Timezone
- Method 1: Using timedatectl Command
- Method 2: Using date Command
- Method 3: Using cat Command
- How to Check Timezone in Linux
- Related Posts
- 15 thoughts on “How to Check Timezone in Linux”
How do I find the current system timezone?
On Linux, I need to find the currently configured timezone as an Olson location. I want my (C or C++) code to be portable to as many Linux systems as possible. For example. I live in London, so my current Olson location is «Europe/London». I’m not interested in timezone IDs like «BST», «EST» or whatever. Debian and Ubuntu have a file /etc/timezone that contains this information, but I don’t think I can rely on that file always being there, can I? Gnome has a function oobs_time_config_get_timezone() which also returns the right string, but I want my code to work on systems without Gnome. So, what’s the best general way to get the currently configured timezone as an Olson location, on Linux?
You’re right that /etc/timezone doesn’t always exist — I’ve got access to a CentOS box which doesn’t have it.
On (at least one version of) CentOS you can get the required information using readlink() on /etc/localtime , which is a symlink to (for example) /usr/share/zoneinfo/Europe/London . Again, far from ideal!
@psmears — that’s actually a workable idea, you should make it a proper answer. I suspect that I’ll have to try various ways in order, and use whichever one works.
13 Answers 13
It’s hard to get a reliable answer. Relying on things like /etc/timezone may be the best bet.
(The variable tzname and the tm_zone member of struct tm , as suggested in other answers, typically contains an abbreviation such as GMT / BST etc, rather than the Olson time string as requested in the question).
- On Debian-based systems (including Ubuntu), /etc/timezone is a file containing the right answer.
- On some Redhat-based systems (including at least some versions of CentOS, RHEL, Fedora), you can get the required information using readlink() on /etc/localtime , which is a symlink to (for example) /usr/share/zoneinfo/Europe/London .
- OpenBSD seems to use the same scheme as RedHat.
However, there are some issues with the above approaches. The /usr/share/zoneinfo directory also contains files such as GMT and GB , so it’s possible the user may configure the symlink to point there.
Also there’s nothing to stop the user copying the right timezone file there instead of creating a symlink.
One possibility to get round this (which seems to work on Debian, RedHat and OpenBSD) is to compare the contents of the /etc/localtime file to the files under /usr/share/zoneinfo, and see which ones match:
eta:~% md5sum /etc/localtime 410c65079e6d14f4eedf50c19bd073f8 /etc/localtime eta:~% find /usr/share/zoneinfo -type f | xargs md5sum | grep 410c65079e6d14f4eedf50c19bd073f8 410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/London 410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Belfast 410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Guernsey 410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Jersey 410c65079e6d14f4eedf50c19bd073f8 /usr/share/zoneinfo/Europe/Isle_of_Man . .
Of course the disadvantage is that this will tell you all timezones that are identical to the current one. (That means identical in the full sense — not just «currently at the same time», but also «always change their clocks on the same day as far as the system knows».)
Your best bet may be to combine the above methods: use /etc/timezone if it exists; otherwise try parsing /etc/localtime as a symlink; if that fails, search for matching timezone definition files; if that fails — give up and go home 😉
(And I have no idea whether any of the above applies on AIX. )
How to Get Current System Time Zone in Linux
For social, commercial, and legal purposes, areas on the world map that share a uniform standard time complete the definition of a time zone. Therefore, the implementation of a time zone depends on the divisions and boundaries that separate different countries and regions.
This approach of identifying time zones is more convenient than strictly relying on longitudes. Enough with the geography lesson, let us get back to solving computing problems the Linux way.
Linux is smart enough to identify the time zone you are under; whether you are a Linux developer on the move or a Linux enthusiast that embraces a change in their environment from time to time.
Under the Linux operating system environment, you will never fail to come across time management utilities like date and timedatectl. It is through such time management utilities that we can be able to run various Linux command tweaks to reveal the exact current system time zone associated with our environment.
How to Find Linux System Timezone
To successfully retrieve the current system time zone of our Linux system, we will use several inbuilt Linux command implementations:
Method 1: Using timedatectl Command
According to its man page, the timedatectl command is primarily used to manipulate the Linux system’s time and date values. Executing the command should additionally retrieve time and date-related values like Local time, Universal time, RTC time, System clock synchronized status, NTP service state, and RTC in local TZ value entry.
As per the above time zone, we are currently in Africa/Nairobi (EAT, +0300).
As you might have noticed from the execution of the timedatectl command as demonstrated above, we are forced to filter through unwanted output values to single out the Time Zone value we are after.
What if we wanted to only output the Time Zone value from the timedatectl command? It is at this point that we borrow the efficiency of the grep command, which will primarily single out the entry associated with the Time Zone from the timedatectl command as demonstrated below:
$ timedatectl | grep 'Time zone'
Method 2: Using date Command
As per the man page of date command, it primarily serves the purpose of printing or setting the date and time values on your Linux operating system. A segment of its execution results should point us to the time zone our system is using.
As expected, the command execution has pointed us to the correct time zone.
Method 3: Using cat Command
According to its manual page, the cat command primarily prints out the standard output associated with a concatenated file. The /etc Linux OS directory contains a file called timezone. Printing the content of this file via the cat command should reveal to us the current system time zone.
We are now confident in our ability to get the current time zone in Linux from the command-line environment.
How to Check Timezone in Linux
In this short article, we will walk newbies through the various simple ways of checking system timezone in Linux. Time management on a Linux machine especially a production server is always an important aspect of system administration.
There are a number of time management utilities available on Linux such as date and timedatectl commands to get the current timezone of system and synchronize with a remote NTP server to enable an automatic and more accurate system time handling.
Well, let us dive into the different ways of finding out our Linux system timezone.
1. We will start by using the traditional date command to find out present timezone as follows:
Alternatively, type the command below, where %Z format prints the alphabetic timezone and %z prints the numeric timezone:
Note: There are many formats in the date man page that you can make use of, to alter the output of the date command:
2. Next, you can likewise use timedatectl, when you run it without any options, the command displays an overview of the system including the timezone like so:
More so, try to employ a pipeline and grep command to only filter the timezone as below:
$ timedatectl | grep “Time zone”
3. In addition, users of Debian and its derivatives can display the content of the file /etc/timezone using cat utility to check your timezone:
Important: For REHL/CentOS 7 and Fedora 25-22 users, the file /etc/localtime is a symbolic link to the timezone file under the directory /usr/share/zoneinfo/.
However, you can use date or timedatectl command to display the current time and timezone as well.
To change the timezone, create the symbolic link /etc/localtime to the appropriate timezone under /usr/share/zoneinfo/:
$ sudo ln -sf /usr/share/zoneinfo/zoneinfo /etc/localtime
The flag -s enables creation of a symbolic link, otherwise a hard link is created by default and -f removes an existing destination file, which in this case is /etc/localtime.
For example, to change the timezone to Africa/Nairobi, issue the command below:
$ sudo ln -sf /usr/share/zoneinfo/Africa/Nairobi /etc/localtime
That’s all! Do not forget to share you thoughts about the article by means of the feedback form below. Importantly, you should look through this time management guide for Linux to get more insight into handling time on your system, it has simple and easy-to-follow examples.
Lastly, always remember to stay tunned to Tecmint for the latest and interesting Linux stuff.
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
15 thoughts on “How to Check Timezone in Linux”
Any suggestion where should I check further? All settings seem correct? It looks like there is an extra character at the end of America/Chicago. ERROR:
tzone_read_system: no match found for America/Chicago^? Here are my current settings
[[email protected] etc]# timedatectl Local time: Thu 2020-04-23 11:28:51 CDT Universal time: Thu 2020-04-23 16:28:51 UTC RTC time: Thu 2020-04-23 16:28:51 Time zone: America/Chicago (CDT, -0500) NTP enabled: no NTP synchronized: yes RTC in local TZ: no DST active: yes Last DST change: DST began at Sun 2020-03-08 01:59:59 CST Sun 2020-03-08 03:00:00 CDT Next DST change: DST ends (the clock jumps one hour backwards) at Sun 2020-11-01 01:59:59 CDT Sun 2020-11-01 01:00:00 CST [[email protected] etc]# [[email protected] etc]# ls -ltr /etc/localtime lrwxrwxrwx. 1 root root 37 Mar 26 12:20 /etc/localtime -> ../usr/share/zoneinfo/America/Chicago [[email protected] etc]# [[email protected] etc]# timedatectl | grep local RTC in local TZ: no [[email protected] etc]# [[email protected] etc]# timedatectl list-timezones | grep Chicago America/Chicago [[email protected] etc]# [[email protected] etc]# date Thu Apr 23 11:34:36 CDT 2020 [[email protected] etc]#
CentOS Linux release 7.7.1908 (Core). We just fixed the issue by “rerunning” the timezone. The log showed bad characters at the end, America/Chicago^? (^?) . We ran tzselect and then chose options 2, 49, 11 for Chicago. Gracefully rebooted the server “shutdown -r now” Thank you, Aaron, for your reply. Reply
@Ketan Great! Many thanks for sharing the solution. This will be of benefit to readers in the future. Reply
The formatting on your «%Z %z» example is a bit off. When your text is displayed, the usual, plain double quotes («) are replaced with “pretty” ones (”) . When I pasted your example into my terminal, I only received errors. You should format your example as:
(Here’s hoping that when I post this comment, the website doesn’t replace my simple quotes with pretty ones!) Reply
Yes, my simple quotes were replaced with pretty ones. That’s not good for displaying things on a technical website. You should see about disabling that feature. Reply
On Centos 7 there’s no “clock” under /etc/sysconfig. And neither is there a /etc/timezone, which, from what you’re saying, should exist on most or all linux distributions. Reply
@lethargo Yap, you can use /etc/localtime instead of /etc/timezone. Thanks for mentioning that. Reply
Indeed, but /etc/localtime is not plain text, it is a binary file. To be more specific, it is a symbolic link to a binary file found in /usr/share/zoneinfo. So you couldn’t simply grep it in Centos 🙂 You simply create a symbolic link to whatever timezone you want and then you can use date or timedatectl to see the current clock. Reply
@lethargos Once again, many thanks for the vital insight into checking and managing timezone on RHEL/CentOS 7, this will be very helpful to users out there. Reply
You’re welcome, but shouldn’t you update the article accordingly, so that other users actually know? There’s a higher chance that they’ll read the article than the comments.
@lethargos As you usefully suggested, we have updated the article to include the correct way of setting and checking timezone in REHL 7/CentOS 7/Fedora 25-22 systems. Many thanks for always following us and offering constructive thoughts.
Hello, How can I change the timezone and how can i set the time period of 12 hrs instead of 24hrs? Reply