# 系统时间

**系统时间**是操作系统维护并向其进程提供的当前日期和时间，其存在是为了让应用程序能够随时获取准确的时间。它以协调世界时（UTC）为基准，在启动时从计算机的实时时钟读取设定，随后在系统运行期间不断更新和校正，最常见的方式是通过网络时间协议（NTP）。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/sysinfo/system-time)</sup>现代内核在这一墙上时钟时间之外还提供若干种不同的时钟，包括测量经过时间且不会跳变的单调时钟，每种时钟服务于不同类别的应用。<sup>[2](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 基准 | Windows 系统时间以 UTC 为基准，启动时从实时时钟初始化，随后定期更新。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/sysinfo/system-time)</sup> |
| Linux 时钟 | CLOCK_REALTIME 是可设置的墙上时钟；CLOCK_MONOTONIC 不可设置，且不受不连续跳变影响；CLOCK_TAI 忽略闰秒；CLOCK_BOOTTIME 包含挂起时间。<sup>[2](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)</sup> |
| 同步 | NTPv4（RFC 5905，2010 年）在现代工作站和快速局域网上将潜在精度提升至数十微秒，时间分辨率优于一纳秒。<sup>[3](https://www.rfc-editor.org/info/rfc5905)</sup> |
| 时间戳格式 | NTP 时间戳是 64 位定点数，以秒为单位，相对于 1900 年 1 月 1 日 0000 UT，其中 32 位为整数部分，32 位为小数部分。<sup>[4](https://www.rfc-editor.org/info/rfc958/)</sup> |
| 闰秒 | 闰秒使 UTC 与天文 UT1 尺度的差值保持在 ±0.9 秒以内；UTC 目前落后 TAI 37 秒，第一个闰秒于 1972 年 6 月 30 日插入。<sup>[5](https://www.nist.gov/pml/time-and-frequency-division/time-realization/leap-seconds)</sup> |
| 2038 年限制 | 有符号 32 位 time_t 在 2038-01-19 03:14:08 UTC 溢出；POSIX.1-2024 要求 time_t 至少为 64 位，但部分系统仍默认使用 32 位。<sup>[6](https://www.man7.org/linux/man-pages/man3/time_t.3type.html)</sup><sup> • </sup><sup>[7](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.16.pdf)</sup> |
| 数据库应用 | Google Spanner 的 TrueTime 时钟返回带有有界不确定度的时间，Spanner 据此为事务分配时间戳以提供外部一致性。<sup>[8](https://docs.cloud.google.com/spanner/docs/true-time-external-consistency?hl=en)</sup> |

## 墙上时钟时间及其设定方式

墙上时钟回答的是“现在几点”这个问题。在 Windows 上，系统以 UTC 为基准，提供 GetSystemTime 将其读入包含月、日、年、星期、时、分、秒和毫秒的结构体，提供 SetSystemTime 来设定时间，并提供 GetSystemTimeAdjustment 和 SetSystemTimeAdjustment，通过在每个时钟中断时施加周期性调整，将日期时间时钟与另一时间源同步。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/sysinfo/system-time)</sup>对于高分辨率 UTC 时间戳，Windows 8 和 Windows Server 2012 引入了 GetSystemTimePreciseAsFileTime，可以低于 1 微秒的精度获取当前日期和时间。<sup>[9](https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime)</sup>

在 POSIX 系统上，同一个墙上时钟就是 CLOCK_REALTIME，它是一个可设置的系统级时钟，测量自纪元（1970-01-01 00:00:00 UTC）以来的秒数和纳秒数。设置它需要适当的权限，且它既受不连续跳变的影响（例如管理员手动更改时钟），也受 adjtime 和 NTP 所执行的增量调整的影响。<sup>[2](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)</sup>C 库的 time() 函数以整秒返回相同的值。<sup>[7](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.16.pdf)</sup>

## 单调时钟与派生时钟

**单调时钟**之所以存在，是因为墙上时钟时间可能发生跳变。CLOCK_MONOTONIC 是一个不可设置的系统级时钟，表示自——用 POSIX 的话说——“过去的某个未指定时间点”以来的单调时间；在 Linux 上，该时间点是自启动以来的秒数。它不受系统时间不连续跳变的影响，但受 adjtime 和 NTP 增量调整的影响，且不计入系统挂起所花费的时间。<sup>[2](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)</sup>CLOCK_MONOTONIC_RAW 在 Linux 2.6.28 中加入，提供基于硬件的原始时间，完全不受 NTP 调整的影响；CLOCK_BOOTTIME（Linux 2.6.39）的行为类似 CLOCK_MONOTONIC，但包含挂起时间。<sup>[2](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)</sup>Apple 的对应物是 mach_absolute_time，一个单调递增的滴答计数器，系统休眠时不会推进。<sup>[10](https://developer.apple.com/documentation/kernel/1462446-mach_absolute_time?changes=l_1_6__8_5&language=objc)</sup>

Linux 还额外提供 CLOCK_TAI，这是一个不可设置的时钟，派生自墙上时钟时间但忽略闰秒，因此不会经历 CLOCK_REALTIME 在 NTP 插入闰秒时所经历的不连续性和回跳。<sup>[2](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)</sup>更快但精度较低的变体 CLOCK_REALTIME_COARSE 和 CLOCK_MONOTONIC_COARSE 服务于那些需要低开销而非细粒度时间戳的调用者。<sup>[2](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)</sup>

在内核内部，时钟源驱动为系统提供基本时间线，时钟事件设备在该时间线上的特定点发出中断以支持高分辨率定时器。当墙上时钟精度不令人满意时，计时代码通过 NTP 将用户可见时间同步到 RTC 时钟或网络时间服务器，但这些措施只是更新相对于时钟源的偏移量。<sup>[11](https://docs.kernel.org/timers/timekeeping.html)</sup>返回 struct timeval 或 struct timespec 的旧内核接口已被逐步淘汰，因为在 32 位架构上 tv_sec 成员会在 2038 年溢出，取而代之的是基于 ktime 的访问器，如 ktime_get() 和 ktime_get_real()。<sup>[12](https://www.kernel.org/doc/html/latest/core-api/timekeeping.html)</sup>

## 同步系统时间

NTP 自 1980 年前后起就开始驯服操作系统时钟，当时第一个实现的精度仅达到数百毫秒。具有完整算法的版本 1 于 1988 年在 RFC 1059 中面世，版本 2 在 RFC 1119 中加入了使用 DES-CBC 的对称密钥认证，版本 3 在 RFC 1305（1992 年）中引入了形式化正确性原则和广播模式。<sup>[13](https://docs.ntpsec.org/latest/history.html)</sup>并行的工作将计时移入内核本身：RFC 1589（1994 年）描述了一种内核模型，可以保持高达一微秒的精度；RFC 2783（1999 年）定义了每秒脉冲 API，用每秒一次的硬件脉冲来校准和稳定系统时钟，现已在所有现代 Unix 系统中实现。<sup>[13](https://docs.ntpsec.org/latest/history.html)</sup>NTPv4 于 2010 年在 RFC 5905 中规定，与版本 3 向后兼容，支持 IPv6，并在现代工作站和快速局域网上将潜在精度扩展至数十微秒；其浮点双精度时间戳定义给出优于一纳秒的时间分辨率和低于每秒一纳秒的频率分辨率。<sup>[3](https://www.rfc-editor.org/info/rfc5905)</sup>

该协议测量时钟偏移、往返延迟、离散度和抖动，并综合来自多个服务器的统计数据来驯服本地时钟；假定的最坏情况频率容差 PHI 为百万分之十五。<sup>[3](https://www.rfc-editor.org/info/rfc5905)</sup>RFC 8633 建议需要精确时间的运营者使用至少四个独立、多样化的时间源，因为一个源会传递其任何错误，两个源在意见不一致时无法裁决，而四个源可以容忍其中一个的丢失。<sup>[14](https://www.rfc-editor.org/info/rfc8633/)</sup>在认证方面，网络时间安全（NTS，RFC 8915，2020 年）使用 TLS 和带关联数据的认证加密来保护 NTP 的客户端-服务器模式，先通过 TLS 建立密钥，随后通过扩展字段进行逐包认证。<sup>[15](https://www.rfc-editor.org/info/rfc8915/)</sup>

**闰秒**是系统时钟面临的最剧烈的不连续性。UTC 基于国际原子时，但通过闰秒进行调整以跟踪地球自转，使 UTC 与 UT1 的差值保持在 ±0.9 秒以内；迄今为止所有闰秒均为正闰秒，UTC 与 TAI 当前的差值为 37 秒。<sup>[5](https://www.nist.gov/pml/time-and-frequency-division/time-realization/leap-seconds)</sup>当插入闰秒时，Linux 内核默认在 00:00:00 UTC 将系统时钟回拨一秒。chrony 的 leapsecmode slew 则通过渐进调整来校正时钟，在 Linux 上以默认的 maxslewrate 需要 12 秒；服务器可以将渐进模式与 smoothtime 指令结合，运行闰秒涂抹，将校正分散到数小时内，使客户端无需特殊配置。<sup>[16](https://chrony-project.org/doc/4.2/chrony.conf.html)</sup>涂抹存在操作风险：2015 年 6 月闰秒前后，一些运营者配置了闰秒涂抹而另一些没有，许多拥有四个源的终端节点无法确定准确时间，因为其中两个给出一致的 UTC 而另外两个给出一致的闰秒涂抹时间。<sup>[14](https://www.rfc-editor.org/info/rfc8633/)</sup>

## 实际精度

NTP 精度随硬件和网络条件差异很大，从每秒脉冲辅助的主服务器上的几微秒到广泛分散的互联网网络上的数十毫秒不等。相比之下，IEEE 1588 精确时间协议（PTP）的目标精度在 100 纳秒量级，主要取决于专用时钟振荡器和计数器的分辨率与稳定性，适用于交换机支持硬件时间戳的高速局域网。<sup>[17](https://www.ntp.org/reflib/ptp/)</sup>

一些系统显式处理不确定性。Google 的 TrueTime 是一种高可用分布式时钟，提供给所有 Google 服务器上的应用，它使所有服务器上的时间戳单调递增；Spanner 为每个事务分配一个时间戳，反映其被认为发生的瞬间，利用这一保证在多版本并发控制下实现跨区域的一致性读取。<sup>[8](https://docs.cloud.google.com/spanner/docs/true-time-external-consistency?hl=en)</sup>

## 2038 年限制

经典的系统时间表示——一个有符号 32 位 time_t，计算自纪元以来的秒数——在 2038-01-19 03:14:08 UTC 溢出，当 32 位可执行文件在该时刻或之后运行于 64 位内核时，会产生 EOVERFLOW 错误。<sup>[7](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.16.pdf)</sup>POSIX.1-2024 要求 time_t 至少为 64 位宽，但一些系统仍默认使用 32 位，在某些架构上可以通过 _TIME_BITS 特性测试宏控制其宽度。<sup>[6](https://www.man7.org/linux/man-pages/man3/time_t.3type.html)</sup>打算在 2038 年之后运行的应用应使用具有更宽 time_t 的 ABI。<sup>[7](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.16.pdf)</sup>

## References

1. [System Time, Win32 apps, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/sysinfo/system-time)
2. [clock_gettime(3), Linux manual page](https://man7.org/linux/man-pages/man3/clock_gettime.3.html)
3. [RFC 5905: Network Time Protocol Version 4, RFC Editor](https://www.rfc-editor.org/info/rfc5905)
4. [RFC 958: Network Time Protocol, RFC Editor](https://www.rfc-editor.org/info/rfc958/)
5. [Leap second and UT1-UTC information, NIST](https://www.nist.gov/pml/time-and-frequency-division/time-realization/leap-seconds)
6. [time_t(3type), Linux manual page](https://www.man7.org/linux/man-pages/man3/time_t.3type.html)
7. [time(2) System Calls Manual, Linux man-pages 6.16](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.16.pdf)
8. [Spanner: TrueTime and external consistency, Google Cloud Documentation](https://docs.cloud.google.com/spanner/docs/true-time-external-consistency?hl=en)
9. [GetSystemTimePreciseAsFileTime function, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime)
10. [mach_absolute_time, Apple Developer Documentation](https://developer.apple.com/documentation/kernel/1462446-mach_absolute_time?changes=l_1_6__8_5&language=objc)
11. [Clock sources, Clock events, sched_clock() and delay timers, Linux Kernel documentation](https://docs.kernel.org/timers/timekeeping.html)
12. [ktime accessors, Linux Kernel documentation](https://www.kernel.org/doc/html/latest/core-api/timekeeping.html)
13. [History of NTP, NTPsec documentation](https://docs.ntpsec.org/latest/history.html)
14. [RFC 8633: Network Time Protocol Best Current Practices, RFC Editor](https://www.rfc-editor.org/info/rfc8633/)
15. [RFC 8915: Network Time Security for the Network Time Protocol, RFC Editor](https://www.rfc-editor.org/info/rfc8915/)
16. [chrony.conf(5), chrony documentation 4.2](https://chrony-project.org/doc/4.2/chrony.conf.html)
17. [IEEE 1588 Precision Time Protocol (PTP), NTP Project](https://www.ntp.org/reflib/ptp/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 测量与时间 › 计时与时间标准 › Time standards, precision, and technical time › Clock synchronization and technical time overview

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/system-time

English original: https://www.edgepedia.org/system-time
