Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Inconsistent results of subtraction with DateTime?

by rjt (Curate)
on Aug 07, 2013 at 19:37 UTC ( [id://1048424]=note: print w/replies, xml ) Need Help??


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
use strict; use warnings; omitted for brevity.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1048424]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-23 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found