Time

From PacBSD Wiki
Jump to: navigation, search


File:Tango-document-new.png This article is a stub.
Notes: This page only contains basic information on dealing with time in PacBSD. (Discuss)
File:Tango-document-new.png

In an operating system, the time (clock) is determined by four parts: time value, time standard, time zone, and Daylight Saving Time (DST) if applicable. This article explains what they are and how to read/set them.

Hardware clock and system clock

A computer has two clocks that need to be considered: the "Hardware clock" and the "System/software clock".

Hardware clock (a.k.a. the Real Time Clock (RTC) or CMOS clock) stores the values of: Year, Month, Day, Hour, Minute, and the Seconds. It does not have the ability to store the time standard (localtime or UTC), nor whether DST is used.

System clock (a.k.a. the software clock) keeps track of: time, time zone, and DST if applicable. It is calculated by the kernel as the number of seconds since midnight January 1st 1970, UTC. The initial value of the system clock is calculated from the hardware clock. After boot-up has completed, the system clock runs independently of the hardware clock. The kernel keeps track of the system clock by counting timer interrupts.

Standard behavior of most operating systems is:

  • Set the system clock from the hardware clock on boot.
  • Keep accurate time of the system clock with an NTP daemon, see #Time synchronization.
  • Set the hardware clock from the system clock on shutdown.

Read clock

To read the current date and time from the system clock use the date command:

$ date
Tuesday, July 26, 2016 at 08:11:45 AM EDT

Set clock

To set the current date and time the date command again can be used:

# date yymmddhhmmss

The syntax for this is as follows:

  • yy : Year in two digit
  • mm : Month (1-12)
  • dd : Day (1-31)
  • hh : Hours (0..23)
  • mm : Minutes (0..59)
  • ss : Seconds (0..61)

As an example to set the current date and time to be "12-Jan-2004, 4:27 PM" use

# date 0401121627
Mon Jan 12 16:27:00 EST 2004

If setting the time only is desired then the date part of the syntax can be left out:

# date 1627

Time standard

There are two time standards: localtime and Coordinated Universal Time (UTC). The localtime standard is dependent on the current time zone, while UTC is the global time standard and is independent of time zone values. Though conceptually different, UTC is also known as GMT (Greenwich Mean Time).

The standard used by hardware clock (CMOS clock, the time that appears in BIOS) is defined by the operating system. By default, Windows uses localtime, Mac OS uses UTC, and UNIX-like operating systems vary. An OS that uses the UTC standard, generally, will consider CMOS (hardware clock) time a UTC time (GMT, Greenwich time) and make an adjustment to it while setting the System time on boot according to your time zone.

If you have multiple operating systems installed in the same machine, they will all derive the current time from the same hardware clock: for this reason you must make sure that all of them see the hardware clock as providing time in the same chosen standard, or some of them will perform the time zone adjustment for the system clock, while others will not. In particular, it is recommended to set the hardware clock to UTC, in order to avoid conflicts between the installed operating systems. For example, if the hardware clock was set to localtime, more than one operating system may adjust it after a DST change, thus resulting in an overcorrection; more problems may arise when travelling between different time zones and using one of the operating systems to reset the system/hardware clock.

By default PacBSD will assume that the hardware clock is set to UTC, to change this to be localtime create a empty /etc/wall_cmos_clock file.

# touch /etc/wall_cmos_clock

Time zone

To check the current timezone offset for the system:

$ date "+%z"
-0400

This returns the offset from UTC which can be used to identify the current timezone settings.

To get a list of all available timezones

$ ls -l /usr/share/zoneinfo

To set or change the timezone of the system create a symbolic link from /usr/share/zoneinfo/Zone/SubZone to /etc/localtime. Keep in mind that it must be a symbolic link, as specified in pacbsd(7)

# ln -sf /usr/share/zoneinfo/Zone/SubZone /etc/localtime

Time skew

Every clock has a value that differs from real time (the best representation of which being International Atomic Time); no clock is perfect. A quartz-based electronic clock keeps imperfect time, but maintains a consistent inaccuracy. This base 'inaccuracy' is known as 'time skew' or 'time drift'.

The software clock is very accurate but like most clocks is not perfectly accurate and will drift as well. Though rarely, the system clock can lose accuracy if the kernel skips interrupts. There are some tools to improve software clock accuracy.

Time synchronization

The Network Time Protocol (NTP) is a protocol for synchronizing the clocks of computer systems over packet-switched, variable-latency data networks. PacBSD uses ntpd default which implements this protocol.

FreeBSD-init

To configure ntpd open /etc/ntp.conf, although by default it should work out of the box and using a global NTP server pool for time synchronizing.

To enable ntpd so it will start at boot run

# sysrc ntpd_enable="YES"

Then starts it with:

# service ntpd start

OpenRC

Under OpenRC {ic|ntp-client}} is configured via /etc/conf.d/ntp-client instead of /etc/ntp.conf.

To enable ntpd so it will start at boot run

# rc-update add ntp-client default

Then start it with:

# rc-service ntp-client start