嵌入式Linux时区设置
说明在多个平台进行时区设置,发现有多种方式,有的组合设置能生效但是有些组合不能,有些不解,特记录一下。时区设置早期平台(gm8126平台)之前gm8126板子上时区设置只需要将相应的时区文件覆盖掉/etc/timezone即可。/etc/timezone是时区文件,里面内容是复杂不可读的时区信息。平台使用glibc。MTK8516平台MTK8516平台使用systemd系统为初始化进程(init)
·
说明
- 在多个平台进行时区设置,发现有多种方式,有的组合设置能生效但是有些组合不能,有些不解,特记录一下。
时区设置
早期平台(gm8126平台)
- 之前gm8126板子上时区设置只需要将相应的时区文件覆盖掉/etc/timezone即可。
- /etc/timezone是时区文件,里面内容是复杂不可读的时区信息。
- 平台使用glibc。
MTK8516平台
- MTK8516平台使用systemd系统为初始化进程(init)。
- 设置时区需要使用以下命令:
sudo timedatectl set-timezone 'Asia/Shanghai'
* 在/usr/share/zoneinfo/目录下需要有相应的时区文件,不然会报错
- /etc/localtime是具体时区文件 /usr/share/zoneinfo/xxx 的软链接,设置时区成功后/etc/localtime 会自动更新。
- /etc/timezone文件依然存在,但是内容不再是时区信息,而是简单的时区字符串,例如:‘Asia/Shanghai’。
- 平台使用glibc。
疑惑
- 不清楚/etc/timezone的具体作用,是否只是为了兼容老版本?
ssc323平台
- ssc323平台使用Busybox生成的老式sysinit为初始化进程,平台使用uclibc。
- 设置时区时,替换/etc/timezone或者创建软链接/etc/localtime链接到具体的时区文件都不生效;只能通过以下设置方法:
1. 设置 /etc/TZ 文件,在该文件中指明时区
2. 设置 TZ 环境变量
总结
- 时区设置方法不同的根源是C标准库不同,使用glibc和uclibc的时区设置方式是不同的,可以根据代码推导和官方文档确认。
- uclibc
官网FAQ说明:https://www.uclibc.org/FAQ.html#timezones
Why does localtime() return times in UTC even when I have my timezone set?
The uClibc time functions get timezone information from the TZ environment variable, as described in the Single Unix Specification Version
3. See http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html for details on valid settings of TZ. For some additional examples,
read https://www.uClibc.org/lists/uclibc/2002-August/004010.html in the uClibc mailing list archive.
You can store the value of TZ in the file '/etc/TZ' and uClibc will then automagically use the specified setting.
- uclibc不支持/etc/timezone和localtime的设置方式,只支持TZ文件和环境变量,
- 示例如下:
时区设置为 中国UTC+8
/etc/TZ 文件中写入 "CST-8"
或者
/etc/TZ 文件中写入 "UTC-8" //为什么是-8,个人也不清楚,测试设置为UTC-8时区才是中国UTC+8
- glibc
官网FAQ说明:https://sourceware.org/glibc/wiki/FAQ#How_can_I_set_the_timezone_correctly.3F
You first have to install yourself the timezone database, it is hosted at http://www.iana.org/time-zones.
Then, simply run the tzselect shell script, answer the question and use the name printed in the end by making a symlink /etc/localtime pointing to /usr/share/zoneinfo/NAME (NAME is the returned value from tzselect). That's all. You never again have to worry. Instead of the system wide setting of /etc/localtime}}, you can also set the {{{TZ environment variable.
The GNU C Library supports the extented POSIX method for setting the TZ variable, this is documented in the manual.
- glibc既支持创建软链接/etc/localtime指向具体的时区文件,也支持设置TZ环境变量的方式。
- 猜测:创建软链接/etc/localtime方式是替换/etc/timezone的升级方式,目的是减少一份时区文件的空间占用。
更多推荐
所有评论(0)