DateTime::Duration: always normalize nanoseconds

2004-02-09 Thread Andrew Pimlott
It seems consistent and logical to normalize nanoseconds when
multiplying durations.  There is also a comment about normalization that
seems both redundant and misleading (since comparison doesn't depend on
nanoseconds being normalized; I assume comparison used to be done
without a reference time).

Although, looking at _normalize_nanoseconds and the tests for it, I
don't understand the sense of normalizing to (-1, 1) at all.

Andrew

--- lib/DateTime/Duration.pm.orig   2003-11-14 22:13:52.0 -0500
+++ lib/DateTime/Duration.pm2004-02-09 21:56:16.0 -0500
@@ -162,7 +162,6 @@
 $self-{$_} += $dur-{$_};
 }
 
-# we might have to normalize_nanoseconds before comparing durations
 $self-_normalize_nanoseconds if $self-{nanoseconds};
 
 return $self;
@@ -194,6 +193,8 @@
 $self-{$_} *= $multiplier;
 }
 
+$self-_normalize_nanoseconds if $self-{nanoseconds};
+
 return $self;
 }
 
--- t/11duration.t.orig 2004-02-09 22:00:36.0 -0500
+++ t/11duration.t  2004-02-09 22:02:08.0 -0500
@@ -2,7 +2,7 @@
 
 use strict;
 
-use Test::More tests = 86;
+use Test::More tests = 88;
 
 use DateTime;
 use DateTime::Duration;
@@ -163,6 +163,12 @@
 
 $new2-subtract( nanoseconds = 1 );
 is( $new2-delta_nanoseconds, 100300400, 'sub nanoseconds works' );
+
+my $new3 = $dur2 * 3;
+
+is( $new3-delta_seconds, 4, 'seconds normalized after multiplication');
+is( $new3-delta_nanoseconds, 5,
+'nanoseconds normalized after multiplication' );
 }
 
 {


off-by-one re leap seconds in subtract_datetime

2004-02-09 Thread Andrew Pimlott
Miscalculation of when we're in a leap minute.

Andrew

--- lib/DateTime.pm.orig2004-01-07 17:39:02.0 -0500
+++ lib/DateTime.pm 2004-02-09 22:19:24.0 -0500
@@ -908,7 +908,7 @@
 {
 my ( $utc_rd_days, $utc_rd_secs ) = $smaller-utc_rd_values;
 
-if ( $utc_rd_secs  86340  ! $is_floating )
+if ( $utc_rd_secs = 86340  ! $is_floating )
 {
 # If the bigger of the two datetimes occurs in the last UTC minute
 # of the UTC day, then that minute may not be 60 seconds long.  If
--- t/19leap_second.t.orig  2004-02-09 22:20:09.0 -0500
+++ t/19leap_second.t   2004-02-09 22:20:13.0 -0500
@@ -2,7 +2,7 @@
 
 use strict;
 
-use Test::More tests = 71;
+use Test::More tests = 74;
 
 use DateTime;
 
@@ -299,3 +299,23 @@
 is( $neg_dur-delta_minutes, 0, 'delta_minutes is 0' );
 is( $neg_dur-delta_seconds, -36, 'delta_seconds is -36' );
 }
+
+# catch off-by-one when carrying a leap second
+{
+my $dt1 = DateTime-new( year = 1998, month = 12, day = 31,
+ hour = 23,  minute = 59, second = 0,
+ nanosecond = 1,
+ time_zone = 'UTC',
+   );
+
+my $dt2 = DateTime-new( year = 1999, month = 1, day = 1,
+ hour = 0,  minute = 0, second = 0,
+ time_zone = 'UTC',
+   );
+
+my $pos_dur = $dt2 - $dt1;
+
+is( $pos_dur-delta_minutes, 0, 'delta_minutes is 0' );
+is( $pos_dur-delta_seconds, 60, 'delta_seconds is 60' );
+is( $pos_dur-delta_nanoseconds, 9, 'delta_nanoseconds is 999...' );
+}