1 AUTHOR: Archaic <archaic@remove-this.indy.rr.com>
5 LICENSE: GNU Free Documentation License http://www.gnu.org/licenses/fdl.txt
7 SYNOPSIS: Explanation of timezones, UTC, and the TZ environment variable
9 DESCRIPTION: Your Linux system is multinational. It can not only talk in
10 different languages, but it's aware of timezones, too. This hint tells you how
13 PREREQUISITES: The script provided is based on lfs-bootscripts-1.11, but should
14 be easy to modify even if major changes happen to the boot scripts.
16 HINT: Earth is divided in 24 time zones. The local time in a zone depends on
17 how many zones it is away from Greenwich, Great Britain. The difference in time
18 is relative to the 0 zone over Greenwich. For example, in Indiana, USA (where I
19 live) we're in the -5 zone. That means, it's five hours earlier here than in
20 Great Britain. This compensates for the world turning.
24 The first and most important question you'll have to answer is whether you
25 want to store the time in your machine in either UTC or local time format. UTC
26 (Universal Time Coordinated) is the same as GMT (Greenwich Mean Time). Local
27 time is the time that is displayed on a clock hanging on a wall near you. Each
28 format has its own advantages and disadvantages, but both of them are
29 discussed in this hint.
31 Traditionally, all POSIX machines (i.e. Solaris, BSD, UNIX) have their
32 system time in UTC format. The more stupid OS's (mainly the Microsoft ones)
33 require their users to configure their machines for local time. Fortunately,
34 Linux can handle both the normal UTC machines and the machines suffering from
35 Microsoft diseases that have their system time in local format.
37 At this point, you'll have to decide what it's gonna be; local or UTC time.
38 Some guidelines: If you're running Windows and Linux together on 1 box, I
39 recommend you use local time. If you have Windows but you hardly use it or if
40 you don't have Windows at all, it's a good idea to store your time in UTC
41 format. Once you've decided, edit /etc/sysconfig/clock. Use UTC=0 for local
42 time and UTC=1 for UTC (GMT) time.
44 ### Determining Your Timezone ###
46 Knowing what timezone you're living in is important for the rest of this
47 hint. But it's not enough to know how many zones you're away from Greenwich,
48 since daylight saving is also influenced by this choice. LFS comes with an easy
49 program to determine your timezone in only a few questions (usually 2 or 3).
54 When this program quits, the last line it prints is your timezone. Here, it
55 prints "America/Indianapolis" (without the quotes). Remember this value. Write
56 it down or put it somewhere in a text file. This variable will be referenced to
57 as $TIMEZONE in the rest of this hint to simplify explanations.
59 ### Using the $TIMEZONE Setting ###
61 Now that you know what timezone you're living in, we can put this knowledge
62 into practice. First, we start by creating the /etc/localtime file. This file
63 needs to exist whether you choose UTC or local time. This can be a symlink or a
64 copy of the real file renamed to localtime. At the time of this writing, LFS
65 was using a real file, so I will use that method, though a symlink will work
68 # cp --remove-destination /usr/share/zoneinfo/$TIMEZONE /etc/localtime
71 Next, we need to modify the setclock boot script, but first a brief
72 explanation of system time and hardware time.
74 The hardware clock is the clock that ticks in your BIOS and keeps the time
75 even if the system is powered off. However, the hardware clock is not all that
76 precise. The system clock is the time that the kernel maintains while it's
77 running and it is considerably more precise. But how does the system clock know
78 what time it is? During boot up, the standard LFS boot scripts set the system
79 clock from the hardware clock. After that, the hardware clock is ignored. The
80 problem with this scenario is that after many days of uptime (depending on the
81 accuracy of your BIOS), there could be a rather large discrepancy between the
82 system clock and the hardware clock. When you reboot, the still inaccurate
83 hardware clock will be used to set the system clock. Therefore, it would
84 behoove us to allow the kernel to update the BIOS time at shutdown so that the
85 BIOS will be more likely to have an accurate time when the system boots up the
86 next time. Reading from and writing to the hardware clock is done by a program
87 named hwclock(8). This program is part of a normal LFS system, so you don't
88 need to download it. We'll modify some of the boot scripts to make this program
89 run at power up and power down.
91 NOTE: Keith Harwood informed me that his DS10 Alpha box's hardware clock is
92 more accurate than its system clock. The system clock was losing a couple of
93 minutes a day. This is most likely a kernel flaw of some sort, but you should
94 probably check your system's time against a known good time and compare it after
95 the system has been running at least a day. I doubt any x86 users will find the
96 hardware clock more accurate than the system clock.
98 In the book, a script named setclock is created in /etc/rc.d/init.d. This
99 script performs only half of the job, because it only sets the system time from
100 the hardware clock. We will modify this script and create some additional
101 symlinks to make this script save the system time to the hardware clock at
104 cat >/etc/rc.d/init.d/setclock <<"EOF"
106 # Begin $rc_base/init.d/setclock - Setting Linux Clock
108 # Based on setclock script from LFS-3.1 and earlier.
109 # Rewritten by Gerard Beekmans - gerard@linuxfromscratch.org
110 # Rewritten by Marc Heerdink to include writing to the hardware clock
111 # Rewritten by Archaic <archaic@remove-this.indy.rr.com> to conform to
112 # lfs-bootscripts-1.11
114 source /etc/sysconfig/rc
116 source /etc/sysconfig/clock
122 echo "Setting system clock..."
123 hwclock --hctosys --utc
128 echo "Setting system clock..."
129 hwclock --hctosys --localtime
134 echo "Invalid value for UTC in /etc/sysconfig/clock: $UTC"
135 echo "Valid values for UTC are 1 and 0."
144 echo "Updating hardware clock..."
145 hwclock --systohc --utc
150 echo "Updating hardware clock..."
151 hwclock --systohc --localtime
156 echo "Invalid value for UTC in /etc/sysconfig/clock: $UTC"
157 echo "Valid values for UTC are 1 and 0."
164 echo "Usage: $0 {start|stop}"
169 # End $rc_base/init.d/setclock
172 chmod 755 /etc/rc.d/init.d/setclock
174 Next are the symlinks. The symlink to run the setclock script is already
175 present in /etc/rc.d/rc.sysinit.d, so the only symlinks we have to create are
176 the ones to run setclock when the system shuts down:
178 # ln -s ../init.d/setclock /etc/rc.d/rc0.d/K45setclock &&
179 # ln -s ../init.d/setclock /etc/rc.d/rc6.d/K45setclock
181 At this point, the boot scripts are correctly set up and the only thing
182 that's left to configure is the TZ environment variable.
184 ### TZ Environment Variable ###
186 This variable is used by hwclock, when it's run from a shell, and some
187 programs that heavily depend on timezones. This variable is used system-wide,
188 so it's a good idea to have it in the system-wide environment that is set up in
189 /etc/profile. Add these lines to your /etc/profile:
191 export TZ=value_of_$TIMEZONE
193 ACKNOWLEDGEMENTS: This hint was originally written by Marc Heerdink. It's still
199 * Marc Heerdink's final version
202 * Edited out irrelevant or redundant info
203 * Fixed some typos and grammatical errors
204 * Updated the script and file locations to match current LFS
205 * Update to new hint format