6

On Fedora 30, I'm currently displaying the date in 24 hour format in the desktop environment. I'm syncing time via chronyd. But on the CLI, the time is displaying as Fri 22 Nov 2019 02:47:51 PM EST for all users.

I want to set the time format to use the 24 hour clock. timedatectl setting and re-logging as the user does not make any difference.

Kahn
  • 1,652
  • 2
  • 19
  • 36

1 Answers1

19

If you use the date command on the command line, and get back something like Fri 22 Nov 2019 02:47:51 PM EST, then it means your locale settings are currently set to a locale that specifies a 12-hour time format.

Type locale to view the current locale settings. Basically, all the locale settings are just environment variables, but there are some dependencies between them.

The logic of the locale settings is as follows:

  • First, the LANG variable will define the default locale for all the LC_* locale categories, unless there is a more specific setting for a particular category.

  • Each locale category has a name, like LC_CTYPE for the character set being used, LC_COLLATE for sorting order, and LC_TIME for the representation of date and time. If an environment variable corresponding to the name of the locale category exists, it will override the default set by LANG.

  • Finally, there is an ultimate override LC_ALL. If it is set, it will override all the other locale settings. It is typically used in scripts or similar as export LC_ALL=C, to ensure that the output of any commands used by the script will be in the POSIX default US English format instead of any localized format that might make parsing the output more difficult. (Of course, parsing the output intended for humans in a script is a non-ideal choice in the first place - if a command has a "script-friendly output" option, you should use it instead!)

Historically, on RedHat/Fedora, the system default locale settings should be in /etc/sysconfig/i18n file (i18n = internationali[s|z]ation, 18 characters between i and n). On modern versions, ther should be a localectl command you can use to query and modify the locale and keyboard layout settings persistently. Note that environment variable settings can always be used to override system-wide locale defaults!

If you are using the default en_US.UTF-8 locale, then en_GB.UTF-8 would be the closest equivalent with 24-hour time representation. To set it, use:

localectl set-locale LC_TIME=en_GB.UTF-8

Then logout & log back in to have the change take effect in your session.

If you use some other locale, use localectl list-locales to see the available locale definitions.

If you are unsure, you can test the output by specifying the desired locale as an environment variable for the date command:

$ LC_TIME=en_GB.UTF-8 date
Sat 23 Nov 15:47:16 EET 2019
$ LC_TIME=en_US.UTF-8 date
Sat 23 Nov 2019 03:47:19 PM EET
telcoM
  • 87,318
  • 3
  • 112
  • 232
  • 11
    For a more "Computerized" locale, I prefer: `localectl set-locale C.UTF-8`. It's still english but more standardized for computers. The time format is 24h. – rodvlopes Jun 20 '20 at 22:05
  • @rodvlopes Some distributions (e.g. older RHEL, I think?) do have the `C` locale, but not `C.UTF-8`... and the character set of the plain `C` locale might be constrained to essentially just US-ASCII. – telcoM Aug 09 '22 at 16:40
  • How do I set locale for just this user? `localectl set-locale LC_TIME=en_GB.UTF-8 ==== AUTHENTICATING FOR org.freedesktop.locale1.set-locale === Authentication is required to set the system locale. ` – Anton Duzenko Jun 09 '23 at 19:10
  • @AntonDuzenko Locale settings are essentially just environment variables. Put `export LC_TIME=en_GB.UTF-8` to an appropriate login script and you're done. Desktop environments might even have a GUI tool for setting per-user locale settings. – telcoM Jun 09 '23 at 19:53
  • Could you give an example? I have a server where `LC_TIME="en_US.UTF-8"` and it still prints in 24h format – Anton Duzenko Jun 10 '23 at 09:46
  • @AntonDuzenko The `export` in the beginning is important. And in Debian 11's KDE, the GUI for regional settings is Applications -> Settings -> System Settings -> Regional Settings -> Formats, enable "Detailed Settings" and there you have a dropdown of locales for "Time". – telcoM Jun 10 '23 at 16:25
  • @telcoM actually it's a CentOS with ssh access only – Anton Duzenko Jun 21 '23 at 10:59