自从过去4个小时到现在,我每隔15分钟需要一次
例如,当前时间为15:30:00
然后我需要时间
2014-08-01 11:30:00
2014-08-01 11:45:00
2014-08-01 12:00:00
2014-08-01 12:15:00
2014-08-01 12:30:00
....
2014-08-01 15:30:00
共16条记录(15小时间隔4小时4 * 4)
我试过
date --date="2014-08-01 15:30:00 4hours ago" +'%Y-%m-%d %H:%M:%S'
答案 0 :(得分:1)
use strict; use warnings;
my $time = time();
my $period = 15*60; # 15 minutes
for ( my $t = $time -16*$period; $t<=$time; $t+=$period) {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($t);
printf "%d-%02d-%02d %2d:%02d:%02d\n", $year+1900, $mon+1, $mday, $hour, $min, $sec;
}
简单:
use strict; use warnings;
use POSIX qw( strftime );
my $time = time();
my $period = 15*60; # 15 minutes
for ( my $t = $time -16*$period; $t<=$time; $t+=$period) {
print strftime "%Y-%m-%d %H:$M:%S\n", localtime($t);
}
答案 1 :(得分:0)
这应该适合你。它打印十七个日期/时间,以当前时间或之前的最新15分钟倍数结束。它使用Time::Piece
,这是一个核心模块,应该已经安装在您的系统上。
use strict;
use warnings;
use 5.010;
use Time::Piece;
use Time::Seconds qw/ ONE_MINUTE /;
my $interval = ONE_MINUTE * 15;
my $now = localtime;
my $second_of_hour = $now->minute * ONE_MINUTE + $now->second;
my $final = $now - $second_of_hour % $interval;
for my $n (-16 .. 0) {
my $moment = $final + $n * $interval;
say $moment->datetime =~ tr/T/ /r;
}
<强>输出强>
2014-08-01 08:15:00
2014-08-01 08:30:00
2014-08-01 08:45:00
2014-08-01 09:00:00
2014-08-01 09:15:00
2014-08-01 09:30:00
2014-08-01 09:45:00
2014-08-01 10:00:00
2014-08-01 10:15:00
2014-08-01 10:30:00
2014-08-01 10:45:00
2014-08-01 11:00:00
2014-08-01 11:15:00
2014-08-01 11:30:00
2014-08-01 11:45:00
2014-08-01 12:00:00
2014-08-01 12:15:00