Solve the time zone problem in Embedded Linux
If I want to do the upper-layer software work, I can do it with ease, but I am not familiar with platform work (system problem solving, driver writing, software porting, etc. Therefore, many problems are caused by feeling the stones and crossing the river without experience. Many problems are a piece of cake for experienced friends, but they are always in front of me. I am ridiculed as a weak chicken "~
Recently I am working on the R & D of home gateway based on Realtek chip RTL8196E. Realtek provides a Linux SDK development environment. Because there is no RTC on the hardware, the Linux system (hereinafter referred to as RTLinux) clock is incorrect. Time Synchronization is required. I can integrate ntpd from busybox. However, I found that even if the synchronization is completed, the time zone output by the date command is incorrect.
I have found many ways to set the time zone in Linux on the Internet. Link the/usr/share/zoneinfo/Asia/Shanghai symbol to/etc/localtime. I tried on my PC Linux (hereinafter referred to as PCLinux) and it worked. However, using the same method in RTLinux does not have any effect.
This is what bloggers want to complain: most of the information found on the Internet tells you how to do it and what goals can be achieved without explaining why it is necessary and what is its internal principle. If you don't know why, you won't be able to make any changes.
There are two ways to solve the above problem:
Set the/etc/TZ file to specify the time zone in the file
Set TZ Environment Variables
If you don't want to go into the cause, you don't have to look at the content later.
The blogger first discovered the seemingly Time Zone meaning of the/etc/TZ file. Its content is PST8PDT7, which does not know what it means. Try to change it.
Go to busybox and find "/etc/TZ" to see who used this file. No reference is found.
Go to the Linux kernel. No ~
Go to boards. Result found:
It can be seen that it is in the romfs/lib/libc. so.0 c library file.
This library file is copied from the toolchain tool set.
Then go to the toolchain and find out many results:
The problem with this time zone was originally determined by uclibc.
Let's see how the/etc/TZ file is solved in uclibc/libc/misc/time. c.
The read_TZ_file () function reads data from the _ UCLIBC_TZ_FILE_PATH _ macro (value: "/etc/TZ.
Set the time zone in _ time_tzset (int use_old_rules.
General process:
| 12345 |
e = getenv("TZ"); if ((!e && !(e = read_TZ_file(buf))) || !*e) { //!ERROR } |
It can be seen that e is first read from the environment variable, if it is not read from/etc/TZ,This is what we want.
The next step is how to handle the parsing. I will not spend any time studying this process. I will take a look at the relevant instructions.
In the official website of uclibc: http://www.uclibc.org/FAQ.html find "TZ", there are:
Find the answer in http://lists.uclibc.org/pipermail/uclibc/2002-August/004010.html:
OK, I am all clear. If it is in China, it should be set to: "CST-8", try:
Successful!
There are two solutions:
- Directly write the time zone information to the/etc/TZ File
- When the system starts, set the environment variable TZ
Both can be done. We first write the "CST-8" in the/etc/TZ file by default"
Solve the problem!
This article permanently updates the link address: