home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LINUX / HOWTO / mini / clock.txt < prev    next >
Text File  |  1997-07-07  |  10KB  |  243 lines

  1. The "Inaccurate Clock" Mini-HOWTO
  2. =================================
  3. By rbean@execpc.com
  4. last revised Dec. 1996
  5.  
  6.    The real-time-clock chips used on PC motherboards (and even expensive
  7. workstations) are notoriously inaccurate. Linux provides a simple way to
  8. correct for this in software, making the clock potentially *very* accurate
  9. even without an external time source. But most people don't seem to know
  10. about it, for several reasons:
  11.  
  12. 1: It's not mentioned in most of the general "how to set up linux"
  13.    documentation, and it would be difficult to set up automatically at
  14.    install time (although not impossible in theory, if you have a modem).
  15.  
  16. 2: If you check 'man clock' you'll get clock(3), which is not what you
  17.    want. (try 'man 8 clock').
  18.  
  19. 3: Most people don't seem to care what time it is anyway.
  20.  
  21. 4: Those few who do care often want to use the xntpd package from
  22.    louie.udel.edu to sync to an external time source, such as a
  23.    network time server or radio clock.
  24.  
  25.    This mini-HOWTO describes the low-tech approach. If you're at all
  26. interested in this sort of thing, I highly recommend that you spend some
  27. time at http://www.eecis.udel.edu/~ntp/ which includes all kinds of
  28. interesting stuff, including complete info on xntpd and links to NIST and
  29. USNO (I have a few more comments on xntpd at the end.) 
  30.  
  31.    NOTE: If you run more than one OS on your machine, you should only let
  32. one of them reset the CMOS clock, so they don't confuse each other.  If
  33. you regularly run both Linux and Windows on the same machine, you may want
  34. to check out some of the shareware clock programs that are available for
  35. Windows instead (follow the links from the URL above). 
  36.  
  37. Using the 'clock' program:
  38. ===========================
  39.  
  40.    Everything you need to know is in the clock(8) man page, but this
  41. mini-HOWTO will walk you through the process.
  42.  
  43.    NOTE: You must be root to run 'clock', or any other program that
  44. affects either the system time or the CMOS clock. 
  45.  
  46. Checking your installation:
  47. ===========================
  48.  
  49.    Check your system startup files for a command like 'clock -a' or
  50. 'clock -ua'. Depending on which distribution you're using, it might be
  51. in /etc/rc.local, or /etc/rc.d/rc.sysinit, or some similar place. 
  52.  
  53.    If it says 'clock -s' or 'clock -us', change the 's' to an 'a', and
  54. then check to see if you have the file /etc/adjtime, which contains a
  55. single line that looks something like this: 
  56.  
  57. 0.000000 842214901 0.000000
  58.  
  59.    These numbers are the correction factor (in seconds per day), the time
  60. the clock was last corrected (in seconds since Jan 1, 1970), and the
  61. partial second that was rounded off last time. If you don't have this
  62. file, login as root and create it, with a single line that looks like this
  63. (all zeros): 
  64.  
  65. 0.0 0 0.0
  66.  
  67.    Then run 'clock -a' or 'clock -ua' manually from the shell to update
  68. the 2nd number (use the 'u' if your clock is set to Universal instead of
  69. local time). 
  70.  
  71. Measuring your clock's drift rate:
  72. ==================================
  73.  
  74.    First, you need to know what time it is :-). Your local time of day
  75. number may or may not be accurate. My favorite method is to call WWV's
  76. voice announcment at (303)499-7111 (this is a toll call). If you have
  77. access to a network time server, you can use the 'ntpdate' program from
  78. the xntpd package (use the -b flag to keep the kernel from messing with
  79. the CMOS clock). Otherwise use 'date -s hh:mm:ss' to set the kernel time
  80. by hand, and then 'clock -w' to set the CMOS clock from the kernel clock.
  81. You'll need to remember when you last set the clock, so write down the
  82. date someplace where you won't lose it. If you used ntpdate, do 'date +%s'
  83. and write down the number of seconds since Jan 1,1970. 
  84.  
  85.    Then come back some days or weeks later and see how far the clock has
  86. drifted. If you're setting the clock by hand, I'd recommend waiting at
  87. least two weeks, and only calculate the drift rate to the nearest .1
  88. sec/day. After several months you could get to the nearest .01 sec/day
  89. (some people claim more accuracy than that, but I'm being conservative
  90. here). If you use 'ntpdate' you shouldn't have to wait that long, but in
  91. any case you can always fine-tune it later. 
  92.  
  93.    You can have cron run 'clock -a' at regular intervals to keep the
  94. system time in line with the (corrected) CMOS time. This command will also
  95. be run from your startup file every time you boot the system, so if you do
  96. that often (as some of us do), that may be enough for your purposes. 
  97.  
  98.    Note that certain programs may complain if the system time jumps by
  99. more than one second at a time, or if it jumps backwards. If you have this
  100. problem, you can use xntpd or ntpdate to correct the time more gradually. 
  101.  
  102. Example:
  103. ========
  104.  
  105. To Set time:
  106. ------------
  107. Login as root.
  108. Dial (303)499-7111 (voice), listen for time announcement. 
  109. Then type:
  110.  
  111. date -s hh:mm:ss
  112.  
  113. but don't press enter until you hear the beep.
  114. [You could use 'ntpdate' here instead of 'date', and skip the phone call]
  115. This sets the "kernel time".
  116. Then type:
  117.  
  118. clock -w   
  119.  
  120. This sets the CMOS time to match the kernel time.
  121. Then type:
  122.  
  123. date +%j
  124. [or 'date +%s' if you used 'ntpdate' instead of 'date' above]
  125.  
  126. and write down the number it gives you for next time.
  127.  
  128. To Reset Time and Check Drift Rate:
  129. ----------------------------------
  130.  
  131. Find the date you wrote down last time. 
  132. Login as root
  133. Then type:
  134.  
  135. clock -a
  136.  
  137. This sets the kernel time to match the current CMOS time.
  138. Dial (303)499-7111 (voice), listen for announcement.
  139. Then type:
  140.  
  141. date
  142.  
  143. and press enter when you hear the beep, but while you're waiting, write
  144. down the time they announce, and don't hang up yet. This tells you what
  145. time your machine thought it was, when it should have been exactly on the
  146. minute.  Now type in
  147.  
  148. date hh:mm:00
  149.  
  150. using the minute *after* the one that was just announced, and press enter
  151. when you hear the beep again (now you can hang up). For hh use the local
  152. hour. This sets the "kernel time".
  153. Then type:
  154.  
  155. clock -w
  156.  
  157. which writes the new (correct) time to the CMOS clock.
  158. Now type:
  159.  
  160. date +%j
  161. [or 'date +%s' if that's what you used before]
  162.  
  163. You now have three numbers (two dates and a time) that will allow you to
  164. calculate the drift rate.
  165.  
  166. Calculating the Correction Factor:
  167. ----------------------------------
  168.  
  169. When you ran 'date' on the minute, was your machine slow or fast? If it
  170. was fast, you'll have to subtract some number of seconds, so write it down
  171. as a negative number. If it was slow, you have to add some seconds, so
  172. write it down as positive. 
  173.  
  174. Now subtract the two dates. If you used 'date +%j', the numbers represent
  175. the day-of-year (1-365, or 1-366 in leap years). If you've passed Jan 1
  176. since you last set the clock you'll have to add 365 (or 366) to the 2nd
  177. number. If you used 'date +%s' then your number is in seconds, and you'll
  178. have to divide it by 86400 to get days. 
  179.  
  180. If you already had a correction factor in /etc/adjtime, you'll have to
  181. account for the number of seconds you've already corrected. If you've
  182. overcorrected, this number will have the opposite sign of the one you just
  183. measured; if you've undercorrected it will have the same sign. Multiply
  184. the old correction factor by the number of days, and then add the new
  185. number of seconds (signed addition-- if the two numbers have the same
  186. sign, you'll get a larger number, if they have opposite signs you'll get a
  187. smaller number). 
  188.  
  189. Then divide the total number of seconds by the number of days to get the
  190. new correction factor, and put it in /etc/adjtime in place of the old one. 
  191. Write down the new date (in seconds or days) for next time. 
  192.  
  193. Here's what my /etc/adjtime looks like:
  194.  
  195. -9.600000 845082716 -0.250655
  196.  
  197. [note 9.6 seconds per day is nearly five minutes per month!]
  198.  
  199. A few words about xntpd:
  200. ========================
  201.  
  202.    Your system actually has two clocks-- the battery powered "real time
  203. clock" that keeps track of time when the system is turned off (also known
  204. as the "CMOS clock", "Hardware clock", or "RTC") and the "kernel time" 
  205. (sometimes called the "software clock" or "system clock") which is based
  206. on the timer interrupt and is initialized from the CMOS clock at boot
  207. time.  The two will drift at different rates, so they will gradually drift
  208. apart from each other, and als