in reply to Problem with DateTime subtract_datetime

Could you give a concrete example where this does not produce the expected output so we can reproduce the problem?

The DateTime documentation for the "subtraction" methods require careful reading. For example, from subtract_datetime (emphasis mine): "The returned duration may have deltas for months, days, minutes, seconds, and nanoseconds." (note there is no mention of weeks). And DateTime::Duration objects have conversion methods to convert between different measures of time, and conversions aren't always possible (see the module's documentation). Personally I try to stay away from this kind of math.

Also, what kind of difference are you looking for? A "logical" difference, like "2003-03-15 minus 2003-02-15 = 1 month" (that's the example from subtract_datetime, note how the answer is not given in days or weeks), or a "absolute" difference, that is, an exact difference between two points in time (in seconds/nanoseconds, which could then be converted to other measures)? Because if it's the latter, you need subtract_datetime_absolute:

my $dt1 = DateTime->from_epoch( epoch=>1403785930, time_zone=>'Europe/London' ); my $dt2 = DateTime->new( year=>2014, month=>6, day=>26, hour=>14, minute=>33, second=>03, nanosecond=>400e6, time_zone=>'Europe/Paris' ); my $diff_sec = $dt1->subtract_datetime_absolute($dt2) ->in_units('nanoseconds')/1e9; print "$diff_sec s\n"; __END__ -53.4 s