subtract_datetime() calculates the difference in days & months but you choose only the day difference and ignore the month difference. And, delta_days(), per pod of installed version here, takes a DateTime object unlike your usage. in_units() seems screwy until one gets the doc linked there.
use strict; use warnings;
use Data::Dumper;
local $Data::Dumper::Indent = 2 ;
local $Data::Dumper::Pad = ' ' ;
local $Data::Dumper::Deepcopy = 1 ;
use DateTime;
my %base =
( 'hour' => 0 , 'minute'=> 0 , 'second' => 0
, 'time_zone' => +0000
);
my $ref_date =
DateTime->new( %base , 'year' => 2013 , 'month' => 1 , 'day' => 1
+);
my @date =
( DateTime->new( %base , 'year' => 2013 , 'month' => 1 , 'day' => 31
+ )
, DateTime->new( %base , 'year' => 2013 , 'month' => 2 , 'day' => 3
+ )
, DateTime->new( %base , 'year' => 2014 , 'month' => 1 , 'day' => 2
+ )
);
for my $date ( @date )
{
for my $method ( \&via_pcouderc , \&via_choroba )
{
printf "%s :: %s ...\n" , map $_->iso8601(), $ref_date , $date;
$method->( $ref_date , $date );
print "\n";
}
print "\n";
}
exit;
sub via_pcouderc
{
my ( $one , $two ) = @_;
my $obj = $two->subtract_datetime( $one );
return show( $obj , $obj->delta_days() , ( caller(0) )[3] );
}
sub via_choroba
{
my ( $one , $two ) = @_;
my $obj = $two->delta_days( $one );
return show( $obj , $obj->in_units('days') , ( caller(0) )[3] );
}
sub show
{
my ( $interval , $result , $sub ) = @_;
printf "%s => %s\n => %s\n" , $sub , $result , Dumper( $interval );
return;
}
__END__
|