in reply to Re^2: How to find the number of days with DateTime ?
in thread How to find the number of days with DateTime ?

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__
2013-01-01T00:00:00 :: 2013-01-31T00:00:00 ... main::via_pcouderc => 30 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 30, 'months' => 0 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2013-01-31T00:00:00 ... main::via_choroba => 30 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 30, 'months' => 0 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2013-02-03T00:00:00 ... main::via_pcouderc => 2 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 2, 'months' => 1 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2013-02-03T00:00:00 ... main::via_choroba => 33 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 33, 'months' => 0 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2014-01-02T00:00:00 ... main::via_pcouderc => 1 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 1, 'months' => 12 }, 'DateTime::Duration' ); 2013-01-01T00:00:00 :: 2014-01-02T00:00:00 ... main::via_choroba => 366 => $VAR1 = bless( { 'seconds' => 0, 'minutes' => 0, 'end_of_month' => 'wrap', 'nanoseconds' => 0, 'days' => 366, 'months' => 0 }, 'DateTime::Duration' );

Replies are listed 'Best First'.
Re^4: How to find the number of days with DateTime ?
by Anonymous Monk on Jan 03, 2014 at 14:34 UTC
    Could somebody please either hide the Perl code in "readmore" tags and remove "readmore" from the output; or hide both parts? Rational is that output is more important, for the OP simply needs to spend quality time with DateTime to understand it.