如何计算Perl中两个时间戳字符串之间的差异 [英] How to calculate the difference between two timestamp strings in Perl

查看:53
本文介绍了如何计算Perl中两个时间戳字符串之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了所有可能的问题,但找不到答案,那么 Perl 专家能帮我解决这个问题吗?

I searched through all the possible questions but couldn't find the answer, so can Perl experts help me on this one?

我有两个时间戳,例如 05/25/2011 05:22:03 PM05/25/2011 05:34:08 PM.它们以字符串形式存储.

I have two timestamps like 05/25/2011 05:22:03 PM and 05/25/2011 05:34:08 PM. They are stored in string form.

my $str1 = '05/25/2011 05:22:03';
my $str2 = '05/25/2011 05:34:08';

后者是工作结束的时间,前者是工作开始的时间.

The latter being the time of a job ending and former being the time it started.

如何找出日期和时间的差异?在这种情况下,日期相同,但也可能不同.

How do I find out the difference in dates and time? The dates are the same in this case but they could differ as well.

推荐答案

我建议你使用时间::Piece 模块.自 Perl 5 9.5 版发布以来,它一直是核心模块,因此不需要安装.

I recommend that you use the Time::Piece module. It has been a core module since the release of version 9.5 of Perl 5, so it shouldn't need installing.

这段代码演示了

use strict;
use warnings;

use Time::Piece;

my $str1 = 'Execution started at 05/25/2011 05:22:03 PM';
my $str2 = 'Execution completed at 05/25/2011 05:34:08 PM';

my @times = map Time::Piece->strptime(/(\d.+M)/, '%m/%d/%Y %H:%M:%S %p'), $str1, $str2;

my $delta = $times[1] - $times[0];
print $delta->pretty;

输出

12 minutes, 5 seconds

这篇关于如何计算Perl中两个时间戳字符串之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆