System Time

Team-Fly    

Solaris™ Operating Environment Boot Camp
By David Rhodes, Dominic Butler
Table of Contents
Chapter 19.  Time, Date, and NTP


On most UNIX systems, time begins at 00:00:00 January 1, 1970, Greenwich Mean Time (GMT), which is often known as the "epoch." The kernel maintains a count of the number of seconds that have passed since the epoch and uses this count for everything that is time-related, such as file creation, time stamps, user logging, and how long processes have been running.

GMT or UTC

Historically, time standards have always used GMT as the reference for all other time zones. In 1986, UTC (Universal Time Coordinated) was introduced as the replacement for the international standard for time. UTC is based on atomic measurements, whereas GMT is based on the Earth's rotation. For this reason, UTC is a more accurate value. However, as far as we are concerned, UTC can be thought of as equivalent to GMT; the differences are unimportant at the levels of accuracy we are dealing with here.

Displaying Time

Whenever any commands are run, if they need to display a date, the internal value for the time will first be converted into UTC and from there into a displayable format using a series of rules that defines how to convert the time to suit the geographical location of the system. These rules are known as "timezones." Various commands exist to set and query the system time and to define the time zone rules to the system. These commands are described in the sections below.

The value for time is stored as a 32-bit value on many UNIX systems. For these, time will "end" at 03:14:07 January 19, 2038, GMT. However, Solaris now uses a 64-bit value for time, which is good news for us as time will "continue" for the next 10 billion years or so!

We can view the system date before it is converted using the time zone rules by displaying it as UTC. A comparison between the local time and UTC is shown below:

 hydrogen# date u; date Thursday April  4 08:18:57 GMT 2002 Thursday April  4 09:18:57 BST 2002 hydrogen# 

Date

Setting the correct time and date on the system is a fundamental requirement. If it is not correct, we cannot be sure that the reported times for user logins are correct, or that log entries in a file are in the correct order. At its most basic, setting the date is a manual and therefore relatively inaccurate task (well, maybe to the nearest second or so). To do this, we would use the date command as shown below:

 hydrogen# date 102217302002 Sun October 22 17:30:00 GMT 2002 hydrogen# 

Setting the time this way is fine for a few machines. But it rapidly becomes a headache to administer as more machines are added, and also becomes more complex if you are trying to maintain a close accuracy between the systems.

Since the system clock is not 100 percent accurate, we should run date on a regular basis to ensure that the time is set correctly, probably by setting the date to a reference time such as a Web page, a speaking clock, or some other system that is known to be accurate. Before we see how to do this, let's look at how the time zones we mentioned are involved.

Time Zones

In the output from date, we can see that the time zone setting is also displayed. The output below shows this is currently set to GMT on our system:

 hydrogen# date Sun October 22 17:30:00 GMT 2002 hydrogen# 

Time zones are controlled by an environmental variable named TZ:

 hydrogen# echo $TZ GB hydrogen# 
Setting the Time Zone

All systems should be configured for a particular time zone; this allows the system to set the clock to the rules that each time zone describes. There are various ways of setting this, although most users will perform this task once, when the operating system is installed by answering a question about where the system is located. This will place an entry for TZ into a file named /etc/default/init:

 hydrogen# grep TZ /etc/default/init TZ=GB hydrogen# 

Other UNIX systems and older versions of Sun OS used to place the time zone information in a file named /etc/TIMEZONE. To ensure compatibility with these systems, this file is still used in Solaris, but is now linked to /etc/default/init:

 hydrogen# ls -ld /etc/TIMEZONE lrwxrwxrwx  1 rootroot14 Oct 22 1999 /etc/TIMEZONE -> ./ default/init hydrogen# 
What Do the Time Zone Settings Do?

The time zone settings provide us with an indication of the system's geographical location, or probable intended location. Secondly, and most importantly, the settings allow the system to apply a series of rules that govern how the time is displayed for that particular time zone. For example, we know that in the UK the time zone is GMT and that we switch to BST (British Summer Time) in March, then back again to GMT in October. Whereas in China, for example, the switch occurs between mid-April and mid-September. By knowing which time zone it is set up for, the system is able to apply these changes automatically.

It is important to note again that the time zone settings do not alter the system clock in any way, simply the way the time is displayed. This can always be confirmed by comparing the output of the local date and the date in UTC, as we showed earlier.

Once we have set the systems correctly, it may be useful on occasions to check the time zone rules (for example, to confirm exactly when BST occurs for a particular year). All the time zone files are located in /usr/share/lib/zoneinfo and contain the time conversion information in a compiled form. To view these rules, we use zdump as shown below:

 # zdump -vc 2004 GB GB  Thu Apr  4 07:36:47 2002 GMT = Thu Apr  4 08:36:47 2002 BST isdst=1 GB  Fri Dec 13 20:45:52 1901 GMT = Fri Dec 13 21:45:52 1901 BST isdst=0 GB  Sat Dec 14 20:45:52 1901 GMT = Sat Dec 14 21:45:52 1901 BST isdst=0 GB  Sun May 21 01:59:59 1916 GMT = Sun May 21 02:59:59 1916 BST isdst=0 GB  Sun May 21 02:00:00 1916 GMT = Sun May 21 03:00:00 1916 BST isdst=1 GB  Sun Oct  1 01:59:59 1916 GMT = Sun Oct  1 02:59:59 1916 BST isdst=1 GB  Sun Oct  1 02:00:00 1916 GMT = Sun Oct  1 02:00:00 1916 GMT isdst=0 ...<output removed for clarity> GB  Sun Mar 30 00:59:59 2003 GMT = Sun Mar 30 00:59:59 2003 GMT isdst=0 GB  Sun Mar 30 01:00:00 2003 GMT = Sun Mar 30 02:00:00 2003 BST isdst=1 GB  Sun Oct 26 00:59:59 2003 GMT = Sun Oct 26 01:59:59 2003 BST isdst=1 GB  Sun Oct 26 01:00:00 2003 GMT = Sun Oct 26 01:00:00 2003 GMT isdst=0 GB  Tue Jan 19 03:14:07 2038 GMT = Tue Jan 19 03:14:07 2038 GMT isdst=0 GB  Mon Jan 18 03:14:07 2038 GMT = Mon Jan 18 03:14:07 2038 GMT isdst=0 hydrogen# 

This shows the time one second before the rule is applied, the time when the rule is applied, and the time one second after the rule has been applied. It's interesting to note that the "final" 32-bit UNIX time is also displayed. The "isdst" value indicates whether we are in Daylight Savings Time or not, where "isdst=1" means we are.

We can see from the above that we switch between GMT and BST depending on whether Daylight Savings Time is in force or not. This results in the system displaying the following output when run at the relevant times:

 hydrogen# date Fri December  1 09:00:00 GMT 2001 hydrogen# hydrogen# date Thu July  1 09:00:00 BST 2002 hydrogen# 

Zdump can also be used to display the time for a particular time zone. For example, to check the date for EST (Eastern Standard Time), we could run either of the commands below. Comparing the times relative to UTC (GMT) will confirm that the offsets are correct:

 hydrogen# date u; TZ="US/Eastern"; date Thursday April  4 08:27:45 GMT 2002 Thursday April  4 03:27:45 EST 2002 hydrogen# hydrogen# date u; zdump US/Eastern Thursday April  4 08:32:55 GMT 2002 US/Eastern  Thu Apr  4 03:32:55 2002 EST hydrogen# 

Since the time zone setting does not affect the system time, only the displayed value, we could take this a stage further and display a clock for any time zones we were interested in, as shown below, without affecting the rest of the system:

 hydrogen# TZ=GB; xclock & hydrogen# TZ="US/Eastern"; xclock & hydrogen# TZ=CST; xclock & hydrogen# 
Creating Time Zone Files

A time zone information file needs to be created and passed onto zic, the time zone compiler. This file needs to contain a series of entries consisting of rules, zones, or links.

For example, let's say that we need to create a new time zone named "Mars," and that the required time zone obeys the following rules:

  • The time zone will be valid from 1995 onward.

  • It is 12 hours ahead of GMT.

  • It changes to Daylight Savings Time every year, on April 1 at 17:30 GMT, by moving backward two hours.

  • It shifts out of Daylight Savings Time every year on the Sunday after October 22 at 15:00 GMT.

  • The time periods are known as MMT and MST, these being "Mars Mean Time" and "Mars Summer Time," respectively.

Now, we'll create the file that contains the time zone rules; in this case, we have called it mars. We'll store the input file in our data area in case we need it later:

 hydrogen# cd /usr/local/utils/data hydrogen# cat mars Rule MARS 1995 max - Apr 1       17:30 -2:00 MST Rule MARS 1995 max - Oct Sun>=22 15:00 0     MMT Zone planetMars 12:00 MARS MMT/MST hydrogen# 

Next, we can compile the rules using the time zone compiler. This will place a file in /usr/share/lib/zoneinfo named after the zone itself, in this case planetMars:

 hydrogen# zic mars hydrogen# 

To ensure that this is correct, let's first check that we have the correct time offset from GMT, by comparing it with the current UTC time:

 hydrogen# date u; TZ=planetMars; date Thursday April  4 09:00:21 GMT 2002 Thursday April  4 19:00:21 MST 2002 hydrogen# 

Next, if we take a look at the rules, we can see when the switches between MST and MMT occur and check that these are correct:

 hydrogen# zdump -vc 2004 planetMars planetMars  Thu Apr  4 09:30:20 2002 GMT = Thu Apr  4 19:30:20 2002 MST isdst=1 planetMars  Fri Dec 13 20:45:52 1901 GMT = Sat Dec 14 06:45:52 1901 MST isdst=0 planetMars  Sat Dec 14 20:45:52 1901 GMT = Sun Dec 15 06:45:52 1901 MST isdst=0 planetMars  Sat Apr  1 05:29:59 1995 GMT = Sat Apr  1 15:29:59 1995 MST isdst=0 planetMars  Sat Apr  1 05:30:00 1995 GMT = Sat Apr  1 15:30:00 1995 MST isdst=1 planetMars  Sun Oct 22 04:59:59 1995 GMT = Sun Oct 22 14:59:59 1995 MST isdst=1 planetMars  Sun Oct 22 05:00:00 1995 GMT = Sun Oct 22 17:00:00 1995 MMT isdst=0 <lines removed for clarity> planetMars  Tue Apr  1 05:29:59 2003 GMT = Tue Apr  1 17:29:59 2003 MMT isdst=0 planetMars  Tue Apr  1 05:30:00 2003 GMT = Tue Apr  1 15:30:00 2003 MST isdst=1 planetMars  Sun Oct 26 04:59:59 2003 GMT = Sun Oct 26 14:59:59 2003 MST isdst=1 planetMars  Sun Oct 26 05:00:00 2003 GMT = Sun Oct 26 17:00:00 2003 MMT isdst=0 planetMars  Tue Jan 19 03:14:07 2038 GMT = Tue Jan 19 15:14:07 2038 MMT isdst=0 planetMars  Mon Jan 18 03:14:07 2038 GMT = Mon Jan 18 15:14:07 2038 MMT isdst=0 hydrogen# 

At this point, we could implement the new time zone by altering /etc/default/init and inserting the correct (new) time zone:

 hydrogen# grep TZ /etc/default/init TZ=planetMars hydrogen# 

Now that we have established what the rules for this particular system are, we usually need to apply the same rules across all the systems on the local network. Each one will need to have the time zone set individually, but this is not usually a problem because, as noted earlier, it will be performed once, at install time.

This leaves us with the problem of synchronizing the clocks across all the systems. We'll take a look at ways this can be performed.


    Team-Fly    
    Top
     



    Solaris Operating Environment Boot Camp
    Solaris Operating Environment Boot Camp
    ISBN: 0130342874
    EAN: 2147483647
    Year: 2002
    Pages: 301

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net