我的目標是在perl中處理廣泛的日期,例如包括公元453年,即 "匈奴王阿提拉 "死亡的那一年https://en.wikipedia.org/wiki/Attila。
。現在,https://perldoc.perl.org/Time::Local指出了timelocal()在處理距離現在遙遠的年份時的一個局限性:
timelocal_modern()和timegm_modern()
。當Time::Local最初被撰寫時,通常的做法是用兩位數來表示年份,比如99代表1999年,1代表2001年。這導致了各種各樣的問題(如果你很年輕的話,可以用谷歌搜索 "千年蟲問題"),開發者最終意識到這是一個糟糕的想法。
關于timelocal(),它說
警告。這些函式和它們的nocheck變體所使用的年值解釋幾乎肯定會導致你的代碼出現錯誤,如果不是現在,就是將來。我們強烈建議你不要在新代碼中使用這些函式,如果可能的話,你應該將舊代碼轉換為使用*_posix或*_modern函式。
...
年值解釋 這并不適用于*_posix或*_modern函式。如果你想確保在你的代碼老化時有一致的行為,請使用這些輸出。
相應地,我想使用timelocal_modern()。但是我下面的代碼同時顯示了這兩樣東西
timelocal()的行為與遙遠的日期,以及timelocal_modern()產生了一個例外的事實。
#!/usr/bin/perl
use strict; use warnings;
use Time::Local;
使用 POSIX qw(strftime)。
print $^V; print "
"。
print "the year Atilla the Hun died:
"。
epochsTOyear(-47855224609)。
my $modern;
eval{$modern=timelocal_modern(0, 0, 0,1,0,0) }。
if(length $@)
{
print join(''/span>, '$modern=timelocal_modern(0,0,0,1,0,0) threw error: ', $@, "
",)。
}
else; }
{
print "modern==$modern
"。
}
yearTOformat1january(-1)。
yearTOformat1january(0)。
yearTOformat1january(2000);
yearTOformat1january(1);
yearTOformat1january(2001)。
yearTOformat1january(1000)。
yearTOformat1january(-999)。
sub yearTOformat1january
{
my @base=(0, 0, 0,1,0,)。)
my $answer = timelocal( @base, $_[0]) 。
print join(''/span>, "year==", $_[0], " so epoch seconds==", $answer, " yields " , (strftime '%Y-%m-%d %a %H: %M:%S', localtime($answer)), "
",)。
}
sub epochsTOyear
{
my $yearresult=(strftime '%Y', localtime($_[0] )。
print join(''/span>, " epoch seconds=="/span>, $_[0], " yields" , $yearresult, "
",)。
return $yearresult。
}
以及我在電腦上得到的東西
~/u/kh/bin> perl --version
這是perl 5,版本18,subversion 4(v5. 18.4)構建for darwin-thread-multi-2級
(使用2注冊的補丁,見perl -V for更多細節)
Copyright 1987-2013, Larry Wall
Perl只能在藝術許可證或GNU通用公共許可證的條款下進行復制。
GNU通用公共許可證,可在Perl 5源套件中找到。
完整的Perl檔案,包括FAQ串列,應該可以在以下網站找到
這個系統,使用"man perl" 或者 "perldoc perl"。 如果你能訪問
互聯網,將你的瀏覽器指向http:/www.perl.org/,即Perl主頁。
~/u/kh/bin> z.pl
v5.18.4。
匈奴王阿提拉死的那一年。
epoch seconds==-47855224609 yields 0453
$modern=timelocal_modern(0,0,0。 1,0,0) 引發錯誤。在/Users/kpr/u/kh/bin/z.pl行12呼叫未定義的子程式&main::timelocal_modern。
年== -1 所以紀元秒== -2240506800 產生1899-01-01孫00。 00:00.
年==0所以紀元秒==946702800得出2000-01-01 Sat 00: 00:00.
年==2000,所以紀元秒==946702800,得出2000-01-01 Sat 00: 00:00.
年==1所以紀元秒==978325200得出2001-01-01 Mon 00。 00:00.
年==2001所以紀元秒==978325200得出2001-01-01 Mon 00。 00:00.
年==1000所以紀元秒==-30610206238得出1000-01-01 Wed00。 00:00.
年==-999所以紀元秒==-33734343838得出0901-01-01 Sat 00: 00:00.
~/u/kh/bin>
那么,我需要做什么來獲得timelocal_modern()?
uj5u.com熱心網友回復:
你需要使用Time::Local version 1.27 or better.
檢查$Time::Local::VERSION以查看您當前擁有的版本。
uj5u.com熱心網友回復:
Time::Local默認不輸出timelocal_modern。替換
use Time::Local;
與
use Time::Local qw( timelocal timelocal_modern )/span>;
我相信,無論如何都要列出你的匯入,這是一個好的做法。它允許讀者找出每個函式的來源。
你可能還需要升級Time::Local。
timelocal_modern被添加到相對較新的 1.27 中,但 Perl 5 18.4 中的 Time::Local 1.2300.。
uj5u.com熱心網友回復:
解決方案是
> brew install perl
<snip>
> brew link --overwrite --dry-run perl
<snip>
> brew link --overwrite perl
正在鏈接/usr/local/Cellar/perl/5.34.0。2476 符號鏈接創建。
> perl --version
這是perl 5,版本34,subversion 0(v5. 34.0)構建 for darwin-thread-multi-2級
而用下面的代碼則是
#!/usr/local/bin/perl
使用 Time::Local。
use Time::Local qw( timelocal_posix timegm_posix )/span>。
使用 POSIX qw(strftime)。
my $yearFROM1900;
unless(scalar @ARGV and $ARGV[0]=~/^[-]?d $/)
{
$yearFROM1900=0;
}
else; }
{
$yearFROM1900=$ARGV[0]。
}
print "yearFROM1900==$yearFROM1900
"。
my $a;
$a=timelocal(0,0,0,1,0, $yearFROM1900) 。
print join(''/span>, (strftime '%Y', localtime($a)), ' < - ', $a, " timelocal()
",)。
$a=timelocal_posix(0,0,0,1,[yearFROM1900]。
print join(''/span>, (strftime '%Y', localtime($a)), ' < - ', $a, " timelocal_posix()
",)。
我們得到兩個非常不同的結果。
如果我們給它一個數字-2470,這是詩人薩福興盛的日期與1900年之間的差異。
結果在timelocal和timelocal_posix之間是一致的。
但是,如果我們給它的年份是0,我們會看到一個巨大的差異。
> z.pl $((-1900-570) # 詩人薩福
yearFROM1900==-2470
-570 <- -80154644400 timelocal()
-570 <-80154644400 timelocal_posix()
> z.pl
yearFROM1900==0
2000 <- 946702800 timelocal()
1900 <- -2208970800 timelocal_posix()
令我驚訝的是,在這臺2019年的macOS電腦上,重新啟動是沒有必要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311378.html
標籤:
上一篇:需要解釋"~0"與"2**64"之間的關系,以及是否有"使用整數"。
下一篇:搜索和替換一個檔案中的多行內容
