# 时间格式化与存储错误

**时间格式化与存储错误**是日期与时间的表示、存储或格式化方式上的软件缺陷：计数器或字段过窄，容纳不下它必须承载的日期；有符号值被重新解释为无符号值；两位年份通过错误的窗口展开；日历规则应用错误；或者通过算术运算构造出一个不存在的日期。它们构成了一类软件缺陷，在这类缺陷中，时间和日期的计算或显示可能被不当处理，在C和C++风格的系统编程及其他领域中皆是如此，其中的溢出难以追踪，可能导致致命错误或被利用的漏洞，尽管并非所有溢出都是缺陷<sup>[1](https://dl.acm.org/doi/10.1145/2743019)</sup><sup> • </sup><sup>[12](https://rohan.padhye.org/files/datetimebugs-msr25.pdf)</sup>。这一类中与时间相关的成员，其区别性特征在于触发条件：它们在某个日历日期触发，往往是在部署多年之后，而且通常产生错误的日期而非崩溃。

| 关键事实 | 详情 |
|---|---|
| 有符号32位Unix时间上限 | 最大值为2,147,483,647秒，于2038年1月19日03:14:07 UTC到达；在计数器回绕的系统上，该值随后变为负数，被读作1901年12月13日晚上8:45<sup>[2](https://cacm.acm.org/opinion/its-about-time/)</sup> |
| 完整的有符号32位范围 | 1901年12月13日20:45:52 UTC（−2,147,483,648）至2038年1月19日03:14:07 UTC，这一限制被称为Y2K38<sup>[3](https://researchportal.hkr.se/ws/portalfiles/portal/63119028/Date_Time_Data_Type_Problem_and_Vision.pdf)</sup> |
| NTP回绕 | NTP 64位时间戳中的32位秒字段，从1900年1月1日起计数，于2036年2月6日回绕为零<sup>[2](https://cacm.acm.org/opinion/its-about-time/)</sup> |
| 64位余量 | 以64位存储秒数的系统可正确运行约2920亿年<sup>[2](https://cacm.acm.org/opinion/its-about-time/)</sup> |
| 文件系统限制 | ext3使用32位时间戳；ext4使用34位，将于2446年耗尽；ZFS、F2FS、NTFS和ReFS在设计上采用64位时间戳<sup>[4](https://unixdates.com/guides/year-2038-problem/)</sup><sup> • </sup><sup>[5](https://www.kaspersky.com/blog/mitigating-y2k38-vulnerability-in-organization/55150/)</sup> |
| Y2K成本 | 全球修复成本估计为3000亿至6000亿美元<sup>[6](https://current.tinyfish.ai/issue/21/echoes/article/11686/the-clock-everyone-can-see)</sup> |
| Debian迁移 | Debian 13（2025年8月9日）在一次ABI变更中将近6500个软件包迁移至64位时间<sup>[7](https://www.networkworld.com/article/4030655/debian-shifts-to-64-bit-time-storage-to-head-off-epochalypse.html)</sup> |

## 系统如何表示时间

在符合POSIX的系统上，`time_t`是一种整数类型，其值表示自纪元——即协调世界时1970年1月1日00:00:00——以来经过的秒数；秒数不计闰秒<sup>[8](https://sourceware.org/pipermail/glibc-cvs/2024q2/085379.html)</sup>。这种设计使时间比较和算术运算变得简单，但它将每一个下游行为都绑定到该整数的宽度和符号性上。有符号32位计数器仅覆盖1901年12月13日至2038年1月19日之间的日期<sup>[3](https://researchportal.hkr.se/ws/portalfiles/portal/63119028/Date_Time_Data_Type_Problem_and_Vision.pdf)</sup>。无符号32位计数器将范围扩展至2106年，但POSIX标准机构得出结论：加宽该类型比将其变为无符号更为可取，现行POSIX要求`time_t`在每种实现的至少一个符合规范的程序设计环境中，是一种宽度至少为64位的整数类型<sup>[9](https://www.austingroupbugs.net/view.php?id=1462)</sup>。

其他系统选择了不同的纪元和单位。NTP时间戳在64位结构的高32位中从1900年1月1日起计数秒数<sup>[2](https://cacm.acm.org/opinion/its-about-time/)</sup>。语言层面的日期类型差异很大：Visual Basic的日期类型保存IEEE 64位值，表示从公元1年1月1日至公元9999年12月31日之间的日期<sup>[10](https://www.mdpi.com/2073-431X/14/11/461)</sup>。glibc文档现在声明，在该库支持的所有平台上，`time_t`均为64位宽，但在少数较旧平台上为32位宽，除非将`_TIME_BITS`定义为64<sup>[8](https://sourceware.org/pipermail/glibc-cvs/2024q2/085379.html)</sup>。

## 失效机制

**回绕与符号。** 在会对有符号32位计数器进行回绕的系统上，于2038年1月19日03:14:07 UTC越过其最大值会使该值变为负数，并可能导致系统将其解释为1901年12月13日晚上8:45<sup>[2](https://cacm.acm.org/opinion/its-about-time/)</sup>。glibc保证`time_t`是有符号的，且其函数对负值（解释为纪元之前的时间）能正确操作<sup>[8](https://sourceware.org/pipermail/glibc-cvs/2024q2/085379.html)</sup>，因此失效不是崩溃，而是一个连贯的、错误的日期：证书在过去过期，日志条目排在其他所有内容之前，计划事件永不触发。

**日历逻辑。** 闰年和闰日错误会在没有任何溢出的情况下产生错误日期。2012年2月29日的Azure故障始于Guest Agent通过取当前日期并给其年份加一来计算证书的到期日期，产生2013年2月29日——一个无效日期，导致证书创建失败<sup>[11](https://azure.microsoft.com/en-us/blog/summary-of-windows-azure-service-disruption-on-feb-29th-2012/)</sup>。

**格式化与构造。** 一项针对开源Python软件中日期和时间缺陷的实证研究发现，时区相关错误是最大的贡献因素，且所研究缺陷中大多数涉及日期/时间值的错误构造<sup>[12](https://rohan.padhye.org/files/datetimebugs-msr25.pdf)</sup>。约60%的所研究缺陷在错误代码以任意输入运行时就会立即显现，而约32%属于中等隐蔽程度，仅在正确输入下才能立即检测到<sup>[12](https://rohan.padhye.org/files/datetimebugs-msr25.pdf)</sup>。一个有记录的Y2K案例研究展示了这一类缺陷如何悄无声息地损坏数据：一个两位年份窗口化缺陷产生了日期191-12-14，在逐年运行到199-12-31之后，脚本跳到了1910-01-01<sup>[13](https://page.mi.fu-berlin.de/prechelt/Biblio/y2kbug_sen1999.pdf)</sup>。日历进位从秒向上传播到分、时、日，这就是为什么年月日顺序是推理时钟相关日期的自然格式<sup>[10](https://www.mdpi.com/2073-431X/14/11/461)</sup>。

## 著名事件

**Y2K（2000年）。** 千年虫来自两位年份字段和窗口化逻辑。上述案例研究展示了其特征性标志：算术运算在产生无意义日期的同时继续运行<sup>[13](https://page.mi.fu-berlin.de/prechelt/Biblio/y2kbug_sen1999.pdf)</sup>。全球修复工作估计耗资3000亿至6000亿美元，涉及一次联合国会议，并催生了美国《2000年信息与准备披露法》<sup>[6](https://current.tinyfish.ai/issue/21/echoes/article/11686/the-clock-everyone-can-see)</sup>。

**PlayStation 3（2010年）。** 2010年3月1日，非Slim版PlayStation 3主机遭遇错误8001050F：系统日期重置为2000年1月1日，PlayStation Network登录失败，奖杯数据出现错误，租赁视频播放被阻止<sup>[14](https://www.gamespot.com/articles/ps3-clock-error-plaguing-pre-2009-consoles-sony-scrambling-to-fix/1100-6252582/)</sup>。索尼确认非Slim版主机的内部时钟将2010年识别为闰年，显示不存在的2010年2月29日，且一旦时钟从2月29日推进到3月1日格林尼治标准时间，症状即消失<sup>[14](https://www.gamespot.com/articles/ps3-clock-error-plaguing-pre-2009-consoles-sony-scrambling-to-fix/1100-6252582/)</sup>。该错误在日期从2月28日变为3月1日时出现<sup>[15](https://www.nytimes.com/2010/03/02/technology/02sony.html)</sup>。

**Windows Azure（2012年）。** 上述闰日证书缺陷因诊断故障而加剧：Fabric Controller错误地假定故障出在硬件而非Guest Agent，且Guest Agent故障在长时间超时后75分钟才被暴露<sup>[11](https://azure.microsoft.com/en-us/blog/summary-of-windows-azure-service-disruption-on-feb-29th-2012/)</sup>。

**Microsoft Exchange（2022年）。** 2022年1月1日，Exchange的FIP-FS反恶意软件扫描引擎失败，错误为“无法将‘2201010001’转换为长整型”，服务器停止投递邮件<sup>[16](https://www.govinfosecurity.com/microsoft-exchange-fixes-disruptive-y2k22-bug-a-18236)</sup>。根本原因是一个有符号int32变量以yymmddHHMM形式存储日期；2022年的日期最小值为2,201,010,001，超过int32最大值2,147,483,647，产生错误1106<sup>[17](https://securityaffairs.com/126248/security/y2k22-bug-temporary-fix.html)</sup>。该缺陷影响Exchange Server 2016和2019<sup>[18](https://arstechnica.com/information-technology/2022/01/exchange-server-bug-gets-a-fix-after-ruining-admins-new-years-plans/)</sup>，微软的临时修复方案是一个PowerShell脚本Reset-ScanEngineVersion.ps1，必须在每台本地部署的Exchange 2016和2019服务器上运行<sup>[17](https://securityaffairs.com/126248/security/y2k22-bug-temporary-fix.html)</sup>。

## 与Y2K的比较

Y2K主要是一个数据格式化问题：文件、数据库和代码中的年份字段可以通过重新编程来发现、加宽和窗口化。2038年问题在性质上不同。许多受影响的嵌入式设备硬件有限，但32位系统无需64位CPU即可支持更大的时间值，例如通过使用64位时间库，因此修复可能受限于遗留软件和数据格式，而非要求许多系统进行物理更换<sup>[19](https://risky.biz/risky-bulletin-the-looming-epochalypse/)</sup><sup> • </sup><sup>[26](https://y2038.com/faq/)</sup>。工厂控制设备、医疗设备、汽车和楼宇管理基础设施都依赖32位固件，而更新从来不是其原始设计的一部分；此类设备在服役数十年后仍在运行，许多根本无法打补丁<sup>[6](https://current.tinyfish.ai/issue/21/echoes/article/11686/the-clock-everyone-can-see)</sup>。修复可能需要系统架构的根本性改变，从32位整数迁移到64位整数，这在较旧硬件和遗留软件上可能既复杂又昂贵<sup>[20](https://www.securityweek.com/the-y2k38-bug-is-a-vulnerability-not-just-a-date-problem-researchers-warn/)</sup>。

2038年边界并非完全在未来。处理2008年之后发放的30年期抵押贷款的金融机构多年来一直在遭遇它<sup>[6](https://current.tinyfish.ai/issue/21/echoes/article/11686/the-clock-everyone-can-see)</sup>。研究人员还警告，攻击者可以通过时间操纵——如GPS欺骗、NTP注入、文件格式字段篡改或协议时间戳操纵——在他们选择的时间提前触发2036年或2038年缺陷<sup>[20](https://www.securityweek.com/the-y2k38-bug-is-a-vulnerability-not-just-a-date-problem-researchers-warn/)</sup>。

## 近期进展

2024至2025年期间出现了主要的迁移浪潮。2025年8月9日发布的Debian 13“Trixie”通过将所有受支持硬件（最旧者除外）的时间存储为64位变量来缓解Y2K38；此次迁移涉及近6500个软件包，且必须一次性完成，因为它改变了应用二进制接口<sup>[7](https://www.networkworld.com/article/4030655/debian-shifts-to-64-bit-time-storage-to-head-off-epochalypse.html)</sup>。Debian在i386系统上将日期保留为32位以保持x86二进制兼容性<sup>[7](https://www.networkworld.com/article/4030655/debian-shifts-to-64-bit-time-storage-to-head-off-epochalypse.html)</sup>。在库方面，glibc提供同时支持32位和64位的双时间配置，其中32位`time_t`无法处理Y2038之后的日期，而64位`__time64_t`可以<sup>[21](https://sourceware.org/glibc/manual/latest/html_node/64_002dbit-time-symbol-handling.html)</sup>，在`time_t`传统上为32位的平台上，调用正确的64位时间系统调用取决于运行版本高于5.1的Linux内核<sup>[22](https://github.com/mmayer/glibc/commit/03caacbc7f3004ad21fc00bf883f00421a211130)</sup>。

文件格式和协议各不相同。ZFS、F2FS、NTFS和ReFS在设计上采用64位时间戳，而ext2和ext3仍然脆弱；ext4需要扩展inode标志，XFS需要bigtime标志，可能还需要离线转换<sup>[5](https://www.kaspersky.com/blog/mitigating-y2k38-vulnerability-in-organization/55150/)</sup>。在数据库中，MySQL的TIMESTAMP类型从根本上限于2038年，需要迁移到DATETIME，而PostgreSQL的标准时间戳类型是安全的<sup>[5](https://www.kaspersky.com/blog/mitigating-y2k38-vulnerability-in-organization/55150/)</sup>。NFSv2和NFSv3协议保留了过时的32位时间存储格式<sup>[5](https://www.kaspersky.com/blog/mitigating-y2k38-vulnerability-in-organization/55150/)</sup>，Debian的发布目标文档指出，即使在大多数64位架构上，utmp和wtmp文件也以32位整数记录时间戳，而InterNetNews（inn2）将`time_t`嵌入其概览和历史数据库的磁盘格式中，当`time_t`的大小改变时需要手动重建<sup>[23](https://wiki.debian.org/ReleaseGoals/64bit-time)</sup>。Debian在2025年表示，溢出距今不到13年，汽车、物联网、电视、路由器和廉价Android手机类别中的许多32位设备仍在出货，且可能运行Debian或衍生版本，并且由于Red Hat、Fedora和SUSE已放弃或仅非官方支持32位，Debian生态系统承担了支持32位系统的很大一部分重担<sup>[7](https://www.networkworld.com/article/4030655/debian-shifts-to-64-bit-time-storage-to-head-off-epochalypse.html)</sup>。

## 检测与缓解

**构建时修复。** 对于32位平台上的C和C++，实际途径是重新编译：Debian软件包通过`DEB_BUILD_OPTIONS`中的`abi=+lfs/future=+lfs`使用`-D_FILE_OFFSET_BITS=64`和`-D_TIME_BITS=64`构建<sup>[23](https://wiki.debian.org/ReleaseGoals/64bit-time)</sup>，glibc将64位类型暴露为`__time64_t`<sup>[21](https://sourceware.org/glibc/manual/latest/html_node/64_002dbit-time-symbol-handling.html)</sup>。Java、Python和Go通常使用可避免溢出的类型<sup>[5](https://www.kaspersky.com/blog/mitigating-y2k38-vulnerability-in-organization/55150/)</sup>。

**针对无法打补丁系统的纪元偏移。** 一项针对32位UNIX嵌入式操作系统的案例研究比较了四种修复方案：将`time_t`加宽至64位、将有符号改为无符号、将进位解释为2038年之后的时间、以及偏移纪元。所采用的方案将系统时间原点后移28年，至1998年，在不改变操作系统的情况下推迟溢出，并在设置为2038年之后的测试环境中得到验证<sup>[24](https://sel.ist.osaka-u.ac.jp/lab-db/betuzuri/archive/1183/1183.pdf)</sup>。

**测试。** 在2038年截止点之后，仍使用32位时间值的32位程序在处理超出其可表示范围的日期时可能失败，因此Gentoo开发者Michal Gorny的文章建议使用faketime测试以控制系统时钟，或运行一台时间被调回过去的完整虚拟机<sup>[25](https://blogs.gentoo.org/mgorny/2024/09/28/the-perils-of-transition-to-64-bit-time_t/)</sup>。Python缺陷研究建议使用Freezegun等库和libfaketime等操作系统级工具来模拟系统时钟，使用Hypothesis进行基于属性的测试以验证往返解析/序列化不变量，以及进行模糊测试以覆盖1900年之前的年份和分数微秒等边缘情况<sup>[12](https://rohan.padhye.org/files/datetimebugs-msr25.pdf)</sup>。微软针对2012年Azure故障的事后修复包括改进时间相关缺陷的测试、增强代码分析工具以及快速失败错误分类<sup>[11](https://azure.microsoft.com/en-us/blog/summary-of-windows-azure-service-disruption-on-feb-29th-2012/)</sup>。供应商针对2038年测试的指南增加了操作保障措施：切勿测试生产系统，在测试前立即创建数据备份，隔离被测系统，并阻止2038年测试信号通过NTP或GPS到达其他系统<sup>[5](https://www.kaspersky.com/blog/mitigating-y2k38-vulnerability-in-organization/55150/)</sup>。

## References

1. [Understanding Integer Overflow in C/C++ (Dietz et al., ACM TOSEM)](https://dl.acm.org/doi/10.1145/2743019)
2. [It's About Time, Communications of the ACM](https://cacm.acm.org/opinion/its-about-time/)
3. [Date and Time Data Type Problem and Vision (Kristiansen et al.)](https://researchportal.hkr.se/ws/portalfiles/portal/63119028/Date_Time_Data_Type_Problem_and_Vision.pdf)
4. [The Year 2038 Problem (Y2K38), explained, Unixdates](https://unixdates.com/guides/year-2038-problem/)
5. [What is the 'year 2038 problem', and how can businesses fix it? (Kaspersky)](https://www.kaspersky.com/blog/mitigating-y2k38-vulnerability-in-organization/55150/)
6. [The Clock Everyone Can See, Current](https://current.tinyfish.ai/issue/21/echoes/article/11686/the-clock-everyone-can-see)
7. [Debian shifts to 64-bit time storage to head off Epochalypse, Network World](https://www.networkworld.com/article/4030655/debian-shifts-to-64-bit-time-storage-to-head-off-epochalypse.html)
8. [glibc: Improve doc for time_t range (BZ 31808)](https://sourceware.org/pipermail/glibc-cvs/2024q2/085379.html)
9. [Austin Group Issue 1462: Require (at least) 64-bit time_t](https://www.austingroupbugs.net/view.php?id=1462)
10. [Approaching Challenges in Representations of Date–Time Ambiguities, MDPI Computers](https://www.mdpi.com/2073-431X/14/11/461)
11. [Summary of Windows Azure Service Disruption on Feb 29th, 2012, Microsoft](https://azure.microsoft.com/en-us/blog/summary-of-windows-azure-service-disruption-on-feb-29th-2012/)
12. [It's About Time: An Empirical Study of Date and Time Bugs in Open-Source Python Software (MSR 2025)](https://rohan.padhye.org/files/datetimebugs-msr25.pdf)
13. [The Surprising Dynamics of a Simple Year 2000 Bug (Prechelt)](https://page.mi.fu-berlin.de/prechelt/Biblio/y2kbug_sen1999.pdf)
14. [PS3 clock error plaguing pre-2009 consoles, GameSpot](https://www.gamespot.com/articles/ps3-clock-error-plaguing-pre-2009-consoles-sony-scrambling-to-fix/1100-6252582/)
15. [Error Blocked PlayStation 3 From Using Its Network, The New York Times](https://www.nytimes.com/2010/03/02/technology/02sony.html)
16. [Microsoft Exchange Fixes Disruptive 'Y2K22' Bug, GovInfoSecurity](https://www.govinfosecurity.com/microsoft-exchange-fixes-disruptive-y2k22-bug-a-18236)
17. [Microsoft rolled out emergency fix for Y2k22 bug in Exchange servers, Security Affairs](https://securityaffairs.com/126248/security/y2k22-bug-temporary-fix.html)
18. [Microsoft fixes harebrained Y2K22 Exchange bug, Ars Technica](https://arstechnica.com/information-technology/2022/01/exchange-server-bug-gets-a-fix-after-ruining-admins-new-years-plans/)
19. [Risky Bulletin: The looming epochalypse](https://risky.biz/risky-bulletin-the-looming-epochalypse/)
20. [The Y2K38 Bug Is a Vulnerability, Not Just a Date Problem, SecurityWeek](https://www.securityweek.com/the-y2k38-bug-is-a-vulnerability-not-just-a-date-problem-researchers-warn/)
21. [64-bit time symbol handling, The GNU C Library manual](https://sourceware.org/glibc/manual/latest/html_node/64_002dbit-time-symbol-handling.html)
22. [glibc commit: doc, _TIME_BITS defaults may change](https://github.com/mmayer/glibc/commit/03caacbc7f3004ad21fc00bf883f00421a211130)
23. [ReleaseGoals/64bit-time, Debian Wiki](https://wiki.debian.org/ReleaseGoals/64bit-time)
24. [A case study on remediation of the Year 2038 problem in a 32-bit embedded system, Osaka University](https://sel.ist.osaka-u.ac.jp/lab-db/betuzuri/archive/1183/1183.pdf)
25. [The perils of transition to 64-bit time_t, Michal Górny (Gentoo)](https://blogs.gentoo.org/mgorny/2024/09/28/the-perils-of-transition-to-64-bit-time_t/)
26. [FAQ - Y2038](https://y2038.com/faq/)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic › Integer overflow, underflow, and wraparound

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/time-formatting-and-storage-bugs

English original: https://www.edgepedia.org/time-formatting-and-storage-bugs
