https://wiki.archlinux.org/api.php?action=feedcontributions&user=Azoth&feedformat=atomArchWiki - User contributions [en]2024-03-29T06:03:26ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=System_time&diff=579292System time2019-08-09T16:46:53Z<p>Azoth: Changed all "$" at the start of commands to "#"</p>
<hr />
<div>[[Category:Mainboards and BIOS]]<br />
[[Category:System administration]]<br />
[[es:System time]]<br />
[[fa:زمان]]<br />
[[fr:Horloge]]<br />
[[ja:時刻]]<br />
[[ru:System time]]<br />
[[zh-hans:System time]]<br />
{{Related articles start}}<br />
{{Related|Network Time Protocol daemon}}<br />
{{Related|OpenNTPD}}<br />
{{Related|Chrony}}<br />
{{Related|systemd-timesyncd}}<br />
{{Related articles end}}<br />
{{Expansion|This article mostly documents [[systemd]] ''timedatectl''; explain basic commands like ''date'' and ''hwclock'' first}}<br />
In an operating system, the time (clock) is determined by four parts: time value, whether it is local time or UTC or something else, time zone, and Daylight Saving Time ''(DST)'' if applicable. This article explains what they are and how to read/set them. Two clocks are present on systems: a hardware clock and a system clock which are also detailed in this article.<br />
<br />
Standard behavior of most operating systems is:<br />
<br />
* Set the system clock from the hardware clock on boot.<br />
* Keep accurate time of the system clock, see [[#Time synchronization]].<br />
* Set the hardware clock from the system clock on shutdown.<br />
<br />
== Hardware clock ==<br />
<br />
The '''hardware clock''' (a.k.a. the Real Time Clock (RTC) or CMOS clock) stores the values of: Year, Month, Day, Hour, Minute, and Seconds. Only 2016, or later, [[UEFI]] firmware has the ability to store the timezone, and whether DST is used.<br />
<br />
=== Read hardware clock ===<br />
<br />
# hwclock --show<br />
<br />
=== Set hardware clock from system clock ===<br />
<br />
The following sets the hardware clock from the system clock. Additionally it updates {{ic|/etc/adjtime}} or creates it if not present. See {{man|8|hwclock}} section "The Adjtime File" for more information on this file as well as the [[#Time skew]] section.<br />
<br />
# hwclock --systohc<br />
<br />
== System clock ==<br />
<br />
The '''system clock''' (a.k.a. the software clock) keeps track of: time, time zone, and DST if applicable. It is calculated by the Linux 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, dependent on the contents of {{ic|/etc/adjtime}}. After boot-up has completed, the system clock runs independently of the hardware clock. The Linux kernel keeps track of the system clock by counting timer interrupts.<br />
<br />
=== Read clock ===<br />
<br />
To check the current system clock time (presented both in local time and UTC) as well as the RTC (hardware clock):<br />
<br />
# timedatectl<br />
<br />
=== Set system clock ===<br />
<br />
To set the local time of the system clock directly:<br />
# timedatectl set-time "yyyy-MM-dd hh:mm:ss"<br />
<br />
For example:<br />
# timedatectl set-time "2014-05-26 11:13:54"<br />
sets the time to May 26th, year 2014, 11:13 and 54 seconds.<br />
<br />
== Time standard ==<br />
<br />
There are two time standards: '''localtime''' and [[Wikipedia:Coordinated Universal Time|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).<br />
<br />
The standard used by the hardware clock (CMOS clock, the BIOS time) is set by the operating system. By default, ''Windows'' uses localtime, ''macOS'' uses UTC, and ''UNIX-like'' systems vary. An OS that uses the UTC standard will generally consider the hardware clock as UTC and make an adjustment to it to set the OS time at boot according to the time zone.<br />
<br />
If multiple operating systems are installed on a machine, they will all derive the current time from the same hardware clock: it is recommended to adopt a unique standard for the hardware clock to avoid conflicts across systems and set it to UTC. Otherwise, if the hardware clock is set to ''localtime'', more than one operating system may adjust it after a [[Wikipedia:Daylight_saving_time|DST]] change for example, thus resulting in an over-correction; problems may also arise when traveling between different time zones and using one of the operating systems to reset the system/hardware clock.<br />
<br />
The hardware clock can be queried and set with the {{ic|timedatectl}} command. <br />
You can see the current hardware clock time standard of the Arch system using:<br />
<br />
{{hc|# timedatectl {{!}} grep local|RTC in local TZ: no}}<br />
<br />
To change the hardware clock time standard to localtime, use:<br />
<br />
# timedatectl set-local-rtc '''1'''<br />
<br />
To revert to the hardware clock being in UTC, type:<br />
<br />
# timedatectl set-local-rtc '''0'''<br />
<br />
These generate {{ic|/etc/adjtime}} automatically and update the RTC accordingly; no further configuration is required.<br />
<br />
During kernel startup, at the point when the RTC driver is loaded, the system clock may be set from the hardware clock. Whether this occurs depends on the hardware platform, the version of the kernel and kernel build options. If this does occur, at this point in the boot sequence, the hardware clock time is assumed to be UTC and the value of {{ic|/sys/class/rtc/rtc''N''/hctosys}} (N=0,1,2,..) will be set to 1. <br />
<br />
Later, the system clock is set again from the hardware clock by systemd, dependent on values in {{ic|/etc/adjtime}}. Hence, having the hardware clock using localtime may cause some unexpected behavior during the boot sequence; e.g system time going backwards, which is always a bad idea ([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html there is a lot more to it]). To avoid it systemd [https://mailman.archlinux.org/pipermail/arch-dev-public/2014-August/026577.html will only synchronize back], if the hardware clock is set to UTC and keep the kernel uninformed about the local timezone. As a consequence timestamps on a FAT filesystem touched by the Linux system will be in UTC. <br />
<br />
{{Note|<br />
* The use of {{ic|timedatectl}} requires an active dbus. Therefore, it may not be possible to use this command under a chroot (such as during installation). In these cases, you can revert back to the hwclock command.<br />
* If {{ic|/etc/adjtime}} is not present, [[systemd]] assumes the hardware clock is set to UTC.<br />
}}<br />
<br />
=== UTC in Windows ===<br />
To [[dual boot with Windows]] it is recommended to configure Windows to use UTC, rather than Linux to use localtime. (Windows by default uses localtime: See [http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx this].)<br />
<br />
It can be done by a simple registry fix: Open {{ic|regedit}} and add a {{ic|DWORD}} value for ''32-bit'' Windows, or {{ic|QWORD}} for ''64-bit'' one, with hexadecimal value {{ic|1}} to the registry:<br />
<br />
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\RealTimeIsUniversal<br />
<br />
You can do this from an Administrator Command Prompt running:<br />
<br />
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation" /v RealTimeIsUniversal /d 1 /t REG_DWORD /f<br />
<br />
(Replace {{ic|DWORD}} with {{ic|QWORD}} for 64-bit Windows.)<br />
<br />
Alternatively, create a {{ic|*.reg}} file (on the desktop) with the following content and double-click it to import it into registry:<br />
<br />
Windows Registry Editor Version 5.00<br />
<br />
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation]<br />
"RealTimeIsUniversal"=dword:00000001<br />
<br />
(Replace {{ic|dword}} with {{ic|qword}} for 64-bit Windows.)<br />
<br />
Should Windows ask to update the clock due to DST changes, let it. It will leave the clock in UTC as expected, only correcting the displayed time.<br />
<br />
The [[#Hardware clock]] and [[#System clock]] time may need to be updated after setting this value.<br />
<br />
If you are having issues with the offset of the time, try reinstalling {{Pkg|tzdata}} and then setting your time zone again:<br />
<br />
# timedatectl set-timezone America/Los_Angeles<br />
<br />
==== Historical notes ====<br />
For ''really old'' Windows, the above method fails, due to Windows bugs. More precisely,<br />
<br />
* Before Vista SP2, there is a bug that resets the clock to ''localtime'' after resuming from the suspend/hibernation state.<br />
* For XP and older, there's a bug related to the daylight saving time. See [https://support.microsoft.com/en-us/kb/2687252] for details.<br />
* For ''even older'' versions of Windows, you might want to read http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html - the functionality was not even documented nor officially supported then.<br />
<br />
For these operating systems, it is recommended to use ''localtime''. <br />
<br />
=== UTC in Ubuntu ===<br />
<br />
Ubuntu and its derivatives have the hardware clock set to be interpreted as in "localtime" if Windows was detected on any disk during Ubuntu installation. This is apparently done deliberately to allow new Linux users to try out Ubuntu on their Windows computers without editing the registry.<br />
<br />
For changing this behavior, see above.<br />
<br />
== Time zone ==<br />
<br />
To check the current zone defined for the system:<br />
<br />
# timedatectl status<br />
<br />
To list available zones:<br />
<br />
# timedatectl list-timezones<br />
<br />
To set your time zone:<br />
<br />
# timedatectl set-timezone ''Zone''/''SubZone''<br />
<br />
Example:<br />
<br />
# timedatectl set-timezone Canada/Eastern<br />
<br />
This will create an {{ic|/etc/localtime}} symlink that points to a zoneinfo file under {{ic|/usr/share/zoneinfo/}}. In case you choose to create the link manually (for example during [[chroot]] where {{ic|timedatectl}} won't work), keep in mind that it must be a symbolic link, as specified in {{man|7|archlinux}}:<br />
<br />
# ln -sf /usr/share/zoneinfo/''Zone''/''SubZone'' /etc/localtime<br />
<br />
{{Tip|The time zone can also be selected interactively with ''tzselect''.}}<br />
<br />
See {{man|1|timedatectl}}, {{man|5|localtime}} and {{man|7|archlinux}} for details.<br />
<br />
=== Setting based on geolocation ===<br />
<br />
To set the timezone automatically based on the IP address location, one can use a geolocation API to retrieve the timezone, for example {{ic|curl <nowiki>https://ipapi.co/timezone</nowiki>}}, and pass the output to {{ic|timedatectl set-timezone}} for automatic setting. Some geo-IP APIs that provide free or partly free services are listed below:<br />
<br />
* https://freegeoip.app<br />
* https://ipapi.co/<br />
* http://ip-api.com/<br />
* https://ipstack.com/<br />
* https://timezoneapi.io/<br />
* https://ipdata.co<br />
<br />
==== Update timezone every time NetworkManager connects to a network ====<br />
<br />
Create a [[NetworkManager#Network services with NetworkManager dispatcher|NetworkManager dispatcher script]]:<br />
<br />
{{hc|/etc/NetworkManager/dispatcher.d/09-timezone|<br />
#!/bin/sh<br />
case "$2" in<br />
up)<br />
timedatectl set-timezone "$(curl --fail https://ipapi.co/timezone)"<br />
;;<br />
esac<br />
}}<br />
<br />
Alternatively, the tool {{aur|tzupdate}} automatically sets the timezone based on the geolocation of the IP address. This [https://medium.com/@ipdata_co/what-is-the-best-commercial-ip-geolocation-api-d8195cda7027 comparison of the most popular IP geolocation apis] may be helpful in deciding which API to use in production.<br />
<br />
== Time skew ==<br />
<br />
Every clock has a value that differs from ''real time'' (the best representation of which being [[Wikipedia:International Atomic Time|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'.<br />
<br />
When the hardware clock is set with {{ic|hwclock}}, a new drift value is calculated in seconds per day. The drift value is calculated by using the difference between the new value set and the hardware clock value just before the set, taking into account the value of the previous drift value and the last time the hardware clock was set. The new drift value and the time when the clock was set is written to the file {{ic|/etc/adjtime}} overwriting the previous values. The hardware clock can therefore be adjusted for drift when the command {{ic|hwclock --adjust}} is run; this also occurs on shutdown but only if the {{ic|hwclock}} daemon is enabled, hence for Arch systems which use systemd, this does not happen.<br />
<br />
{{Note|If the hwclock has been set again less than 24 hours after a previous set, the drift is not recalculated as {{ic|hwclock}} considers the elapsed time period too short to accurately calculate the drift.}}<br />
<br />
If the hardware clock keeps losing or gaining time in large increments, it is possible that an invalid drift has been recorded (but only applicable, if the hwclock daemon is running). This can happen if you have set the hardware clock time incorrectly or your [[#Time standard|time standard]] is not synchronized with a Windows or macOS install. The drift value can be removed by first removing the file {{ic|/etc/adjtime}}, then setting the correct hardware clock and system clock time. You should then check if your time standard is correct.<br />
<br />
{{Note|If you wish to make use of the drift value stored in {{ic|/etc/adjtime}} even when using systemd, (e.g. you cannot or do not want to use NTP), you must call {{ic|hwclock --adjust}} on a regular basis, perhaps by creating a [[cron]] job.}}<br />
<br />
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:<br />
<br />
* See [[#Time synchronization]].<br />
<br />
== Time synchronization ==<br />
<br />
The [[Wikipedia:Network Time Protocol|Network Time Protocol]] (NTP) is a protocol for synchronizing the clocks of computer systems over packet-switched, variable-latency data networks. The following are implementations of NTP available for Arch Linux:<br />
<br />
* {{App|[[ConnMan]]|A lightweight network manager with NTP support.|https://01.org/connman|{{Pkg|connman}}}}<br />
* {{App|[[Network Time Protocol daemon]]|The [[Wikipedia:reference implementation|reference implementation]] of the protocol, especially recommended to be used on time servers. It can also adjust the interrupt frequency and the number of ticks per second to decrease system clock drift, and will cause the hardware clock to be re-synchronised every 11 minutes.|http://www.ntp.org/|{{Pkg|ntp}}}}<br />
* {{App|sntp|An [[wikipedia:Network Time Protocol#SNTP|SNTP]] client that comes with NTPd. It supersedes ''ntpdate'' and is recommended in non-server environments.|http://www.ntp.org/|{{Pkg|ntp}}}}<br />
* {{App|[[systemd-timesyncd]]|A simple [[wikipedia:Network Time Protocol#SNTP|SNTP]] daemon that only implements a client side, focusing only on querying time from one remote server. It should be more than appropriate for most installations.|https://www.freedesktop.org/wiki/Software/systemd/|{{Pkg|systemd}}}}<br />
* {{App|[[OpenNTPD]]|Part of the OpenBSD project and implements both a client and a server.|http://www.openntpd.org/|{{Pkg|openntpd}}}}<br />
* {{App|[[Chrony]]|A client and server that is roaming friendly and designed specifically for systems that are not online all the time.|https://chrony.tuxfamily.org/|{{Pkg|chrony}}}}<br />
* {{App|ntpclient|A simple command-line NTP client.|http://doolittle.icarus.com/ntpclient/|{{Aur|ntpclient}}}}<br />
<br />
== Per-user/session or temporary settings ==<br />
<br />
For some use cases it may be useful to change the time settings without touching the global system values. For example to test applications relying on the time during development or adjusting the system time zone when logging into a server remotely from another zone. <br />
<br />
To make an application "see" a different date/time than the system one, you can use the ''faketime'' (from {{Pkg|libfaketime}}) or the {{Pkg|datefudge}}{{Broken package link|{{aur-mirror|datefudge}}}} utilities.<br />
<br />
If instead you want an application to "see" a different time zone than the system one, set the {{ic|TZ}} [[environment variable]], for example: <br />
<br />
{{hc|1=# date && export TZ=":/usr/share/zoneinfo/Pacific/Fiji" && date|2=<br />
Tue Nov 1 14:34:51 CET 2016<br />
Wed Nov 2 01:34:51 FJT 2016<br />
}}<br />
<br />
This is different than just setting the time, as for example it allows to test the behavior of a program with positive or negative UTC offset values, or the effects of DST changes when developing on systems in a non-DST time zone.<br />
<br />
Another use case is having different time zones set for different users of the same system: this can be accomplished by setting the {{ic|TZ}} variable in the shell's configuration file, see [[Environment variables#Defining variables]].<br />
<br />
== Troubleshooting ==<br />
<br />
=== Clock shows a value that is neither UTC nor local time ===<br />
<br />
This might be caused by a number of reasons. For example, if your hardware clock is running on local time, but {{ic|timedatectl}} is set to assume it is in UTC, the result would be that your timezone's offset to UTC effectively gets applied twice, resulting in wrong values for your local time and UTC.<br />
<br />
To force your clock to the correct time, and to also write the correct UTC to your hardware clock, follow these steps:<br />
<br />
* Setup [[ntpd]] (enabling it as a service is not necessary).<br />
* Set your [[#Time zone|time zone]] correctly.<br />
* Run {{ic|ntpd -qg}} to manually synchronize your clock with the network, ignoring large deviations between local UTC and network UTC.<br />
* Run {{ic|hwclock --systohc}} to write the current software UTC time to the hardware clock.<br />
<br />
== Tips and tricks ==<br />
<br />
=== fake-hwclock ===<br />
<br />
[https://github.com/xanmanning/alarm-fake-hwclock alarm-fake-hwclock] designed especially for system without battery backed up RTC, it includes a systemd service which on shutdown saves the current time and on startup restores the saved time, thus avoiding strange time travel errors. <br />
<br />
[[Arch_User_Repository#Installing_packages|Install]] {{AUR|fake-hwclock-git}}, [[Systemd#Using_units|start and enable]] the service {{ic|fake-hwclock.service}}.<br />
<br />
== See also ==<br />
<br />
* [http://sunnyan.tistory.com/entry/Linux-Clocks-and-Time Linux Tips - Linux, Clocks, and Time]<br />
* [https://opensource.com/article/17/6/timekeeping-linux-vms An introduction to timekeeping in Linux VMs]<br />
* [https://data.iana.org/time-zones/tz-link.html Sources for Time Zone and Daylight Saving Time Data] for {{Pkg|tzdata}}<br />
* [https://www.ucolick.org/~sla/leapsecs/timescales.html Time Scales]<br />
* [[Gentoo: System time]]<br />
* [[Wikipedia:Time]]</div>Azoth