Time Tools
Summary:
- Timing Processes
- Inquiring Time And Date
- Calendar
- Time Zones And Setting Time
- Keeping Time
- High Accuracy NTP Server
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
$ time updatedb<br> 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
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
This bash specific command prints "the accumulated user and system times for processes run from the shell."
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
date
In it's most simple form, 'date' echoes the current system time and date:
$ date<br> 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<br> Wed Oct 31 13:12:30 CET 2001
$ date -d 1969-04-16<br> Wed Apr 16 00:00:00 CET 1969
$ date -d pst<br> 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<br> 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').
$ date -d "1965-05-20" +%U<br> 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'<br> 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.
Calendar
The
cal
$ 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
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
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
The command line tool to set the hardware clock is
hwclock
# hwclock ––show<br> 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).
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
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).
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.
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
# Sites commonly use the names "time", "tick", or "tock".<br> server time.domain1.com prefer<br> server tick.domain2.com<br> server tock.domain3.com<br> server 127.127.1.0 # local clock<br> 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<br> 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
time.yourdomain.com
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
Legal: This page is covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB and Mandrakesoft.
Version 1.4 last modified by BjornUnden6 on 24/10/2004 at 16:16
Document data
- Lost account?
- Join the community, be part of the Club: it's free!
- Get the PWP Download Subscription!