3 AUTHOR: Marc Heerdink <marc@linuxfromscratch.org>
6 This is an in-depth explanation of timezones, UTC, the TZ environment variable and similar topics.
9 version 2.1 (04/10/2002)
17 3 Determining your timezone
20 4.2 Changing bootscripts
23 4.3 TZ environment variable
24 5 Time synchronization
25 5.1 Picking an NTP server
34 Earth is divided in 24 time zones. The local time in a zone depends on how
35 many zones that zone is away from Greenwich, Great-Britain. The difference in
36 time is relative to the 0-zone over Greenwich. For example, in Holland (where I
37 live) we're in the +1-zone (that means, it's one hour later here than in Great
38 Britain). This compensates for the world turning.
39 Your Linux system is multinational. It can not only talk different languages,
40 but it's aware of timezones too. This hint tells you how you can set it up.
41 The second chapter of this hint is probably the most important one, the rest
42 of this hint depends on the choice you make in chapter 2. Good luck... :)
44 Send comments, suggestions, love-letters and improvements to:
45 Marc Heerdink <marc@linuxfromscratch.org>
52 The first and most important question you'll have to answer is whether you
53 want to store the time in your machine in either UTC or local time format. UTC
54 is Greenwich time, local time is the time that is displayed on a clock hanging
55 on a wall near you (tm). Each format has it's own advantages and disadvantages,
56 but both of them are discussed in this hint.
57 Traditionally, all POSIX machines (i.e. Solaris boxes, BSD Machines but Linux
58 boxes too) have their system time in UTC (Coördinated Universal Time) format.
59 Stupider OSes require their users to configure their machines for local time,
60 and these OSes are mainly the Microsoft ones. Fortunately, Linux can handle
61 both the normal UTC machines and the machines suffering from Microsoft diseases
62 that have their system time in local format. I still recommend UTC, that's the
63 way Linux was originally set up. Usage of local time is only a workaround.
64 At this point, you'll have to decide what it's gonna be: local or UTC time.
65 Some guidelines: If you're running Windows and Linux together on 1 box, I
66 recommend you use local time, if you have Windows but you hardly use it or if
67 you don't have Windows at all, it's a good idea to store your time in UTC
68 format. Both ways are described here.
71 ============================
72 3. DETERMINING YOUR TIMEZONE
73 ============================
75 Knowing what timezone you're living in, is important for the rest of this
76 hint. But it's not enough to know how many zones you're away from Greenwich,
77 since daylight saving is also influenced by this choice. LFS comes with an
78 easy program to determine your timezone in only a few questions (usually 2 or
83 When this program quits, the last line it prints is your timezone. Here, it
84 prints "Europe/Amsterdam" (without the quotes) because I live in the
85 Netherlands. Remember this value; write it down or put it somewhere in a text
86 file. This variable will be referenced to as _TIMEZONE_ in the rest of this
87 hint to simplify explanations.
94 Now that you know what timezone you're living in, we can put this knowledge
95 into practice. This chapter deals with the various ways a timezone needs to be
96 set up. Each subchapter describes what we're going to do and tells you how. I
97 assume you're using a virgin LFS installation. Otherwise, you'll have to do
101 =====================
102 4.1 CREATING SYMLINKS
103 =====================
105 These are symlinks that have to be present, this is not different for either
106 people with local time or people with UTC time. Why these symlinks have to be
107 created, will be explained in chapter 4.2. For now, just enter the
108 /usr/share/zoneinfo directory and create a symlink from localtime to your
109 timezone with the following command:
111 cd /usr/share/zoneinfo &&
112 ln -sf _TIMEZONE_ localtime
114 Chapter 3 describes this _TIMEZONE_ thingie. Read it if you didn't already do
115 so, it contains importand information. Next, we need another sylink in /etc:
118 ln -sf ../usr/share/zoneinfo/localtime
120 The symlinks are now set up, changing bootscripts is next.
123 ========================
124 4.2 CHANGING BOOTSCRIPTS
125 ========================
127 At boot time, the system clock that is maintained by the Linux kernel, has
128 to be synchronized to the hardware clock. The hardware clock is the clock that
129 ticks in your BIOS and keeps the time even if the system is powered time. The
130 reason to do so, is that the hardware clock is not really precise. The Linux
131 kernel uses a far more precise way of time keeping, that I'm not going to
132 describe in detail here. To keep the Linux system time when the system goes
133 down, the system time has to be written to the hardware clock when the system
134 is going to reboot or power down.
135 Writing to the hardware clock and reading from it is performed by a program
136 named hwclock(8). This program is part of a normal LFS system, so you don't
137 need to download it. We'll modify some of the bootscripts to make this program
138 run at power up and power down.
139 This chapter describes the changes that have to be made to either the SYSV
140 bootscrips that are described in the book, or to the BSD style bootscripts
141 that are described in the BSD Init hint. If you don't know what type you have,
142 you can assume you're using the SYSV type bootscripts.
149 In the book, a script named setclock is created in /etc/init.d/setclock is
150 created. This script performs only half of the job, because it only sets the
151 system time from the hardware clock. We will modify this script and create
152 some additional symlinks to make this script save the system time to the
153 hardware clock if needed. But first, you'll have to check if the
154 /etc/sysconfig/clock file is correct. This file defines a variable UTC. What
155 value this variable gets, is pretty self-explanatory after reading chapter 2.
156 Summarized: UTC=1 for UTC hardware clocks, UTC=0 for localtime clocks. Next:
157 we create a new /etc/init.d/setclock:
159 rm -f /etc/init.d/setclock
160 cat >/etc/init.d/setclock <<EOF
162 # Begin /etc/init.d/setclock
165 # Include the functions declared in the /etc/init.d/functions file
166 # and include the variables from the /etc/sysconfig/clock file
169 source /etc/init.d/functions
170 source /etc/sysconfig/clock
176 /sbin/hwclock --hctosys --utc
179 /sbin/hwclock --hctosys --localtime
182 echo "Invalid value for UTC in /etc/sysconfig/clock: $UTC"
183 echo "Valid values for UTC are 1 and 0."
191 /sbin/hwclock --systohc --utc
194 /sbin/hwclock --systohc --localtime
197 echo "Invalid value for UTC in /etc/sysconfig/clock: $UTC"
198 echo "Valid values for UTC are 1 and 0."
204 echo "Usage: $0 {start|stop}"
209 # End /etc/init.d/setclock
211 chmod 755 /etc/init.d/setclock
213 Next are the symlinks. The symlink to run the setclock script is already
214 present in /etc/init.d/rcS.d, so the only symlinks we have to create are the
215 ones to run setclock when the system shuts down:
218 ln -sf ../init.d/setclock S700setclock &&
220 ln -sf ../init.d/setclock S700setclock
222 At this point, the bootscripts are correctly set up and the only thing that's
223 left to configure is the TZ environment variable in chapter 4.3.
230 The scripts are hwclock aware out of the box. The only thing you'll have to
231 change, are the parameters for hwclock in the /etc/rc.d/rc.sysinit and
232 /etc/rc.d/rc.0 scripts. Open these scripts in a text editor and search for the
233 lines that call hwclock. These lines should look like
236 (rc.sysinit) /sbin/hwclock --hctosys --utc
237 (rc.0) /sbin/hwclock --systohc --utc
240 (rc.sysinit) /sbin/hwclock --hctosys --localtime
241 (rc.0) /sbin/hwclock --systohc --localtime
246 ===========================
247 4.3 TZ ENVIRONMENT VARIABLE
248 ===========================
250 This variable is used by hwclock when it's run from a shell, and some
251 programs that heavily depend on timezones. This variable is used system-wide,
252 so it's a good idea to have it in the system-wide environment that is set up in
253 /etc/profile. Add these lines to your /etc/profile:
259 =======================
260 5. TIME SYNCHRONIZATION
261 =======================
263 Time synchronization means "making your system display the correct time".
264 You'll learn how to synchronize your time with one of the NTP (Network Time
265 Protocol) servers out there and how to keep this time synchronized. In this
266 chapter, we'll use a handy utility named "getdate" to do the time
267 synchronization. Get it from:
269 ftp://metalab.unc.edu/pub/Linux/system/network/misc/getdate_rfc868-1.2.tar.gz
271 Now extract it in /usr/src:
274 tar xzvf /path/to/getdate_rfc868-1.2.tar.gz
283 This is everything we need for time synchronization.
286 =========================
287 5.1 PICKING AN NTP SERVER
288 =========================
290 This is the most difficult part of time synchronization. You need an NTP
291 server that is preferably in your timezone, and that is working. Pick one from
292 the list @ http://www.eecis.udel.edu/~mills/ntp/clock1a.html. Test if it works
295 getdate your.ntp.server
297 If you don't get a positive output, pick the next NTP server from the list and
298 try it again, otherwise, adjust your system time manually to a value as close as
299 possible to this ntp server. For example, if you get this:
301 ntp.foo.bar.com: (-40) Sun Mar 4 13:33:33 2001
303 Adjust your time with this command:
307 It's not really important yet what time exactly you set it to, but we need a
308 value that is close enough for getdate, since it won't work with a greater
309 difference between NTP and local time than 180 seconds.
316 Type this to synchronize your system time with the NTP server:
318 getdate -adjust 5 180 your.ntp.server
320 This is it! The correct time will be written to your hardware clock next time
321 your system shuts down (well... if you read chapter 4) so you'll have a quite
322 correct time for a few days. "A few days?!" Yes, because your hardware clock
323 isn't really precise. To make sure you always have the correct time, you'll have
324 to periodically synchronize it again. How this can be done will be explained in
332 You can keep your system clock in shape if you regularly synchronize your
333 local clock with an NTP server. It can be a good idea to do this every time you
334 connect to the internet, or (if you have a continuous connection) with a cron
335 job. If you don't like this automation, you can of course do it manually from
336 time to time. This chapter contains some examples for the given methods.
337 First method is "synchronize when the internet link goes up." In this
338 example, the standard PPP daemon is used, because it's often used for serial
339 links. Open the file /etc/ppp/ip-up in a text editor. This file gets auto-
340 matically executed when the ppp link goes up so it's safe to synchronize from
341 here. Add this line to the end:
343 /usr/bin/getdate -adjust 5 180 your.ntp.server
345 The second method is "synchronize every x hours using a cron job." There are
346 too many cron daemons out there to give an example that works for every daemon.
347 Normally, the man page for the crontab editor you use will give you enough
348 information to be able to do this yourself. If you have made such a cronjob for
349 one particular cron daemon, please let me know and I'll add it here.
350 The third and last "do it yourself"-method is pretty straightforward. Run the
351 familiar getdate -adjust line every once in a while...