在Perl中查找两个日期之间的时间 [英] Finding the time between two dates in Perl

查看:146
本文介绍了在Perl中查找两个日期之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以分钟为单位计算和显示输入的日期和当前时间之间的时差。我有:

I am trying to calculate and display, in minutes, the difference between the entered date and the present time. I have:

print "Enter a date YYYY MM DD. Remember perl's months go from 0-11.\n";
while ( @dateEnt < 1 ) {
    my $dateEntered = <STDIN>;
    chomp $dateEntered;
    push @dateEnt, $dateEntered;

    (@datedata) = split( /\s+/, $dateEntered );
    $year  = $datedata[0];
    $month = $datedata[1];
    $day   = $datedata[2];
}

$time         = time;
$readabletime = localtime($time);

use Time::Local;
$timeB = localtime [1];
$timeBetween = $readabletime[1] - $timeB;
print "The time between the dates is $timeBetween\n";

执行此操作时,我什么也没得到答案。有什么帮助吗?

When I execute this, I get nothing for an answer. Any help?

推荐答案

使用严格; 使用警告; 会告诉您很多问题。

use strict; and use warnings; would have told you about a lot of the problems.


  • 那个 while 循环是多余的,因为您 push 无需任何验证。因此,在第二次迭代中,总是为真。

  • 使得 @dateEnt 变得多余。

  • 本地时间在标量上下文中给出一个字符串。您不能使用字符串进行数学运算。 (有时您可以作弊,因为 perl 可以将其转换为数字,但这不适用于日期字符串。)

  • 使用通常位于程序的顶部,因为无论如何它都是完成的第一件事

  • 数组上下文中的localtime 返回一个值数组。 ($ sec,$ min,$ hour,$ mday,$ mon,$ year,$ wday,$ yday,$ isdst)= localtime(time); 可以执行(本地时间)[1] ,这会给您 $ min 。但这对进行比较而言意义不大。

  • $ visibletime [1] 引用名为 @readytime 。这个不存在。

  • that while loop is redundant, because you push without any validation. So it'll always be true on the second iteration.
  • which makes @dateEnt redundant.
  • localtime in a scalar context gives a string. You cannot do maths with a string. (Occasionally you can cheat, because perl can convert it to a number, but that doesn't work with a date string).
  • use is usually at the top of a program, because it's the first thing 'done' regardless
  • localtime in an array context returns an array of values. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); you can do (localtime)[1] which gives you $min. But that's not too meaningful for comparing anyway.
  • $readabletime[1] refers to the second element of an array called @readabletime. This doesn't exist.

因此,要完成您要执行的操作-我建议使用 Time :: Piece (核心perl模块)是这样的:

So to accomplish what you're after - I would suggest using Time::Piece (core perl module) like this:

#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece

my $date;
while ( not $date ) {
    print "Enter a date YYYY MM DD\n";
    my $dateEntered = <STDIN>;
    chomp $dateEntered;
    $date = Time::Piece->strptime( $dateEntered, "%Y %m %d" );
}
print "Understood: $date\n";
my $timebetween_s = ( time() - $date->epoch );
print $timebetween_s, "s between then and now\n";

注意-它使用 strptime 转换在 Time :: Piece 对象中输入日期(如果需要,您可以仅 print 进行打印,但您也可以进行其他转换)。 strptime 支持许多不同的格式。

Note - it uses strptime to convert the entered date to a Time::Piece object (which you can just print if you want and get a stringified date, but you can also do other transformations). strptime supports lots of different formats.

但是您只需减去 $ date-> ;; epoch -从 time()获取自1970年1月1日以来的时间(以秒为单位)。这也是自1970年1月1日以来的时间(以秒为单位)。给您一个您想要的时间增量-以秒为单位。如果要使用其他单位,则需要将其划分。

But you just subtract $date->epoch - which gets the time in seconds since 1st Jan 1970, from time() which is also the time in seconds since 1st Jan 1970. This gives you a time delta like you want - in seconds. You'll need to divide it if you want different units.

这篇关于在Perl中查找两个日期之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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