使用Perl减去15分钟

The*_*Man 6 perl datetime

我认为这将非常简单,但我现在真的没有选择.我想从给定的时间减去15分钟.

示例我的时间是15:04我想将15分钟减去14:49.我在互联网上搜索过解决方案,但没有可以帮助我的perl模块.

Eug*_*ash 20

你可以使用DateTime:

my $dt = DateTime->new(
    year   => 1,
    month  => 1,
    day    => 1,
    hour   => 15, 
    minute => 4,
);  

$dt->subtract(minutes => 15);
printf "%d:%d\n", $dt->hour, $dt->minute; # prints 14:49
Run Code Online (Sandbox Code Playgroud)

  • 如果他只关心时间而不关注日期,他可以尝试[时间::时钟](http://search.cpan.org/perldoc?Time::Clock) (2认同)

Sod*_*ved 7

这一切都取决于你的时间存储方式.我更喜欢使用内置的time_t返回time.

my $now = time();
my $before1 = $now - (15*60);      # 15 minutes ago
my $before2 = $now - (3*60*60);    # 3 hours ago
my $before3 = $now - (2*24*60*60); # 2 days ago
Run Code Online (Sandbox Code Playgroud)

对于输出我使用POSIX模块

print POSIX::strftime( '%Y-%m-%d %T', localtime($before1) );
Run Code Online (Sandbox Code Playgroud)

  • 并非所有日子都有24小时. (3认同)