in reply to Inconsistent results of subtraction with DateTime?
DateTime says:
- date vs datetime math
If you only care about the date (calendar) portion of a datetime, you should use either delta_md() or delta_days(), not subtract_datetime().
In short, subtract_datetime gives you an answer that includes days, minutes, and seconds between the two times. But delta_days essentially chops off the "time" part of your DateTime object and then does the subtraction, so you only get days.
To see for yourself, try dumping the DateTime::Duration objects:
use DateTime; use DateTime::Format::ISO8601; my ($first, $last) = map { DateTime::Format::ISO8601->parse_dateti +me($_) } qw < 2013-08-01T20:10:31 2013-08-06T20:09:34 + >; my ($sub, $days) = map { $last->$_($first) } qw< subtract_datetime delta_days >; printf "%12s %5s | %-5s\n", 'Value', 'sub', 'days'; printf "%.21s+%.8s\n", ('-'x21)x2; for (qw< months days minutes seconds nanoseconds >) { printf "%12s : %5d | %-5d\n", $_, $sub->{$_}, $days->{$_}; }
Output:
Value sub | days ---------------------+-------- months : 0 | 0 days : 4 | 5 minutes : 1439 | 0 seconds : 3 | 0 nanoseconds : 0 | 0
|
|---|