如何将纪元之前的日期表示为 UNIX 时间戳 [英] How to represent dates before epoch as a UNIX timestamp

查看:45
本文介绍了如何将纪元之前的日期表示为 UNIX 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我突然想到我不知道有一种机制可以存储 1970 年 1 月之前的日期.1 作为 Unix 时间戳.由于那个日期是 Unix纪元",所以这并不奇怪.

It occurred to me that I'm not aware of a mechanism to store dates before 1970 jan. 1 as Unix timestamps. Since that date is the Unix "epoch" this isn't much of a surprise.

但是 - 即使它不是为此而设计的 - 我仍然希望以 Unix 格式存储过去的日期.
我需要这个是有原因的.

But - even though it's not designed for that - I still wish to store dates in the far past in Unix format.
I need this for reasons.

所以我的问题是:如何让 unix 时间戳包含无效"但仍然有效的日期?存储负的秒数会起作用吗?我们甚至可以在unix时间戳中存储秒数吗?我的意思是它不是未签名吗?

So my question is: how would one go about making unix-timestamps contain "invalid" but still working dates? Would storing a negative amount of seconds work? Can we even store negative amounts of seconds in a unix-timestamp? I mean isn't it unsigned?

另外,如果我是对的,那么我只能存储最早到 1901 的日期.十二月13 20:45:52 是否可以通过任何方式将其追溯到更远的历史?

Also if I'm correct then I could only store dates as far back as 1901. dec. 13 20:45:52 could this be extended any further back in history by any means?

推荐答案

Unix Time通常 UTC 时间从 1970 年的第一个时刻开始的 32 位整秒数,纪元1 January 1970 00:00:00 UTC.这意味着一个大约 136 年的范围,其中大约一半在时代的两侧.负数较早,零为纪元,正数较晚.对于有符号的 32 位整数,值范围从 1901-12-132038-01-19 03:14:07 UTC.

Unix Time is usually a 32-bit number of whole seconds from the first moment of 1970 in UTC, the epoch being 1 January 1970 00:00:00 UTC. That means a range of about 136 years with about half on either side of the epoch. Negative numbers are earlier, zero is the epoch, and positive are later. For a signed 32-bit integer, the values range from 1901-12-13 to 2038-01-19 03:14:07 UTC.

这不是一成不变的.嗯,它写的,但是是用一堆不同的石头写的.旧的说 32 位,新的说 64 位.一些规范说这个意思是实现定义的".一些 Unix 系统使用 unsigned int 只扩展到 epoch 之后的未来,但通常的做法是使用有符号数.有些使用浮点数而不是整数.有关详细信息,请参阅关于 Unix 时间这个问题.

This is not written in stone. Well, it is written, but in a bunch of different stones. Older ones say 32-bit, newer ones 64-bit. Some specifications says that the meaning is "implementation-defined". Some Unix systems use an unsigned int to extend only into the future past the epoch, but usual practice has been a signed number. Some use a float rather than an integer. For details, see Wikipedia article on Unix Time, and this Question.

所以,基本上,您的问题毫无意义.您必须了解您的编程语言(标准 C、其他 C、Java 等)、环境(符合 POSIX 标准)、特定软件库、数据库存储或应用程序的上下文.

So, basically, your Question makes no sense. You have to know the context of your programming language (standard C, other C, Java, etc.), environment (POSIX-compliant), particular software library, or database store, or application.

其他几十个时代 已被各种软件系统使用,其中一些非常流行和常见.示例包括 NTFS 文件系统的 1601 年 1 月 1 日和COBOL,1980 年 1 月 1 日用于各种 FAT 文件系统,2001 年 1 月 1 日用于 Apple Cocoa,1900 年 1 月 0 日用于 Excel &Lotus 1-2-3 电子表格.

Add to this lack of specificity the fact that a couple dozen other epochs have been used by various software systems, some extremely popular and common. Examples include January 1, 1601 for NTFS file system & COBOL, January 1, 1980 for various FAT file systems, January 1, 2001 for Apple Cocoa, and January 0, 1900 for Excel & Lotus 1-2-3 spreadsheets.

进一步添加一个事实,即使用了不同的计数粒度.除了整秒,有些系统还使用毫秒、微秒或纳秒.

Further add the fact that different granularities of count have been used. Besides whole seconds, some systems use milliseconds, microseconds, or nanoseconds.

我建议不要将日期时间作为从纪元开始的计数.而是使用您的编程语言或数据库中可用的特定数据类型.

I recommend against tracking date-time as a count-from-epoch. Instead use specific data types where available in your programming language or database.

当数据类型不可用或交换数据时,请遵循 ISO 8601 标准它为各种日期时间值定义了合理的字符串格式.

When data types are not available, or when exchanging data, follow the ISO 8601 standard which defines sensible string formats for various kinds of date-time values.

  • 日期
    • 2015-07-29
    • 2015-07-29T14:59:08Z
    • 2001-02-13T12:34:56.123+05:30
    • 2015-W31
    • 2015-W31-3
    • 2015-210
    • 2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
    • P3Y6M4DT12H30M5S =三年六个月四天十二小时三十分五秒的时间段"
    • P3Y6M4DT12H30M5S = "period of three years, six months, four days, twelve hours, thirty minutes, and five seconds"

    在 StackOverflow.com 上搜索更多关于这些主题的问题和答案.

    Search StackOverflow.com for many more Questions and Answers on these topics.

    这篇关于如何将纪元之前的日期表示为 UNIX 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆