MandrakeUser.Org - Your Mandrake-Linux Knowledge Base!


 
 

*DocIndex - Utils & Apps

Time Tools

* Timing Processes
* Inquiring Time And Date
* Calendar
* Time Zones And Setting Time
* Keeping Time
* High Accuracy NTP Server

Related Resources:

man time
info date
man cal
man hwclock
man rdate
The Clock Mini-HOWTO
/usr/share/doc/ntp-{..}

Revision / Modified: Mar. 22, 2002
Author: Tom Berger, Todd Lyons

 

Every so often you want a quick check on what time it is. Or what time it is halfway around the globe. Or what week day a certain day in the past was or will be in the future. Or maybe you have one of those annoying machines which can't keep the correct system time and you want to change that.

Linux comes with a lot of built-in 'time tools' which you can easily access from the command line (and of course some graphical front-ends). This article introduces some of them.

* Timing Processes

Confusingly enough there's a time command on your machine, but it doesn't tell you the time ;-). The 'time' command derives its name from the verb, not from the noun; it is intended to measure the time and system resources a certain application needs to run:

$ time updatedb
1.10user 0.56system 0:01.69elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (390major+1742minor)pagefaults 0swaps

This tells you that the 'updatedb' process (used to update the 'slocate' database') needed 1.1 CPU-seconds in user mode, 0.56 CPU-seconds in kernel mode and 1.69 'real' seconds to complete it's task. During this task, the process got 98% of all available CPU time (computed by adding up both 'CPU-seconds' values and dividing the result by the 'real' time value).
The following values tell you something about the memory and input/output (I/O) resources used by the process (read man time for the gory details).

So 'time' makes a quick and dirty benchmarking tool, e.g. for kernel compilations. Its results, however, should be taken with some grains of salt. It's not intended to be scientifically accurate.

To complicate things further, there's also a times command.
This bash specific command prints "the accumulated user and system times for processes run from the shell."

* section index * top

* Inquiring Time And Date

Most window managers and desktop environments offer their own graphical time and date tools and / or applets. The Mandrake Control Center also comes with an embedded time utility, draktime ('System' - 'Date & Time'). Most if not all of these tools are graphical interfaces to the datecommand line tool.

In it's most simple form, 'date' echoes the current system time and date:

$ date
Thu Nov 1 13:04:19 CET 2001

If you have a look at 'date's info pages however, you will discover that it offers a plethora of options and time formatting strings.

The most interesting options are '-d', '-s' and '-u':

  • '-d' allows you to inquire times and dates which are not 'now':

    $ date -d yesterday
    Wed Oct 31 13:12:30 CET 2001

    $ date -d 1969-04-16
    Wed Apr 16 00:00:00 CET 1969

    $ date -d pst
    Thu Nov 1 09:00:00 CET 2001

    'pst' marks the Pacific Standard Time timezone. For more valid strings, check the 'Shell utilities' info page, node 'Date input formats'

  • '-s' allows the system administrator to set the system time and date:

    date -s 2001-11-01
    date -s 21:08:0

    More on setting time in the second to next chapter.

  • '-u' is a shortcut option for '-d utc' and displays the current Universal Coordinated Time (formerly known as 'Greenwich Mean Time, GMT').

The formatting strings of the 'date' command allow you to control the output in great detail. Let's say you want to know in which week May 20th 1965 was:

$ date -d "1965-05-20" +%U
20

Formatting strings follow options and the date string and have to be preceded with a '+'. You can use as many formatting strings you want:

$ date -d "1965-05-20" +'Week: %U, Day Nr: %j, Day: %a'
Week: 20, Day Nr: 140, Day: Thu

This tells you that the 20th of May 1965 was in the 20th week of the year, that it was the 140th day of that year and that it was a Thursday. Remember to put the formatting string into single quotes if it contains empty spaces.

* section index * top

* Calendar

The cal command provides you with a neat console calendar:

$ cal
November 2001
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

'cal' takes only a few options, the most relevant are:

  • '-3' displays the calendars of the previous, current and next month side by side.
  • '-y' displays the calendar for the current year.
  • '[month] [year]' displays the specified month of the specified year:
    $ cal 09 1958
    September 1958
    Mo Tu We Th Fr Sa Su
    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30

* section index * top

* Time Zones And Setting Time

The world is divided into 25 time zones, with UTC (former GMT) being the 'zero' zone. Before the widespread use of the DOS and Windows 9x operating systems, computer hardware clocks were usually set to UTC and the operating system would handle the conversion to the actual time zone.

DOS and its derivatives however can't do this conversion, they assume that the hardware clock time is the current time. That is, if you set your hardware clock to UTC, Unix operating systems and Windows NT and successors will display the current time whereas DOS and derivatives will display the UTC. If you dual-boot with latter operating systems, it is recommended to set the hardware clock to the current time. If not, set it to UTC. Mandrake Linux defaults to setting the clock to local time.
Most time related problems on Linux like 'jumping' system times, shifting time stamps in various applications (especially in mail programs), no automatic Daylight Saving Time switching are due to the fact that the hardware clock is not set to UTC.

Information on the current time zone and hardware clock setting is stored in '/etc/sysconfig/clock'. To see which time zones are available or to set a per user time zone via the TZ variable, use the tzselect utility. On a system level, you can change the time zone via the Mandrake Control Center ('System' - 'Date & Time') or by editing the 'clock' file.

If you've paid attention, you won't be surprised when I tell you that setting time in Linux happens at two levels: the system time level (with the date -s command mentioned above) and at the hardware clock level (also known as 'RTC', the 'Real Time Clock').
The command line tool to set the hardware clock is hwclock('root' only):

# hwclock --show
Thu 01 Nov 2001 04:05:19 PM CET -0.106658 seconds

How do you know if this is local time or UTC? Well, 'hwclock' can't tell you that. It derives the default value to use from the file '/etc/adjtime'. On Mandrake Linux the value is most likely set to local time, since that is what the installation program defaults to. If you want to change that default value, you have to supply either the '--localtime' or the '--utc' option to 'hwclock'.

To set the hardware clock, you'd use a command like this:

hwclock --set --date="01 Nov 2001 04:00:00"

The '--date=' strings accepts all the datum formats of the 'date' command discussed above.

The 'hwclock' man page is quite long and does a pretty good job at explaining everything in great detail. I will keep it short here and just mention the three most interesting options:

  • '--hctosys' sets the system time to the current hardware clock time.
  • '--systohc' sets the hardware clock time to the current system time.
  • '--adjust' corrects the hardware clock by the 'systematic drift' time recorded in '/etc/adjtime' (and displayed by the 'hwclock --show' command).

Notice that the time tool in the Mandrake Control Center sets system timeand hardware clock.

* section index * top

* Keeping Time

During the installation of Mandrake Linux you are given the possibility to configure an NTP (Network Time Protocol) client to keep your computer's time in sync over a network. For a single workstation and the once-in-a-while time synchronization it is a bit of an overkill.

A quick and dirty solution is the ancient rdate (package 'rdate') utility.

You will have to find a Public NTP Time Server. You are advised to

  • use a Secondary (stratum 2) Time Server, since the Primary Time Servers are often busy,
  • observe the rules of the NTP server in question,
  • choose a server in your geographical vicinity,
  • use the server only when you really need it (once a day should do).

Once you've found an NTP server, test it with:

rdate -p [server address]

If you get a 'Connection refused' error, you have to find another server. If you get something in the order of:

[server address]     Thu Nov  1 17:12:07 2001
                  

you've found a good server.

To set the system time to the time returned by the NTP server, you have to supply the '-s' option to 'rdate' and be 'root':

rdate -s [server address]

If everything goes well, you will get no message at all and the system time will be adjusted quietly.

To automate this, you can then go on and set up a cron job.

* section index * top

* High Accuracy NTP Server

This section describes how to configure a time server to achieve a degree of accuracy along the lines of +/- 0.00001 seconds accuracy or better. This server tracks the skew of your system clock and constantly adjusts it to match a derived signal based on the official atomic clock at the U.S. Naval Observatory. This method has a few advantages over the method of the previous section. The single largest advantage is the accuracy that it maintains.

First, install the ntp package. There are actually two time servers shipped with Mandrake 8.2, ntp and xntp. For the purposes of this document, we will just use ntp.

urpmi ntp

Before we try to start it, we should customize the main ntp configuration file located at /etc/ntp.conf. You must configure a master time server, one that your time server can synchronize with. In order to not overload the Primary Public Time servers (called Stratum 1), we will use Secondary Public Time servers (called Stratum 2). The official list of Stratum 2 Public Time Servers is http://www.eecis.udel.edu/~mills/ntp/clock2.htm. Go to that list and select one or more that is close to you. It is generally acceptable to use up to three sites. Most of the servers listed there mention that they would like to be notified that you are using them for a time server. The modifications to the config file:

# Sites commonly use the names "time", "tick", or "tock".
server time.domain1.com prefer
server tick.domain2.com
server tock.domain3.com
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

Now you are ready to start the server and configure it to start when your system reboots (it happens occassionally :-)

service ntpd start
chkconfig ntpd on

The final issue to be addressed is that of efficiency. If you have a small business which has a network of 20 computers, it is a burden on the official time servers to synchronize the times with all of your computers. Instead, use one machine as the main time server. This machine will synchronize with your three selected public time sources. Name it time.yourdomain.com. Then synchronize all of your other machines to the machine named time.yourdomain.com. In this manner, all of your machines will be ultimately synchronized with the official U.S. Naval Observatory atomic clock without presenting too much of a load to the official Public Time Servers.

* section index * top

 
Legal: All texts on this site are covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.