# Unix时间

**Unix时间**（又称POSIX时间或[纪元](https://www.edgepedia.org/zh/epoch-computing)时间）是一种将时间瞬间表示为自纪元（[1970年1月](https://www.edgepedia.org/zh/january-1970)1日00:00:00 UTC）起经过的秒数之系统，其中不计闰秒。IEEE POSIX标准将此量定义为*近似*经过秒数的一个值，通过固定算术公式与[协调世界时](https://www.edgepedia.org/zh/coordinated-universal-time)名称相关联，并声明以自纪元起的秒数表示时，每一天都恰好由86400秒构成。<sup>[1](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap04.html)</sup>它与许多互联网协议中使用的时间表示形式相同。<sup>[2](https://dev-doc.rust-lang.org/stable/std/time/struct.SystemTime.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 自1970-01-01 00:00:00 UTC起的非闰秒，依据POSIX“自纪元起的秒数”<sup>[3](https://www.ucolick.org/~sla/navyls/0400.html)</sup> |
| 日长规则 | 按定义每天恰好86400秒；闰秒不计入<sup>[1](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap04.html)</sup> |
| 1970年以前的值 | 存在负值（丹尼斯·里奇的出生时间为−893,400,000），但POSIX对1970年之前年份的关系未作定义<sup>[4](https://learn.microsoft.com/en-us/archive/blogs/mikekelly/unix-time-and-windows-time)</sup><sup> • </sup><sup>[1](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap04.html)</sup> |
| 2038年限制 | 有符号32位time_t达到2,147,483,647（2038年1月19日03:14:07 UTC）；在回绕的系统上，下一个值对应1901年12月13日<sup>[5](https://clock.global/concepts/unix-time)</sup><sup> • </sup><sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup> |
| 闰秒 | 自1972年以来已插入27次；最后一次为2016-12-31 23:59:60 UTC<sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup><sup> • </sup><sup>[7](https://bzic-tools.com/blog/unix-timestamp-pitfalls-y2038-milliseconds-timezones)</sup> |
| TAI偏移 | TAI比UTC快37秒；Unix时间跟踪UTC，而非TAI<sup>[8](https://www.nist.gov/pml/time-and-frequency-division/time-realization/leap-seconds)</sup> |
| 精度变体 | 秒（POSIX time()）、毫秒（JavaScript、Java）、纳秒（timespec、Go） |

## 定义与结构

POSIX通过一个由拆分时间字段tm_sec、tm_min、tm_hour、tm_yday和tm_year构成的C语言表达式，将日历日期与自纪元起的秒数关联起来。闰年修正项使用整数除法，从1973年起每4年增加一天，从2001年起每100年减去一天，从2001年起每400年再加回一天。<sup>[1](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap04.html)</sup>100/400闰年规则是在POSIX.1的2001年版中才确立的，此前的版本有一条错误规则。<sup>[3](https://www.ucolick.org/~sla/navyls/0400.html)</sup>

**负值。** 在time_t为有符号整数的系统上，1970年以前的时间可以表示为负计数；Unix共同发明人[丹尼斯·里奇](https://www.edgepedia.org/zh/dennis-ritchie)（Dennis Ritchie）的出生时间即为Unix时间值−893,400,000。<sup>[4](https://learn.microsoft.com/en-us/archive/blogs/mikekelly/unix-time-and-windows-time)</sup>然而标准声明，若年份早于1970年或值为负，则自纪元起的秒数与UTC之间的关系未作定义，因此可移植软件不能依赖对前纪元值的任何特定解释。<sup>[1](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap04.html)</sup>

## 与UTC、TAI及闰秒的关系

UTC以[国际原子时](https://www.edgepedia.org/zh/international-atomic-time)（TAI）为基础，但通过闰秒调整以跟踪[地球自转](https://www.edgepedia.org/zh/earths-rotation)；当前差值为37秒，TAI较快。<sup>[8](https://www.nist.gov/pml/time-and-frequency-division/time-realization/leap-seconds)</sup>自1972年1月1日起，UTC采用SI秒，并以整数秒落后于TAI，通过在6月或12月底（通常如此）插入正闰秒或负闰秒，使其与UT1的偏差保持在0.90秒以内。<sup>[9](https://descanso.jpl.nasa.gov/monograph/series2/Descanso2_S02.pdf)</sup>

Unix时间有意忽略这一机制。为使时间差的计算简单且兼容，POSIX委员会决定闰秒不计入自纪元起的秒数度量。<sup>[5](https://clock.global/concepts/unix-time)</sup>因此，Unix时间戳实际上并不计算自1970年以来经过的物理SI秒；其值因已插入的闰秒而偏少，自1972年以来共插入27次。<sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup>POSIX本身措辞谨慎：该值*近似*经过秒数，而值的变化如何与实际时间对齐由实现定义。<sup>[1](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap04.html)</sup>

**闰秒期间。** UTC时间23:59:60无法在Unix时间中表示。大多数计算机系统重复23:59:59对应的值，因此两个不同的真实秒带有相同的[时间戳](https://www.edgepedia.org/zh/timestamp)。<sup>[10](http://time.kinali.ch/ptti/2000papers/paper34.pdf)</sup>米尔斯（Mills）的NTP参考文档将POSIX惯例描述为在闰秒结束时将时钟回拨一秒，使POSIX时标不连续且存在歧义。<sup>[11](https://www.ntp.org/reflib/leap/)</sup>无论哪种方式，包含闰秒的区间在物理上比减去端点时间戳所暗示的要长，因此从UTC时间戳计算出的速率测量会显示异常值。<sup>[10](http://time.kinali.ch/ptti/2000papers/paper34.pdf)</sup>

## 精度变体与表示形式

time()以整秒报告结果，而clock_gettime()和gettimeofday()分别产生struct timespec（包含秒和纳秒）和struct timeval（包含秒和微秒），从而能够提供更精细的时间读数。<sup>[12](https://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html)</sup>各语言运行时使用的计时单位不同：JavaScript的Date.now()和Java的System.currentTimeMillis()返回自纪元起的毫秒数，而Go的time.UnixNano()返回纳秒数。<sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup>

**计时单位陷阱。** 1700000000秒若被误读为毫秒，解码为1970-01-20，即纪元后二十天；1700000000000毫秒若被误读为秒，解码为公元55840年。<sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup>时间戳的位数取决于所使用的单位。<sup>[7](https://bzic-tools.com/blog/unix-timestamp-pitfalls-y2038-milliseconds-timezones)</sup>

Unix时间是瞬间计数；另一种表示形式是struct tm中的拆分日历时间（年、月、日、时、分、秒），通过mktime和localtime等函数与time_t相互转换。Rust的SystemTime清晰地记录了这一区别：duration_since(UNIX_EPOCH).as_secs()返回自UTC时间1970年初以来的非闰秒数，即许多互联网协议中使用的等效于POSIX time_t的计数，而SystemTime不计闰秒，闰秒附近的行为取决于平台。<sup>[2](https://dev-doc.rust-lang.org/stable/std/time/struct.SystemTime.html)</sup>

## 2038年问题

有符号32位time_t在2038年1月19日03:14:07 UTC整达到最大值2,147,483,647。超过最大值后的递增是否回绕取决于实现；在回绕的系统上，下一个值为−2,147,483,648，对应1901年12月13日20:45:52 UTC。<sup>[5](https://clock.global/concepts/unix-time)</sup>同样的位宽也设定了一个下界：这样的time_t无法表示1901-12-13 20:45:52 UTC之前的任何瞬间。<sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup>无符号32位time_t将上界推至4,294,967,295，即2106-02-07 06:28:15 UTC，代价是失去1970年以前的时间戳。<sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup>

POSIX.1-2017指出，time_t为32位有符号整数的实现在2038年将失效，并声明标准不解决此问题，但强制要求time_t类型以便于最终修复。<sup>[12](https://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html)</sup>64位平台自20世纪90年代末起已使用64位time_t；关键的修复工作是32位ARM和x86用户空间迁移及嵌入式固件。<sup>[13](https://epochkit.dev/blog/year-2038-problem)</sup>截至2020年3月发布的Linux内核5.6，32位系统已通过64位time_t获得明确支持。<sup>[13](https://epochkit.dev/blog/year-2038-problem)</sup>glibc 2.34与内核联合引入了所有时间相关系统调用的Y2038安全替代方案，各发行版在2022至2024年间开始将64位选项设为默认；macOS自Catalina（2019年）起已完全采用64位。<sup>[14](https://unixdates.com/guides/year-2038-problem/)</sup>

## 数字地标

若干数值整齐的时间戳可作为地标。值0即纪元本身；1000000000对应2001-09-09 01:46:40 UTC，1700000000对应2023-11-14 22:13:20 UTC。<sup>[6](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)</sup>2009年2月13日星期五23:31:30 UTC，该值达到连续数字1234567890。<sup>[4](https://learn.microsoft.com/en-us/archive/blogs/mikekelly/unix-time-and-windows-time)</sup>2038年上限为2147483647。<sup>[5](https://clock.global/concepts/unix-time)</sup>

1970年纪元并非首选。现存最早的time系统调用手册页（1971年11月）将time定义为返回自1971年1月1日00:00:00起的时间，以六十分之一秒为单位；60 Hz的节拍率继承自交流电线路频率，而60 Hz下的32位计数器仅持续约2.5年，这在V1手册中已被记录为缺陷。<sup>[5](https://clock.global/concepts/unix-time)</sup>到第三版（1972年3月），纪元已推进至1972年1月1日；到第六版（1973年8月），该调用返回自1970年1月1日00:00:00 GMT起的时间，以秒为单位，此后该编码一直沿用，并在20世纪80年代末被POSIX原样采纳。<sup>[5](https://clock.global/concepts/unix-time)</sup>

## 与其他纪元系统的比较

其他系统选择不同的纪元和单位，因此转换就是一个固定偏移量加上一个比例因子：

- **Windows FILETIME**使用1601-01-01纪元，以100纳秒为刻度；该纪元始于Unix纪元之前11,644,473,600秒，因此转换时将刻度除以10,000,000再减去11,644,473,600。<sup>[15](https://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux)</sup>微软在KB文章Q167297中记录了Unix到FILETIME方向的转换。<sup>[16](https://devblogs.microsoft.com/oldnewthing/20030905-02/?p=42653)</sup>
- **GPS时间**不针对闰秒进行调整，比UTC快自1980年1月6日以来的整数闰秒数，加上或减去少量纳秒；接收机通常自动应用广播偏移量。<sup>[17](https://www.nist.gov/pml/time-and-frequency-division/how-utcnist-related-coordinated-universal-time-utc-international)</sup>GPS秒转换为PTP秒需加上常数315,964,819。<sup>[18](https://www.ucolick.org/~sla/navyls/att-1383/Leap_Second_History.pdf)</sup>
- **NTP**使用1900年1月1日0时作为基准纪元；在1972年1月1日0时UTC的第一个计时节拍，NTP时钟读数为自其纪元以来的2,272,060,800 SI秒。<sup>[11](https://www.ntp.org/reflib/leap/)</sup>PTP秒与NTP秒的关系为PTP = NTP − 2,208,988,800 + (TAI − UTC)。<sup>[18](https://www.ucolick.org/~sla/navyls/att-1383/Leap_Second_History.pdf)</sup>
- **其他**：macOS X（CFDate/NSDate）使用2001-01-01纪元，以小数秒计；旧版MacOS使用1904-01-01，以秒计；Excel使用1899-12-31纪元，以天计；ICU的通用时间尺度以64位刻度计自公元1年1月1日午夜起的时间。<sup>[19](https://unicode-org.github.io/icu/userguide/datetime/universaltimescale.html)</sup>

## 闰秒的实际处理

在6月或12月的最后一天，增加的闰秒紧接在23:59:59之后，编号为23:59:60，而负闰秒则通过跳过23:59:59来移除。<sup>[20](https://www.eecis.udel.edu/%7emills/database/papers/time.pdf)</sup>闰秒使UT1−UTC保持在0.9秒以内，IANA tzdb的leap-seconds.list是tzdb使用者所用的参考数据文件。<sup>[21](https://data.iana.org/time-zones/tzdb-2026e/leap-seconds.list)</sup>

**三种实用策略**并存。NTP在闰秒期间冻结时钟，因此无需在结束时回拨一秒。<sup>[11](https://www.ntp.org/reflib/leap/)</sup>一些符合POSIX的系统则选择回拨时钟。<sup>[11](https://www.ntp.org/reflib/leap/)</sup>大型运营商采用涂抹法：谷歌、AWS、Facebook和Akamai将额外的一秒均匀分散到24小时窗口内（谷歌使用UTC正午到正午），因此每个指示秒为1 + 1/86400 SI秒；时钟比平时略慢，涂抹时钟与步进时钟的偏差最多可达半秒。<sup>[7](https://bzic-tools.com/blog/unix-timestamp-pitfalls-y2038-milliseconds-timezones)</sup><sup> • </sup><sup>[26](https://developers.google.com/time/smear)</sup>第四种方法存在于时区数据库中：阿瑟·戴维·奥尔森（Arthur David Olson）的时间库使用1970-01-01 00:00:10 TAI的纪元，其right/UTC约定可以显示真实的TAI，而多年来Unix的localtime()例程不支持闰秒，实际上将TAI当作UTC处理，每遇一个闰秒便滑移一秒。<sup>[22](https://cr.yp.to/proto/utctai.html)</sup>

## 历史与近期发展

Unix计时起源于1969–70年前后在PDP-7和PDP-9上的最早Unix版本，里奇和汤普森1974年的论文对此有当时的描述。<sup>[23](https://cs.nyu.edu/~mwalfish/classes/15sp/ref/ritchie74.pdf)</sup>该编码从以1971年为纪元、按60 Hz节拍计数到以1970年为纪元、按秒计数的演变，到1973年已完成。<sup>[5](https://clock.global/concepts/unix-time)</sup>

**闰秒政策正在改变。** 第27届CGPM（2022年）第4号决议重申UTC由BIPM产生，速率与TAI相同但相差整数秒，并决定UT1−UTC的最大差值将在2035年或之前增大，为无闰秒的UTC铺平道路。<sup>[24](https://www.bipm.org/en/cgpm-2022/resolution-4)</sup>2025年3月由时间频率咨询委员会和IERS组织的一次研讨会估计，到2035年负闰秒的概率将达到30%。<sup>[25](https://www.bipm.org/documents/d/guest/cgpm-2026-draft-resolution-c-en)</sup>决议草案C提议连续UTC于2027年5月20日生效，结束闰秒插入，最大|UT1−UTC|设为3,600秒（1小时），确保数个世纪的连续性；草案还估计，行业为负闰秒所做的准备所需投资与千年虫准备相当。<sup>[25](https://www.bipm.org/documents/d/guest/cgpm-2026-draft-resolution-c-en)</sup>最近一次插入为2016-12-31 23:59:60 UTC，很可能也是最后一次。<sup>[7](https://bzic-tools.com/blog/unix-timestamp-pitfalls-y2038-milliseconds-timezones)</sup>

**2038年修复**已通过Linux内核5.6对32位系统的64位time_t支持（2020年）、glibc 2.34的Y2038安全系统调用以及2022–2024年发行版默认切换取得进展，剩余的迁移工作是32位ARM和x86用户空间及嵌入式固件。<sup>[13](https://epochkit.dev/blog/year-2038-problem)</sup><sup> • </sup><sup>[14](https://unixdates.com/guides/year-2038-problem/)</sup>

## References

1. [POSIX.1-2017 Base Definitions, Chapter 4 — Seconds Since the Epoch, Open Group](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/basedefs/V1_chap04.html)
2. [SystemTime in std::time, Rust documentation](https://dev-doc.rust-lang.org/stable/std/time/struct.SystemTime.html)
3. [History of IEEE P1003.1 POSIX time, LEAPSECS mailing list (2003)](https://www.ucolick.org/~sla/navyls/0400.html)
4. [Unix Time and Windows Time, Microsoft Learn archive](https://learn.microsoft.com/en-us/archive/blogs/mikekelly/unix-time-and-windows-time)
5. [Unix Time — POSIX Time, Epoch Time & Timestamps, Clock.global](https://clock.global/concepts/unix-time)
6. [Unix timestamps, epochs, and the year 2038 problem, utilrepo](https://utilrepo.com/en/blog/unix-timestamps-and-the-2038-problem/)
7. [Unix Timestamp Pitfalls: Y2038, Milliseconds vs Seconds, and Timezone Hell, bzic-tools](https://bzic-tools.com/blog/unix-timestamp-pitfalls-y2038-milliseconds-timezones)
8. [Leap second and UT1-UTC information, NIST](https://www.nist.gov/pml/time-and-frequency-division/time-realization/leap-seconds)
9. [Coordinated Universal Time (UTC), JPL DESCANSO Monograph](https://descanso.jpl.nasa.gov/monograph/series2/Descanso2_S02.pdf)
10. [Using the Network Time Protocol (NTP) to Transmit International Atomic Time (TAI), PTTI 2000](http://time.kinali.ch/ptti/2000papers/paper34.pdf)
11. [The NTP Timescale and Leap Seconds, NTP Project (Mills)](https://www.ntp.org/reflib/leap/)
12. [POSIX.1-2017 time() — Rationale, Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html)
13. [The year 2038 problem: what breaks on January 19, 2038 and why, epochkit](https://epochkit.dev/blog/year-2038-problem)
14. [The Year 2038 Problem (Y2K38), explained, Unixdates](https://unixdates.com/guides/year-2038-problem/)
15. [Convert Windows Filetime to second in Unix/Linux, Stack Overflow](https://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux)
16. [How to recognize different types of timestamps from quite a long way away, The Old New Thing](https://devblogs.microsoft.com/oldnewthing/20030905-02/?p=42653)
17. [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)
18. [Comparison of Time Scales (Maynard, 2006)](https://www.ucolick.org/~sla/navyls/att-1383/Leap_Second_History.pdf)
19. [Universal Time Scale, ICU Documentation](https://unicode-org.github.io/icu/userguide/datetime/universaltimescale.html)
20. [On the Chronometry and Metrology of Computer Network Timescales (Mills)](https://www.eecis.udel.edu/%7emills/database/papers/time.pdf)
21. [IANA tzdb leap-seconds.list (2026e)](https://data.iana.org/time-zones/tzdb-2026e/leap-seconds.list)
22. [UTC, TAI, and UNIX time, D. J. Bernstein](https://cr.yp.to/proto/utctai.html)
23. [The UNIX Time-Sharing System (Ritchie & Thompson, 1974)](https://cs.nyu.edu/~mwalfish/classes/15sp/ref/ritchie74.pdf)
24. [Resolution 4 of the 27th CGPM (2022), BIPM](https://www.bipm.org/en/cgpm-2022/resolution-4)
25. [Draft Resolution C: continuity of UTC (CGPM 2026), BIPM](https://www.bipm.org/documents/d/guest/cgpm-2026-draft-resolution-c-en)
26. [Leap Smear, Public NTP, Google for Developers](https://developers.google.com/time/smear)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 19, 2026; Sep 30, 2026; 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/unix-time

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