得到两个Time :: Piece对象之间的分钟差 [英] getting minutes difference between two Time::Piece objects

查看:107
本文介绍了得到两个Time :: Piece对象之间的分钟差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

use Time::Piece;
use Time::Seconds;

my $timespan = $latest_time - $timestamp;

print $latest_time . "\n";
print $timestamp . "\n";
print $timespan->minutes;

其中$ latest_time = Time :: Piece-> new;和$ timestamp = Time :: Piece-> strptime();

where $latest_time = Time::Piece->new; and $timestamp = Time::Piece->strptime();

我得到结果:

Thu Mar 27 09:40:19 2014
Thu Mar 27 09:40:00 2014
-479.683333333333

出了什么问题? $ timespan应该有0分钟,对吗? -479来自哪里?

What went wrong? there should be 0 minutes for $timespan, correct? Where is -479 coming from?

推荐答案

再现错误 >

Reproducing the "bug"

之所以会出现此问题,是因为strptime默认为UTC而不是本地时区。这可以在下面的代码中得到证明,该代码需要当前时间,将其打印出来,然后重新解析并显示出差异:

This issue arises because strptime defaults to UTC instead of to the local timezone. This can be demonstrated in the following code which takes a current time, prints it out, then reparses it and shows the difference:

use strict;
use warnings;

use Time::Piece;

my $now = Time::Piece->new();
print $now->strftime(), "\n";
my $fmt = "%Y-%m-%d %H:%M:%S";

my $nowstr = $now->strftime($fmt);
my $parsed = Time::Piece->strptime("$nowstr", $fmt);
print "($nowstr)\n";
print $parsed->strftime(), "\n";

my $diff = $now - $parsed;
print $diff->hours, " hours difference\n";

输出:

Wed, 26 Mar 2014 21:42:08 Pacific Daylight Time
(2014-03-26 21:42:08)
Wed, 26 Mar 2014 21:42:08 UTC
7 hours difference

一个棘手的解决方案-解析时间以本地方式读取

现在,在黑客攻击中,我已经在我的草莓perl系统上发现了一个潜在的 hack 。通过这样调用 strptime $ now-> strptime

Now, in hacking around, I've discovered one potential hack for this on my strawberry perl system. It's by calling strptime like this: $now->strptime.

my $nowstr = "2014-03-26 21:51:00";   #$now->strftime($fmt);
my $parsed = $now->strptime("$nowstr", $fmt);   #Time::Piece->strptime("$nowstr", $fmt);
print "($nowstr)\n";
print $parsed->strftime(), "\n";

my $diff = $now - $parsed;
print $diff->hours, " hours difference\n";

要确认 strptime 是否确实在使用设置时间时,我给了它比当前时间早6分钟的时间。输出如下:

To confirm that strptime was actually using the time I set it, I gave it one that was 6 minutes before the current time. The output is as follows:

Wed, 26 Mar 2014 21:57:00 Pacific Daylight Time
(2014-03-26 21:51:00)
Wed, 26 Mar 2014 21:51:00 Pacific Standard Time
0.1 hours difference

解析的时间将从 $ now 继承 c_islocal 值。 $ now 只需使用 localtime -> new()<初始化/ code>当然不是 gmtime

The parsed time will inherit the c_islocal value from $now. $now just needs to be initialized with either localtime or ->new() and not gmtime of course.

您可以看到一个要求DST,而另一个要求DST不会,但是日期数学仍然可以正确完成。通过查看 strptime _mktime new

As you can see one claims DST while the other does not, but date math is still done correctly. I was able to figure out this hack by looking at the source for strptime, _mktime, and new.

希望,至少我重现错误的代码将对具有$$ c经验的人有所帮助c> Time :: Piece ,我希望有更好的解决方案。

Hopefully, at the very least my code to reproduce the error will be helpful to someone with more experience with Time::Piece, and I'd love a better solution.

这篇关于得到两个Time :: Piece对象之间的分钟差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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