查看投票和评论,DateTime 模块似乎是此类内容的权威、首选模块。不幸的是,它的$dt->epoch() 文档带有这些警告;
Since the epoch does not account for leap seconds, the epoch time for
1972-12-31T23:59:60 (UTC) is exactly the same as that for 1973-01-01T00:00:00.
This module uses Time::Local to calculate the epoch, which may or may not
handle epochs before 1904 or after 2038 (depending on the size of your system's
integers, and whether or not Perl was compiled with 64-bit int support).
看起来这些是您必须在其中工作的限制。
话虽如此,这条评论可能是对用户的明智警告
- 正在使用具有 32 位整数的机器;或
- 即使对于“旧”日期也具有较低的容错能力
如果您有一台 32 位机器,第一个 将是一个问题。带符号的基于 32 位的纪元的范围(以年为单位)约为 2^31 / (3600*24*365) 或(仅)从 1970 年到/从 68 年(假设为 unix 纪元)。然而,对于 64 位 int,它从 1970 年到/从 1970 年变成了 290,000 年——我想这没关系。 :-)
只有你能说第二个问题是否会成为问题。
以下是对错误程度的粗略检查的结果;
$ perl -MDateTime -E 'say DateTime->new( year => 0 )->epoch / (365.25 * 24 * 3600)'
-1969.96030116359 # Year 0ad is 1969.96 years before 1970
$ perl -MDateTime -E 'say DateTime->new( year => -1000 )->epoch / (365.25*24*3600)'
-2969.93839835729 # year 1000bc is 2969.94 years before 1970
$ perl -MDateTime -E 'say ((DateTime->new( year => -1000 )->epoch - DateTime->new( year => 0 )->epoch ) / (365.25*24*3600))'
-999.978097193703 # 1,000bc has an error of 0.022 years
$ perl -MDateTime -E 'say 1000*365.25 + ((DateTime->new( year => -1000 )->epoch - DateTime->new( year => 0 )->epoch ) / (24 * 3600))'
8 # ... or 8 days
$
注意:我不知道这个“错误”在多大程度上是由于我检查它的方式 - 一年不是 365.25 天。事实上,让我更正一下 - 我从 here 中更好地定义了一年中的天数,我们得到了;
$ perl -MDateTime -E 'say 1000*365.242189 + ((DateTime->new( year => -1000 )->epoch - DateTime->new( year => 0 )->epoch ) / (24 * 3600))'
0.189000000013039
因此,使用大约公元前 1000 年的日期时会出现 0.2 天的错误。
简而言之,如果你有 64 位的机器,你应该没问题。