[英]Compare date time zone with time() in perl
我正在嘗試使用perl中的time()將格式為08-07-2016 08:16:26 GMT的文件創建時間與當前時間進行比較。 由於time()返回紀元時間,所以我不確定如何找到這兩種不同時間格式之間的時差。
我嘗試了以下類似操作,並且由於明顯的原因,我收到一條錯誤消息:“參數08-07-2016 08:16:26 GMT”不是數字減法。
my $current_time = time();
my $time_diff = $creation_time - $current_time;
if ($time_diff > 10) { #compare if the difference is greater than 10hours
# do something...
}
我有一些問題:
或者是否有一種方法可以至少使用DateTime或Time :: Local將任何給定的時間格式轉換為紀元?
如何將日期參數傳遞給DateTime構造函數?
my $dt1 = DateTime-> new (
year =>'1998',
month =>'4',
day =>'4',
hour =>'21',
time_zone =>'local'
);
相反,我們可以做些什么
my $date = '08-07-2016 08:16:26 GMT';
my $dt1 = DateTime->new($date); # how can i pass a parameter to the constructor
print Dumper($dt1->epoch);
在此先感謝您的幫助。
時間::自2007年以來, Piece已成為Perl的標准組成部分。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
use Time::Seconds;
my $creation_string = '08-07-2016 08:16:26 GMT';
my $creation_time = Time::Piece->strptime($creation_string, '%d-%m-%Y %H:%M:%S %Z');
my $current_time = gmtime;
my $diff = $current_time - $creation_time;
say $diff; # Difference in seconds
say $diff->pretty;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.