获取两个UNIX时间戳之间的日期更改点

Ver*_*Lom 2 perl timestamp

我有日期范围(两个UNIX时间戳),如果存在,我需要在23:59:59找到点.我怎么能用Perl做到这一点?

PS我觉得()并不是一个好主意,因为我可以拥有非常大的范围.还有其他变种吗?

igo*_*gor 5

我会用Date::Calc.假设您的两个时间戳位于$ts1$ts2:

use Date::Calc qw(Time_to_Date Date_to_Time Delta_Days Add_Delta_Days);

my @date1 = (Time_to_Date($ts1))[0..2];
my @date2 = (Time_to_Date($ts2))[0..2];
my @midnights;

for (my $i = 0; $i < Delta_Days(@date1, @date2); ++$i) {
    push @midnights, Date_to_Time(Add_Delta_Days(@date1, $i), 23, 59, 59);
}
Run Code Online (Sandbox Code Playgroud)

@midnights 现在包含两个给定时间戳之间所有23:59:59点的UNIX时间戳(纪元秒).

免责声明:当然你也可以这样做DateTime.