On Thu, Jul 15, 2010 at 10:43:20AM -0500, [email protected] wrote:
> Probably a simple question, but I'm not seeing the answer... I want to
> find out the difference in seconds between two dates, and I thought this
> would be the way to do it (see below). I expected the last output line
> to be 'dur is: 1320', which is 22 minutes times 60 seconds, not 'dur is:
> 0'. Any guidance from anyone? I'm sure I'm misunderstanding something
> simple and critical...
Try this:
use strict;
use warnings;
use DateTime;
use DateTime::Duration;
use Data::Dumper;
my $dt1 = DateTime->now->set_time_zone( 'America/Chicago' );
my $dt2 = $dt1->clone->add( minutes => 22 );
my $difference = $dt2->subtract_datetime_absolute($dt1);
my $dur_secs = $difference->in_units('seconds');
print Dumper($difference), "\n";
print "in seconds: $dur_secs\n";
prints:
$VAR1 = bless( {
'seconds' => '1320',
'minutes' => 0,
'end_of_month' => 'wrap',
'nanoseconds' => 0,
'days' => 0,
'months' => 0
}, 'DateTime::Duration' );
in seconds: 1320
--
"I felt a kind of forlorn sense of being lost in a world of
incredibly stupid and malicious dwarfs." - Aleister Crowley
. . . . .
Karen Etheridge, [email protected] GCS C+++$ USL+++$ P+++$ w--- M++
http://etheridge.ca/ PS++ PE-- b++ DI++++ e++ h(-)