On Tue, 10 Feb 2004, Mark Fowler wrote: > Okay, I'm probably being stupid but...how do I get the number of seconds > a DateTime::Duration takes? I *think* I should be using delta_seconds, > but that doesn't seem to work: > > use DateTime::Duration; > my $dur = DateTime::Duration->new(hours => 2); > print $dur->hours . ":" . > $dur->minutes . ":" . > $dur->seconds . "\n"; > > print $dur->delta_minutes . ":" . > $dur->delta_seconds . "\n"; > > Prints: > > 2:0:0 > 120:0 > > Shouldn't that print > > 2:0:0 > 120:7200 > > Or am I being really stupid? It has been known.
I think it's a documentation problem. Basically, 2 hours != 7200 seconds, because of leap seconds. A duration is made up internally of several different units, months, days, minutes, seconds, and nanoseconds. Of those, the only ones that convert or normalize two other units are seconds <=> nanoseconds. For all the others, we cannot convert between them. The "delta_*" methods return the internal values, and are used for doing math. The non-delta methods return values more suitable for display. I should add a section to the docs on this so that people know what to expect. If you want to know how many seconds a duration really represents, you have to add it to a datetime to find out, so you could do: my $now = DateTime->now( time_zone => 'UTC' ); my $later = $now->clone->add_duration($duration); my $seconds_dur = $later->subtract_datetime_absolute($now); This returns a duration which only contains seconds and nanoseconds. There are other subtract/delta methods in DateTime.pm to generate different types of durations. -dave /*======================= House Absolute Consulting www.houseabsolute.com =======================*/