Time

From ArchWiki

Jump to: navigation, search
Summary
This article provides an introduction to the concept of keeping time on computers in general, and describes how clocks are configured and managed in Arch Linux.
Related
Network Time Protocol
rc.conf

This article explains how to read and set the hardware clock (a.k.a. the Real Time Clock (RTC) or CMOS clock) and the system clock. For maintaining accurate system time, please see Network Time Protocol.

Contents

About

The hardware clock keeps values of year, month, day, hour, minute, and seconds. The hardware clock time value may either be in the localtime standard or the Coordinated Universal Time (UTC) standard. Localtime is dependent on your local time zone while UTC (aka Greenwich Mean Time) is global time and independent of time zone values. The hardware clock can only store time values and does not store information on whether localtime or UTC time is used, nor whether daylight savings time is used. Common operating systems like Windows and Mac OS will set the hardware clock dependent on your time zone (localtime), while UNIX-like operating systems can be told to use either value.

Operating systems also have a software clock (aka the system clock) that runs independent of the hardware clock. The system clock keeps track of the time, time zone, and whether or not your location uses Daylight Savings Time (DST). The Arch Linux script (/etc/rc.sysinit) sets the system clock from the hardware clock on boot, and the script (/etc/rc.shutdown) sets the hardware clock from the system clock on shutdown. Users often use NTP to keep the system clock set to the proper time.

Time Standard

You can set the time standard through the command line. You can check what you have set your Arch Linux install to use by:

$ grep ^HARDWARECLOCK /etc/rc.conf

To immediately change the hardware clock time standard, you can set localtime by (if you use Windows or Mac OS you will want to use this):

# hwclock --localtime

And to set it as UTC by:

# hwclock --utc

The time standard will also need to be entered in your Arch Linux system configuration (rc.conf) so that it will be set the next time you restart:

HARDWARECLOCK="localtime"

or

HARDWARECLOCK="UTC"
Note: GNU/Linux will change to-and-from DST when the HARDWARECLOCK setting is set to UTC, regardless of whether GNU/Linux was running at the time DST is entered or left. When the HARDWARECLOCK setting is set to localtime, GNU/Linux will not adjust the time, operating under the assumption that you dual-boot and that the other OS takes care of the DST switch. If this is not the case, the DST change needs to be made manually.

Your hardware clock and system clock time may need to be updated after this, read #Time Set.

Time Zone

Be sure that your time zone is set correctly in /etc/rc.conf, this not only is necessary for the localtime to be set correctly but also for other programs you may use. You can do this by:

$ grep ^TIMEZONE /etc/rc.conf

You can find the time zones listed in /usr/share/zoneinfo/ and then use a nearby city that corresponds to your time zone. For example:

TIMEZONE="America/Chicago"

The new time zone will be taken into effect when you set the hardware clock, see the next step.

Time Set

The hardware clock can either be set directly or from the system clock. To check the current hardware clock time and system clock time respectively:

$ hwclock --show
$ date

To set the hardware clock directly (in military time):

# hwclock --set --date "MM/DD/YYYY hh:mm:ss"

To set the system clock:

# date MMDDhhmmYYYY

The hardware clock can be set from the system clock and vice versa:

# hwclock --systohc
# hwclock --hctosys

Time Skew

No clock is perfect. Every clock has a value that differs from 'real time' (the best representation of which being International Atomic Time). A quartz based eletronic clock keeps imperfect time, but maintains a very consistent inaccuracy. This base 'inaccuracy' is known as 'time skew' or 'time drift'. Each time the clock is set with hwclock (e.g. during shutdown), hwclock calculates the difference between the new value and previous values and calculates a mean difference. This difference is recorded in the file /var/lib/hwclock/adjtime. An Arch Linux cron job script adjusts the hardware clock according to the recorded drift every hour. If you are seeing large changes to your hardware clock, it is likely because a large drift is recorded in /var/lib/hwclock/adjtime. This can happen if you have set the hardware clock time incorrectly, or your time standard is not syncronized with a Windows or Mac OS install. To fix this remove /var/lib/hwclock/adjtime, set the correct hardware clock and system clock time, and check if your time standard is correct.

The system clock is calculated and stored by the Linux kernel as the number of seconds since midnight January 1st 1970 UTC. The Linux kernel keeps track of the system clock by counting timer interupts. The software clock is very accurate but like most clocks isn't perfectly accurate and will drift as well. Though rare, the system clock can loose accuracy if the kernel skips interupts. System clocks can be kept on accurate time by use of NTP.

Resources

Personal tools