当输入仅为秒时,Perl 本地时间返回 16 小时
我一定做错了什么,但我无法弄清楚。当我将秒作为 localtime() 的参数时,我会得到秒和 16 小时。
my $startTime = time;
(process)
my $endTime = time;
my $diffTime = ( $endTime - $startTime );
($sec,$min,$hour) = localtime( $diffTime );
print STDERR "diffTime = $diffTime\n";
print STDERR "hour = $hour\n";
print STDERR "min= $min\n";
print STDERR "sec = $sec\n";
print( sprintf( "Elapsed time : %02d:%02d:%02d\n", $hour, $min, $sec ) );
...总是打印:
diffTime = 4
hour = 16
min= 0
sec = 4
Elapsed time : 16:00:04
好的。弄清楚如何添加评论 - NoScript 设置太严格。
谢谢...
我似乎无法向该线程添加评论,所以我只是感谢这里的每个人。
不使用 gmtime 是问题所在。它可能不是最有效的解决方案,但它可以满足我的需要,这只是用户评估他/她可能等待例程完成多长时间并决定输入有多大的信息他/她熟悉的数据集。
I must be doing something wrong, but I can't figure it out. When I give seconds as an argument to localtime(), I get seconds and 16 hours back.
my $startTime = time;
(process)
my $endTime = time;
my $diffTime = ( $endTime - $startTime );
($sec,$min,$hour) = localtime( $diffTime );
print STDERR "diffTime = $diffTime\n";
print STDERR "hour = $hour\n";
print STDERR "min= $min\n";
print STDERR "sec = $sec\n";
print( sprintf( "Elapsed time : %02d:%02d:%02d\n", $hour, $min, $sec ) );
...always prints:
diffTime = 4
hour = 16
min= 0
sec = 4
Elapsed time : 16:00:04
OKAY. Figured out how to add comments - NoScript settings were too tight.
Thanks...
I can't seem to add comments to this thread, so I'll just thank everyone here.
NOT using gmtime was the problem. It may not be the most efficient solution, but it works for what I need, which is just a simple bit of info for the user to evaluate how long he/she might wait for the routine to complete and make decisions about how large an input dataset he/she is comfortable with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你混合了持续时间和时间戳,这是自找麻烦......
正如曼苏尔建议的那样,您可能可以使用
gmtime
,但您仍然将苹果用作橙子。以下内容有效,并使用核心 Time::Piece 和 Time::Seconds 模块。不过,对于
$diffTime->pretty
,您需要比当前 Perl 版本捆绑的 Time::Piece 版本更新的版本 (1.20)。这可能会打印如下内容:
You're mixing durations and timestamps, that's asking for trouble...
You might get away with using
gmtime
, like Mansoor suggests, but still, you're using an apple as an orange.The following works, and uses the core Time::Piece and Time::Seconds modules. For
$diffTime->pretty
, you need a newer version (1.20) of Time::Piece than is bundled with current versions of Perl, though.This might print something like:
$difftime
是两个“纪元时间”(自 1970 年 1 月 1 日午夜 GMT 起的秒数)之间的差异,但这并不能使其成为纪元时间。所以它并不是localtime()
真正合适的参数。您最好自己计算 h/m/s 细分:$difftime
is the difference between two "epoch times" (seconds since midnight GMT 1/1/1970), but that doesn't make it an epoch time. So it's not really a suitable argument forlocaltime()
. You're better off computing the h/m/s breakdown yourself:您想要使用
gmtime
函数而不是localtime
函数。由于纪元日期为 1970 年 1 月 1 日 00:00:00 GMT/UTC,因此调用
localtime(0)
将为您提供您<的纪元日期/em> 时区。编辑:感谢您的跟进。正如已经提到的,这仅在您测量的时间间隔小于 24 小时时才有效。
localtime
和gmtime
函数实际上都返回日期值:秒、分钟、小时、月份中的某一天、月份、年份、年份中的某一天,以及是否时间进入夏令时。当然,除了日期之外,秒、分钟和小时之外的所有内容都没有意义。You want to use the
gmtime
function instead of thelocaltime
function.Since the epoch date is Jan. 1, 1970 00:00:00 GMT/UTC, calling
localtime(0)
will give you the epoch date in your time zone.EDIT: Thanks for the follow-ups. As has been mentioned, this only works if you're measuring an interval that is less than 24 hours. Both the
localtime
andgmtime
functions actually return the values of a date: seconds, minutes, hours, day of the month, month, year, day of the year, and whether the time falls into a daylight savings period. Of course, everything beyond seconds, minutes, and hours don't make sense outside of the context of a date.这与说由于 ℉ → ℃ 需要转换
C = (F - 32) * 5/9
相同的问题,如果温度升高 5 ℉,则必须与以下内容相同变暖 -15 ℃,因为这是当你将 5 华氏度代入明显但错误的公式时得到的数字。明白为什么这没有意义吗?变暖 5 华氏度与变冷 15 ℃ 完全不同!
你在你的时代也犯过同样的错误。当您减去自该纪元以来每个测量秒数的数量时,您不会得到一个也测量自该纪元以来经过的秒数的结果!
您只能在纪元秒(即自纪元以来经过的秒数)上调用
localtime
或gmtime
,而不能在一般秒上调用。否则,您必须自己进行除法,或者使用模块来找出减法结果中有多少个更大的单位。This is the same problem as saying that since ℉ → ℃ requires a conversion of
C = (F - 32) * 5/9
, that if it got warmer by 5 ℉ that that must be the same as getting warmer by −15 ℃, since that’s the number you get when you just plug 5 ℉ into the obvious-but-wrong formula.See why that makes no sense? Getting warmer by 5 ℉ is not at all the same as getting colder by 15 ℃!
You’ve made much the same error with your times. When you subtract quantities each measuring seconds elapsed since the epoch, you do not get a result that also measures seconds elapsed since the epoch!!
You can only call
localtime
orgmtime
on epoch seconds, meaning on seconds elapsed since the epoch, not on seconds in general. You have to do your own dividing otherwise, or use a module, to find out how many larger units there are in the result of that subtraction.