简体   繁体   English

如何在有两个datetime字符串的perl中获得日期和时间的差异?

[英]How to get difference in date and time in perl where I have two datetime strings?

I have two datetime strings as: 我有两个日期时间字符串:

$bstr = 02/07/17 09:25:22
$betr = 02/07/17 10:55:48
my $bstrDateTime = DateTime->from_epoch( epoch => str2time( $bstr ) );
my $betrDateTime = DateTime->from_epoch( epoch => str2time( $betr ) );
my $diff = $betrDateTime->subtract_datetime( $bstrDateTime );

$diff_h = sprintf $diff->in_units('hours');
$diff_m = sprintf $diff->in_units('minutes');
$diff_s = sprintf $diff->in_units('seconds');

$difference = $diff_h.":".$diff_m.":".$diff_s;
print $difference;

But I am getting wrong minute difference. 但是我错了微小的差别。

Output was: 1:90:48 输出为:1:90:48

Where I have done wrong? 我做错了什么? Instead of 90, it should be 30. 应该是30,而不是90。

You are currently using in_units but it is not doing what you expect. 您当前正在使用in_units,但未达到预期的效果。 As it has different functionality as it calculates the difference between the given objects in the specified unit. 由于它具有不同的功能,因此它会计算指定单位中给定对象之间的差异。 Provided that they are conversion compatible. 前提是它们与转换兼容。 For more information on this refer - DateTime::Duration or StackOverflow . 有关此信息的更多信息,请参见DateTime :: DurationStackOverflow

Now the correct code which will give your desired output is below: 现在,将给出所需输出的正确代码如下:

my $bstr = '02/07/17 09:25:22';
my $betr = '02/07/17 10:55:48';
my $bstrDateTime = DateTime->from_epoch(epoch => str2time($bstr));
my $betrDateTime = DateTime->from_epoch(epoch => str2time($betr));

my $diff = $betrDateTime->subtract_datetime( $bstrDateTime ); 

my $diff_h = $diff->hours;
my $diff_m = $diff->minutes;
my $diff_s = $diff->seconds;

my $difference = $diff_h.":".$diff_m.":".$diff_s;
print $difference;

Also you should have placed the timestamps inside quotes in your code posted and semicolon is also missing in the first couple of lines too. 另外,您应该在已发布的代码中将时间戳记放在引号内,并且前两行中也缺少分号。 Do remember to run the code which you are pasting to avoid these kinds of errors. 请记住运行您要粘贴的代码,以避免此类错误。

It helps if you give us code that actually compiles, so we don't have to spend time fixing your missing semicolons and quotes. 如果您提供给我们可以实际编译的代码,那么它会有所帮助,因此我们不必花费时间来修复丢失的分号和引号。

Using str2time() and creating a DateTime object from the epoch is a little wasteful. 使用str2time()并从纪元创建DateTime对象有点浪费。 Better to use DateTime::Format::Strptime to directly parse your string to a DateTime object. 最好使用DateTime :: Format :: Strptime直接将您的字符串解析为DateTime对象。

Using the hours() , minutes() and seconds() methods on DateTime::Duration (instead of in_units() ) will give you the results to want. 在DateTime :: Duration(而不是in_units() )上使用hours()minutes()seconds()方法将为您提供所需的结果。

use strict;
use warnings;

use DateTime::Format::Strptime;

my $date_parser = DateTime::Format::Strptime->new(
  pattern => '%d/%m/%y %H:%M:%S',
);

my $bstr = '02/07/17 09:25:22';
my $betr = '02/07/17 10:55:48';
my $bstrDateTime = $date_parser->parse_datetime( $bstr );
my $betrDateTime = $date_parser->parse_datetime( $betr );
my $diff = $betrDateTime->subtract_datetime( $bstrDateTime );

print $diff->hours, ', ', $diff->minutes, ', ', $diff->seconds, "\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM