Locales, character encodings and time zones

Making a system speak the right language and keep the right time zone: locale variables and their precedence, character encodings, and the difference between the hardware clock and system time.

Lesson 3 of 3 in objective 107. Administrative tasks, part of LPIC-1 Exam 102-500.

Locale precedence. LC_ALL is the sledgehammer. From wins to only if nothing above is set: LC_ALL (overrides every category at once), then LC_TIME, LC_NUMERIC, LC_COLLATE… (one category each), then LANG (the fallback for anything not set). wins LC_ALL overrides every category at once LC_TIME, LC_NUMERIC, LC_COLLATE… one category each LANG the fallback for anything not set only if nothing above is set
Locale precedence. LC_ALL is the sledgehammer.

Locales

A locale name looks like en_GB.UTF-8: language, territory, and the character encoding. The environment variables that select it form a precedence chain — LC_ALL overrides everything, the individual LC_* variables (LC_TIME, LC_NUMERIC, LC_COLLATE, LC_MESSAGES) set one category each, and LANG is the fallback for anything not otherwise set. So LC_ALL is the sledgehammer, and setting it is how scripts force predictable output: LC_ALL=C sorts and formats in the plain, unlocalised way, which is why so many scripts begin with it.

The precedence is what makes a single category the precise tool, and the exam asks it that way round: on a host where LANG is de_DE.UTF-8, wanting untranslated English messages while KEEPING German dates and numbers is export LC_MESSAGES=C and nothing broader. export LC_ALL=C or export LANG=C would take the formatting with it, and export LC_CTYPE=C changes which characters count as letters rather than which language anything is written in. The variable has to be exported, like DISPLAY or PATH, because what reads it is the program you are about to run and not the shell.

locale prints the current settings and locale -a lists what is available. The character encoding half matters on its own: ASCII covers 128 characters, the ISO-8859 family extended that to one byte per character per region, and Unicode covers everything — with UTF-8 the variable-width encoding of it that is compatible with ASCII for the first 128 code points. iconv converts a file between encodings.

ASCII is not a rival of Unicode; it is the first 128 code points of it. ASCII and Unicode in UTF-8 overlap. ASCII holds nothing of its own. Unicode in UTF-8 alone: Every other character there is, encoded at variable width. In both: The first 128 code points, unchanged by UTF-8. What each holds alone In both ASCII nothing of its own — all of it is in the middle Unicode in UTF-8 Every other character there is, encoded at variable width The first 128 code points, unchanged by UTF-8
ASCII is not a rival of Unicode; it is the first 128 code points of it.

Time zones and the two clocks

The machine has a hardware clock in its firmware and a system clock the kernel keeps. hwclock reads and writes the hardware one; date reads and sets the system one. The hardware clock is best kept in UTC, with the local time derived from it, because that is what makes daylight saving a display concern rather than a stored one.

The time zone is /etc/localtime, which is a copy of or symlink to a file under /usr/share/zoneinfo/. /etc/timezone on Debian systems names it in plain text. The TZ environment variable overrides it for one process, and timedatectl is the systemd command that reads and sets time, time zone and NTP synchronisation in one place — timedatectl set-timezone Europe/Berlin, timedatectl list-timezones.

On a machine with no systemd there is no timedatectl, and the zone is set by making that link by hand: ln -sf /usr/share/zoneinfo/Europe/Lisbon /etc/localtime, with -s for a symbolic link and -f to replace the link that is already there. What matters is that /etc/localtime must BE the zone data — a copy of the file or a link to it — and not the name of a zone. Writing the string Europe/Lisbon into /etc/localtime produces a file the C library cannot parse, which is a favourite distractor precisely because it is what /etc/timezone legitimately contains one directory along.

One UTC instant, handed along until it is shown as local time. In order: Hardware clock (in firmware, best kept in UTC; hwclock reads and writes it), then System clock (the one the kernel keeps; date reads and sets it), then /etc/localtime (a zone file from /usr/share/zoneinfo/; TZ overrides it per process), then Local time on screen (daylight saving applied here, not stored in either clock). Hardware clock in firmware, best kept in UTC; hwclock reads and writes it System clock the one the kernel keeps; date reads and sets it /etc/localtime a zone file from /usr/share/zoneinfo/; TZ overrides it per process Local time on screen daylight saving applied here, not stored in either clock
One UTC instant, handed along until it is shown as local time.

Worth carrying in

LANG / LC_* / LC_ALL
Fallback, per-category, and the override that beats both.
LC_ALL=C
Plain unlocalised behaviour — predictable sorting and formatting for scripts.
locale -a
Every locale installed.
iconv -f -t
Convert a file between character encodings.
/etc/localtime
The active time zone, from /usr/share/zoneinfo/.
TZ=Europe/Berlin
Override the time zone for one process.
hwclock
The firmware clock. date reads and sets the system clock.
timedatectl set-timezone
Time, zone and NTP state in one systemd command.
ln -sf /usr/share/zoneinfo/… /etc/localtime
Setting the zone without systemd. The file holds zone DATA, never a zone name.
export LC_MESSAGES=C
English messages while every other category keeps its locale.

What the exam does with this

Objective
107. Administrative tasks
Share of the exam
20% (the whole objective)
Questions in this lesson
15
Signed for by a person
0

Partly checked. None of the 15 questions here has been read against the cited source by a person. 15 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Locales, character encodings and time zones

Questions in this lesson

Practise Locales, character encodings and time zones

The rest of objective 107