Pages

Saturday, 21 September 2019

timezone and locale on Linux

timezone and locale are two important settings affecting what the input/output format is used.

1. System-wide setting

1.1 timezone

  • config file: /etc/localtime
  • config tools: 
    • tzselect (provided by glibc)
    • timedatectl (provided by systemd)
[root@cf-31 LC_TIME]# ls -l /etc/localtime 
lrwxrwxrwx. 1 root root 37 Jul 29 23:44 /etc/localtime -> ../usr/share/zoneinfo/America/Toronto

All timezone info files are located under: /usr/share/zoneinfo/.

1.2 locale

  • config file: /etc/locale.conf
  • config tools:
    • locale (provided by glibc)
    • localectl (provided by systemd)

[root@cf-31 LC_TIME]# cat /etc/locale.conf 
LANG="en_CA.UTF-8"

All locale files are located under: /usr/share/locale.

2. Per process setting

Programs using glibc API automatically timezone-aware and locale-aware. The APIs take environment variables TZ, LC_ALL into account. Most unix tools take advantages of glibc. Here we use "date" and "ls" for examples.

2.1 TZ for timezone

# TZ=":Asia/Chongqing" date; date
Sat Sep 21 15:07:49 CST 2019
Sat Sep 21 03:07:49 EDT 2019

# TZ=":Asia/Chongqing" ls -l /etc/passwd; ls -l /etc/passwd
-rw-r--r--. 1 root root 2313 Jul 31 02:47 /etc/passwd
-rw-r--r--. 1 root root 2313 Jul 30 14:47 /etc/passwd

2.2 LC_ALL for locale


3. NO timezone and locale at kernel space

The Unix kernel only keeps time in UTC. All timezone info and locales happen only in userspace.
The system call "time()" always return UTC time. It's the userspace apps responsibility to display the current time properly.



No comments:

Post a Comment