将纪元差异转换为天数

use*_*128 -1 perl iso epoch

在将它们转换为纪元后,我计算了两个ISO 8601日期的差异.如何在天数中得出它们的差异?我的代码是

my $ResolvedDate = "2014-06-04T10:48:07.124Z";
my $currentDate = "2014-06-04T06:03:36-04:00"

my $resolved_epoch = &convert_time_epoch($ResolvedDate);
my $current_epoch = &convert_time_epoch($currentDate);

if (($resolvedDate - $currentDate) > $noOfDays) {
    print "Difference in greater than x\n";
    $built = 0;
    return ($built);
} else {
    print "Difference in smaller than x \n";
    $built = 1;
    return ($built);
}

sub convert_time_epoch {
    my $time_c = str2time(@_);
    my @time_l = localtime($time_c);
    my $epoch = strftime("%s", @time_l);

    return($epoch);
}
Run Code Online (Sandbox Code Playgroud)

这里除了$ built我还想要返回确切的天数,已解决的日期大于当前日期.

Leo*_*erd 5

"天数"很尴尬,因为这是本地时间和DST存在(或者至少可能存在).

通过简单地除以86400,您可以轻松获得24小时的数量,这可能足以满足您的需求.

但是,如果您想要mday字段更改的真实次数,则可能与此简单除法所获得的值略有不同.


Cha*_*hak 5

如果日期以纪元秒为单位,则取差值除以一天的秒数(即 86400)。就像这样:

my $days_difference = int(($time1 - $time2) / 86400);
Run Code Online (Sandbox Code Playgroud)

如果您使用日期时间那么

my $duration = $dt1->delta_days($dt2); #$dt1 and $dt2 are DateTime objects.
print $duration->days;
Run Code Online (Sandbox Code Playgroud)