Linux get current timezone

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.
Читайте также:  Line in alsa linux

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 Check and Set Timezone in Ubuntu 20.04

In this tutorial, you will learn how to check and set the timezone in Ubuntu 20.04 using the timedatectl command.

There are a variety of ways of checking your timezone, from using the date command to the timedatactl command. However, setting your timezone has been greatly simplified and improved with the timedatactl command.

Читайте также:  Linux mint темная тема

The timedatectl command allows you to output detailed information about your system’s date in time, from NTP info to comparisons between your date with the UTC date.

While we’ve limited the scope of this tutorial to strictly being about setting your timezone, the timedatectl has additional functionality that is very useful for configurations around time and date.

Checking Your Current Timezone

The currently configured timezone is set in the /etc/timezone file. To view your current timezone you can cat the file’s contents.

Another method is to use the date command. By giving it the argument +%Z , you can output your system’s current time zone name.

To get the timezone name and offset, you can use the data command with the +»%Z %z» argument. The uppercase Z prints the timezone name, while the lowercase z outputs the time offset.

The timedatectl command be used to get more details about your system’s current time and date configurations.

 Local time: Sat 2020-09-05 03:09:26 UTC Universal time: Sat 2020-09-05 03:09:26 UTC RTC time: Sat 2020-09-05 03:09:26 Time zone: Etc/UTC (UTC, +0000) System clock synchronized: yes NTP service: active RTC in local TZ: no 

Setting Your Timezone

To set your timezone the timedatectl command will be used. Before you set your new timezone you may want to know what timezones are available. You can list all available timezones using the timedatectl list-timezones command.

timedatectl list-timezones
Africa/Abidjan Africa/Accra Africa/Algiers Africa/Bissau Africa/Cairo Africa/Casablanca Africa/Ceuta Africa/El_Aaiun Africa/Johannesburg Africa/Juba Africa/Khartoum Africa/Lagos Africa/Maputo Africa/Monrovia Africa/Nairobi Africa/Ndjamena Africa/Sao_Tome . 

The timezone list is very extensive. To narrow the list down to only your region you can pipe the results to the grep command. For example, to narrow our results to only the Americas, we would run the following command.

timedatectl list-timezones | grep America
. America/Santiago America/Santo_Domingo America/Sao_Paulo America/Scoresbysund America/Sitka America/St_Johns America/Swift_Current America/Tegucigalpa America/Thule America/Thunder_Bay America/Tijuana America/Toronto America/Vancouver America/Whitehorse America/Winnipeg America/Yakutat America/Yellowknife

Once you know the time zone you want to set your Ubuntu server to, you use the timedatactl set-timezone command to set it.

sudo timedatectl set-timezone America/Toronto

and then confirm your changes were applied by running the timedatactl command.

 Local time: Fri 2020-09-04 23:30:58 EDT Universal time: Sat 2020-09-05 03:30:58 UTC RTC time: Sat 2020-09-05 03:30:59 Time zone: America/Toronto (EDT, -0400) System clock synchronized: yes NTP service: active RTC in local TZ: no 

Источник

Читайте также:  Относительное имя файла linux

How can I display the current time & date setting?

I don’t know of a single file, but this may give you the info needed:

cat /etc/timezone grep UTC /etc/default/rcS date # hardware clock sudo hwclock --show 

Best example (IMHO) using timedatectl (in command-line/terminal):

$ timedatectl Local time: Thu 2014-07-24 19:51:23 IST Universal time: Thu 2014-07-24 14:21:23 UTC Timezone: Asia/Kolkata (IST, +0530) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a 

Visit the manpage for more settings and further information.

Additional info that may also be useful: to change time zone do «sudo timedatectl set-timezone America/Los_Angeles» (use your own time zone instead of America/Los_Angeles. Timezones can be found with timedatectl list-timezones)

Fri 22 Nov 2019 04:31:50 PM UTC

Check out info date , and for example date +’%z’

For the time zone, you can use geolocation:

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

If you need a formatted area and time zone, you can use:

$ grep `date +%Z` /etc/timezone Etc/UTC 
PS> Get-TimeZone Id : Europe/Vilnius DisplayName : (UTC+02:00) Eastern European Standard Time StandardName : Eastern European Standard Time DaylightName : Eastern European Summer Time BaseUtcOffset : 02:00:00 SupportsDaylightSavingTime : True 

I suspect this won’t be popular answer in a Linux community, but I really like the verb-noun convention. It makes it easier for me to remember commands, and it will also work on all distributions with PowerShell installed 🙂

Источник

Оцените статью
Adblock
detector