# 时间戳

时间戳是对某个时间点无歧义的表示，依据某种明示的时间尺度记录，且在多数格式中还标明相对于该尺度的明示偏移量。<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup>在计算领域，时间戳将时间线上的确定点附着于各类事件，如日志条目、数据库事务、文件修改和网络消息。时间戳的意义取决于其时间基准：同一串数字，参照UTC、参照[1970年1月](https://www.edgepedia.org/zh/january-1970)1日这样的纪元、还是参照未同步的本地时钟，所指的时刻各不相同。

| 关键事实 | 详情 |
|---|---|
| 互联网标准格式 | RFC 3339（2002年7月）定义了互联网日期/时间格式，即ISO 8601扩展格式的合规子集，多数字段和标点均为强制<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup> |
| 示例 | `1985-04-12T23:20:50.52Z` 表示1985年4月12日23时之后的20分50.52秒，UTC<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup> |
| 闰秒 | RFC 3339允许在闰秒插入时秒值为60，如 `1990-12-31T23:59:60Z`；POSIX时间则忽略闰秒<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup><sup> • </sup><sup>[2](https://pubs.opengroup.org/onlinepubs/007904975/xrat/xbd_chap04.html)</sup> |
| 常用纪元 | Unix和Java从1970-01-01T00:00:00Z起计秒；NTP从1900年1月1日0时UTC起计<sup>[3](https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/time/Instant.html)</sup><sup> • </sup><sup>[4](https://www.rfc-editor.org/info/rfc5905/)</sup> |
| 分发 | NIST的互联网时间服务每秒应答约100万次请求，时间可溯源至UTC(NIST)<sup>[5](https://www.nist.gov/atomic-clocks/how-we-tell-time/how-does-atomic-time-get-your-computer)</sup> |
| 扩展 | RFC 9557（2024年4月）定义了IXDTF，一种可选后缀语法，用于在RFC 3339时间戳上附加时区名及其他信息<sup>[6](https://datatracker.ietf.org/doc/html/rfc9557)</sup> |
| 分布式数据库 | Google Spanner从TrueTime获取提交时间戳，TrueTime暴露时钟不确定区间，Spanner等待该区间过去，以在全球范围内一致地排列事务<sup>[7](https://cloud.google.com/blog/products/databases/strict-serializability-and-external-consistency-in-spanner)</sup> |

## 时间基准：UTC及其实现

互联网上的时间戳参照协调世界时（UTC），即国际公认的世界时间标准。UTC建立在[国际原子时](https://www.edgepedia.org/zh/international-atomic-time)（TAI）之上，TAI是60多个计时实验室中300多台原子钟的加权平均，由巴黎附近的[国际计量局](https://www.edgepedia.org/zh/international-bureau-of-weights-and-measures)（BIPM）计算；最稳定的钟获得最大权重，任何单台钟的权重上限为A/N，其中A = 2.5，N为钟的数量。<sup>[8](https://www.nist.gov/pml/time-and-frequency-division/how-utcnist-related-coordinated-universal-time-utc-international)</sup>UTC与TAI相差整数个闰秒，闰秒的插入是为了使UTC与UT1（与地球自转相连的天文时间尺度）的偏差保持在小于1秒的范围内。<sup>[8](https://www.nist.gov/pml/time-and-frequency-division/how-utcnist-related-coordinated-universal-time-utc-international)</sup>

没有任何时钟保持UTC的官方版本，因为TAI和UTC是“纸面”时间尺度，只有在所有贡献数据到齐后才计算得出。各国实验室转而维持实时实现，以括号中的实验室名命名：美国的UTC(NIST)由铯束钟和氢微波激射钟组成的钟组产生，时间尺度中通常约有十台，经校准而在时间和频率上与UTC一致。<sup>[8](https://www.nist.gov/pml/time-and-frequency-division/how-utcnist-related-coordinated-universal-time-utc-international)</sup><sup> • </sup><sup>[9](https://www.nist.gov/pml/time-and-frequency-division/time-realization/utcnist-time-scale/how-utcnist-works)</sup>UTC(NIST)与UTC(USNO)——[美国海军天文台](https://www.edgepedia.org/zh/united-states-naval-observatory)为国防部提供服务的实现——通常保持在20纳秒以内的一致性，二者均为美国官方时间源。<sup>[8](https://www.nist.gov/pml/time-and-frequency-division/how-utcnist-related-coordinated-universal-time-utc-international)</sup>

## 文本格式：RFC 3339及其扩展

当今多数互联网协议使用的格式定义于RFC 3339，2002年7月作为[ISO 8601](https://www.edgepedia.org/zh/iso-8601)的一种受限规范发布。<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup>时间戳的形式为 `date-time = full-date "T" full-time`，包含四位年份、两位月份和日期、24小时制时间、可选的小数秒，以及强制的[UTC偏移量](https://www.edgepedia.org/zh/utc-offset)，或为 `Z`（读作“祖鲁”，源自国际民航组织音标字母表，表示偏移00:00），或为 `-08:00` 这样的数字偏移。<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup>`T` 和 `Z` 字符可写作小写，但生成器应使用大写。<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup>

这一设计有两个有意为之的特性：第一，当所有时间戳共享相同的偏移表示和相同的小数秒位数时，字符串按普通字符串排序即得时间顺序，例如用C的 `strcmp()`；可选的标点会破坏这一点。<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup>第二，拒绝未注明偏移量的本地时间：因为未注明偏移量的本地时间在约24个时区中的23个会被误读，依赖同步的本地时间系统必须使用网络时间协议等机制获取UTC。<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup>

**偏移量与区域。** UTC偏移量确定时刻，但不提供民用语境。需要处理夏令时转换的应用会附加时区名，历史上采用非标准后缀。RFC 9557于2024年4月发布，定义了互联网扩展日期/时间格式（IXDTF）：在RFC 3339时间戳之后的可选后缀，可携带时区标识符及其他键值信息，与现有RFC 3339字符串及原有的Java风格后缀语法兼容。<sup>[6](https://datatracker.ietf.org/doc/html/rfc9557)</sup>RFC 9557还更新了对 `Z` 的解释，它不再意味着UTC是指定时间的首选参考点。<sup>[6](https://datatracker.ietf.org/doc/html/rfc9557)</sup>时区规则本身（包括历史和未来的转换）以[时区](https://www.edgepedia.org/zh/time-zone)信息格式（TZif）交换，其版本1的32位转换时间覆盖1901-12-13 20:45:52至2038-01-19 03:14:07 UT，版本2和3文件则加入64位值。<sup>[10](https://www.rfc-editor.org/info/rfc8536/)</sup>

## 计数格式：纪元与二进制时间戳

许多系统将时间戳存储为从固定纪元起计的秒数或更细单位的计数，而非格式化文本。[Unix时间](https://www.edgepedia.org/zh/unix-time)和Java的 `Instant` 类从1970-01-01T00:00:00Z起计；Java的 `Instant` 存储64位纪元秒计数和32位秒内纳秒字段，后者始终在0到999,999,999之间。<sup>[3](https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/time/Instant.html)</sup>NTP使用不同的主纪元，即1900年1月1日0时UTC，此时其时间戳的所有位均为零。<sup>[4](https://www.rfc-editor.org/info/rfc5905/)</sup>[GPS时间](https://www.edgepedia.org/zh/gps-time)不应用闰秒，因此比UTC超前的秒数等于自1980年1月6日以来插入的闰秒数；接收机应用GPS广播消息中携带的偏移量。<sup>[8](https://www.nist.gov/pml/time-and-frequency-division/how-utcnist-related-coordinated-universal-time-utc-international)</sup>

闰秒在这些计数之间造成已知的分歧。POSIX时间——Unix标准的“纪元起秒数”惯例——完全忽略闰秒以便于计算时间差；截至2000年9月，自1970年纪元以来UTC已加入24个闰秒，因此分解后的POSIX时间尽管外表相似，并不一定是UTC。<sup>[2](https://pubs.opengroup.org/onlinepubs/007904975/xrat/xbd_chap04.html)</sup>RFC 3339采取相反做法，允许闰秒插入时秒值为60，如 `1990-12-31T23:59:60Z`，或同一时刻在太平洋标准时间下写作 `1990-12-31T15:59:60-08:00`。<sup>[1](https://www.rfc-editor.org/info/rfc3339/)</sup>

## 分发时间戳

时间戳只有在产生它的时钟已同步时才可信。[网络时间协议](https://www.edgepedia.org/zh/network-time-protocol)第4版规定于RFC 5905（2010年6月），在分布式时间服务器与客户端之间同步系统时钟，每次交换携带起始、接收和发送时间戳，并根据最佳偏移估计校准本地时钟。<sup>[4](https://www.rfc-editor.org/info/rfc5905/)</sup>NIST运行多台直接链接至UTC(NIST)的1级网络时间服务器；其NTP响应携带自1900年1月1日起UTC秒的64位时间戳，分辨率为200皮秒。<sup>[11](https://www.nist.gov/pml/time-and-frequency-division/time-distribution/internet-time-service-its)</sup>该服务使用量极大：NIST报告每秒约100万次命中，超过Google。<sup>[5](https://www.nist.gov/atomic-clocks/how-we-tell-time/how-does-atomic-time-get-your-computer)</sup>

## 分布式数据库中的时间戳

在时钟独立的机器间排列事件，是与格式化不同的问题。Google的Spanner是一个全球分布式数据库，从TrueTime为每个事务分配提交时间戳；TrueTime是一个分布式时钟API，返回约束真实当前时间的不确定区间 `[最早, 最晚]`；在Google作出这一描述时，第99百分位的不确定度低于1毫秒。<sup>[7](https://cloud.google.com/blog/products/databases/strict-serializability-and-external-consistency-in-spanner)</sup>由于事务的时间戳所指的时刻可能尚未真正到来，提交会等待不确定区间（通常几毫秒）过去后再完成。<sup>[12](https://docs.cloud.google.com/spanner/docs/whitepapers/life-of-reads-and-writes)</sup>结果是外部一致性：若事务Txn1在实际时间上先于Txn2提交，则Txn1的提交时间戳更早，因此任何观察到Txn2效果的读取也必观察到Txn1的。<sup>[13](https://cloud.google.com/spanner/docs/transactions?authuser=00)</sup>TrueTime的保证强于单机单调性：在另一个时间戳T'生成完毕后计算出的时间戳T，保证在所有服务器上都大于T'。<sup>[14](https://docs.cloud.google.com/spanner/docs/true-time-external-consistency)</sup>

## References

1. [RFC 3339: Date and Time on the Internet: Timestamps, RFC Editor](https://www.rfc-editor.org/info/rfc3339/)
2. [The Open Group Base Specifications Issue 6, Rationale, A.4.14 Seconds Since the Epoch](https://pubs.opengroup.org/onlinepubs/007904975/xrat/xbd_chap04.html)
3. [Class Instant, Java SE API Documentation, Oracle](https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/time/Instant.html)
4. [RFC 5905: Network Time Protocol Version 4: Protocol and Algorithms Specification, RFC Editor](https://www.rfc-editor.org/info/rfc5905/)
5. [How Does Atomic Time Get to Your Computer?, NIST](https://www.nist.gov/atomic-clocks/how-we-tell-time/how-does-atomic-time-get-your-computer)
6. [RFC 9557: Date and Time on the Internet: Timestamps with Additional Information, IETF](https://datatracker.ietf.org/doc/html/rfc9557)
7. [Strict Serializability and External Consistency in Spanner, Google Cloud Blog](https://cloud.google.com/blog/products/databases/strict-serializability-and-external-consistency-in-spanner)
8. [How is UTC(NIST) related to UTC, TAI, GMT, USNO time, GPS time and Zulu time?, NIST](https://www.nist.gov/pml/time-and-frequency-division/how-utcnist-related-coordinated-universal-time-utc-international)
9. [How UTC(NIST) Works, NIST Time and Frequency Division](https://www.nist.gov/pml/time-and-frequency-division/time-realization/utcnist-time-scale/how-utcnist-works)
10. [RFC 8536: The Time Zone Information Format (TZif), RFC Editor](https://www.rfc-editor.org/info/rfc8536/)
11. [NIST Internet Time Service (ITS), NIST](https://www.nist.gov/pml/time-and-frequency-division/time-distribution/internet-time-service-its)
12. [Life of Spanner Reads & Writes, Google Cloud Documentation](https://docs.cloud.google.com/spanner/docs/whitepapers/life-of-reads-and-writes)
13. [Transactions overview, Google Cloud Spanner Documentation](https://cloud.google.com/spanner/docs/transactions?authuser=00)
14. [Spanner: TrueTime and external consistency, Google Cloud Documentation](https://docs.cloud.google.com/spanner/docs/true-time-external-consistency)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization › Text, encodings, and archives

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/timestamp

English original: https://www.edgepedia.org/timestamp
