使用perl中的Date :: Manip计算天数差异 [英] calculate the difference in days using Date::Manip in perl

查看:233
本文介绍了使用perl中的Date :: Manip计算天数差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码仅表示如下所示的月份和日期的差异:

The code below only expresses the difference in months and days like so:

0:2:0:5:0:0:0

0:2:0:5:0:0:0

所以它有效,但是我想知道$ ADDate可以变化的总天数。希望这很简单,我只是完全错过了如何做。

So it works, but I want to know the total number of days given that $ADDate can vary quite a bit. Hopefully this is simple, and I just completely missed how to do it.

#!/usr/bin/perl

use Date::Manip 6.42;

my $ADDate = "20131211000820.0Z";
my $var;
my @val;
my $diff;

calc_period($ADDate = "20131211000820.0Z");

sub calc_period
{
$ADDate =~ s/^([\d][\d][\d][\d])([\d][\d])([\d][\d])/$1-$2-$3/gs;
$ADDate =~ s/.........$//gs;
$today = ParseDate("today");

$beginning = ParseDate($ADDate);
$end = ParseDate($today);
$delta = DateCalc($beginning,$end,\$err,1);

#$delta =~ s/([\d+][:][\d+]):.*$/$1/gs;


print "$delta\n";
print "$ADDate\n";
}


推荐答案

我不熟悉Date :: Manip,但我认为另一种方法是使用 Time :: Piece 解析你的字符串,并做任何你喜欢的,因为取两个Time :: Piece对象的差异返回一个Time :: Seconds对象

I'm not familiar with Date::Manip, but I think another way to do this is to use Time::Piece to parse your string and do whatever you like with that since taking the difference of two Time::Piece object returns a Time::Seconds object.

以下示例将显示差异当前时间和硬编码时间,并在几天内显示。

The following example will show the difference of the current time and the hardcoded time and show it in days.

#!/usr/bin/perl

use strict;
use warnings;

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

my $d = "20131211000820.0Z";

my $t = Time::Piece->strptime($d, "%Y%m%d%H%M%S.0Z");
my $now = Time::Piece->localtime();

my $diff = Time::Seconds->new($now - $t);

print $diff->days, "\n";

这篇关于使用perl中的Date :: Manip计算天数差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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