Hi,

in trying not to reinvent the date/time wheel, I'm trying to use DateTime and DateTime::Duration but it's confusing me.

I've got a time (i.e. 23:15:00) and I need to offset that by a given (integer) number of hours. So if my offset is 2, the time should be 01:15.

This is what I did:

my $hours   = '23';
my $minutes = '15';
my $offset  = 2;

my $ts = new DateTime(
        year      => '2007',  ## don't care about year/month/day
        month     => '6',
        day       => '3',
        hour      => $hours,
        minute    => $minutes,
        second    => 0
);

my $duration = DateTime::Duration->new(hours => $offset);

my $normalizedTs = $ts->add($duration);

print STDERR "Converted time from $hours:$minutes with offset=$offset to " . $normalizedTs->strftime("%H:%S:00") . "\n";


Unfortunately, this gives me 01:00:00 instead of 01:15:00, somehow it's loosing track of the minutes.

I tried 'minutes => $offset * 60' as well, no change.

What obvious thing am I missing here?

Reply via email to