perl - 获取两个 UNIX 时间戳之间的日变化点

标签 perl timestamp

我有日期范围(两个 UNIX 时间戳),我需要找到 23:59:59 的点(如果存在)。 我如何使用 Perl 来做到这一点?

附言我认为 for() 不是好主意,因为我可以有很大的范围。还有其他变体吗?

最佳答案

我会使用 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);
}

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

免责声明:当然,您也可以使用DateTime

关于perl - 获取两个 UNIX 时间戳之间的日变化点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4440376/

相关文章:

postgresql - 时间戳与当前时间的部分索引

mysql - 行创建和最后修改的时间戳

Javascript 时间戳是遥远的 future

perl - 打开和保存文件时丢失编码

linux - SLURM 上的多线程

perl - ScriptAlias 配置在 apache2 中不起作用

perl - 处理相互使用的模块的最佳方式是什么?

perl - 为什么不调用回调?

git - 是否可以将 git commit 设置为具有 1970 年之前的时间戳?

php - 将 unix 时间戳转换为 twitter/facebook 样式