Skip to content

Commit

Permalink
Negative Seconds with fractional seconds had the wrong sign
Browse files Browse the repository at this point in the history
  • Loading branch information
OrionSteve committed Sep 13, 2018
1 parent e1bbb3c commit 2ef9131
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/DateTime/Format/Pg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ sub parse_duration {

$string =~ s/\b(\d+):(\d\d):(\d\d)(\.\d+)?\b/$1h $2m $3$4s/g;
$string =~ s/\b(\d+):(\d\d)\b/$1h $2m/g;
$string =~ s/(-\d+h)\s+(\d+m)\s+(\d+s)\s*/$1 -$2 -$3 /;
$string =~ s/(-\d+h)\s+(\d+m)\s+(\d+(?:\.\d+)?s)\s*/$1 -$2 -$3 /;
$string =~ s/(-\d+h)\s+(\d+m)\s*/$1 -$2 /;

while ($string =~ s/^\s*(-?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i) {
Expand Down Expand Up @@ -629,7 +629,8 @@ sub parse_duration {
# (duh, as 1 sec = 1_000_000 nano sec). If we're missing 0's,
# we should pad them
$fractional .= '0'x (6 - length($fractional));
push @extra_args, ("nanoseconds" => $fractional);
my $sign = ($amount > 0) ? 1 : -1;
push @extra_args, ("nanoseconds" => $sign * $fractional);
}

$du->$arith_method($base_unit => $amount * $num, @extra_args);
Expand Down
7 changes: 7 additions & 0 deletions t/parse_interval.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ BEGIN
[ '-1 days' => DateTime::Duration->new(days => -1) ],
[ '-23:59' => DateTime::Duration->new(hours => -23, minutes => -59) ],
[ '-1 days -00:01' => DateTime::Duration->new( days => -1, minutes => -1) ],
[ '-1 days -20:30:56.123456' => DateTime::Duration->new(
days => -1,
minutes => -1230, # = 20 * 60 + 30
seconds => -56,
nanoseconds => -123456,
),
],
[ '1 mon -1 days' => DateTime::Duration->new(months => 1)->add(days => -1) ],
[ '1 day 1 month' => DateTime::Duration->new(months => 1)->add(days => 1) ],
[ '1 month -1 days' => DateTime::Duration->new(months => 1)->add(days => -1) ],
Expand Down

0 comments on commit 2ef9131

Please sign in to comment.