* Iain 'Spoon' Truskett ([EMAIL PROTECTED]) [01 Apr 2003 13:18]:
> Adding and subtracting small durations doesn't appear to
> affect the epoch. I tried adding a line to recalculate it,
> but that just seemed to screw things up elsewhere.
Ok. After having a break for lunch and doing some other stuff:
This probably isn't perfect; I've not really examined how the internals
of DT.pm operate. On the other hand, it does still pass all tests =)
Index: lib/DateTime.pm
===
RCS file: /cvsroot/perl-date-time/modules/DateTime.pm/lib/DateTime.pm,v
retrieving revision 1.124
diff -u -r1.124 DateTime.pm
--- lib/DateTime.pm 1 Apr 2003 02:37:36 - 1.124
+++ lib/DateTime.pm 1 Apr 2003 05:19:09 -
@@ -557,6 +557,7 @@
return $self->{utc_c}{epoch}
if exists $self->{utc_c}{epoch};
+$self->_calc_utc_rd;
my ( $year, $month, $day ) = $self->_utc_ymd;
my @hms = $self->_utc_hms;
@@ -677,6 +678,7 @@
$self->{utc_rd_secs} += $deltas{seconds};
_normalize_seconds( $self->{utc_rd_days}, $self->{utc_rd_secs} );
+delete $self->{utc_c}{epoch};
$self->_calc_local_rd;
}
Index: t/04epoch.t
===
RCS file: /cvsroot/perl-date-time/modules/DateTime.pm/t/04epoch.t,v
retrieving revision 1.10
diff -u -r1.10 04epoch.t
--- t/04epoch.t 1 Apr 2003 02:29:57 - 1.10
+++ t/04epoch.t 1 Apr 2003 05:19:10 -
@@ -1,6 +1,6 @@
use strict;
-use Test::More tests => 19;
+use Test::More tests => 24;
use DateTime;
@@ -69,4 +69,23 @@
is( $dt->offset, -3600, 'offset should be -3600' );
is( $dt->epoch, 0, 'epoch is 0' );
+}
+
+# Adding/subtracting should affect epoch
+{
+my $expected = '1049160602';
+my $epochtest = DateTime->from_epoch( epoch => $expected );
+
+is( $epochtest->epoch, $expected,
+"epoch method returns correct value ($expected)");
+is( $epochtest->hour, 1, "hour" );
+is( $epochtest->min, 30, "minute" );
+
+$epochtest->add( hours => 2 );
+$expected += 2*60*60;
+
+is( $epochtest->hour, 3, "adjusted hour" );
+is( $epochtest->epoch, $expected,
+"epoch method returns correct adjusted value ($expected)");
+
}