Subject: | off-by-one error in _format_fractional |
Hi,
The date included in the test below triggers an interesting edge case in
_format_fractional. When formatted initially, the correct string is
built. When this string is parsed and formatted again, there is one
nanosecond difference between the dates.
I believe this is due to some oddity with integer maths, though it is
difficult to pin down. Testing in the debugger, the issue was resolved
by changing:
sprintf(".%09d", $ns)
to:
sprintf(".%09d", "$ns")
I have attached a patch for DateTime::Format::Pg 0.16004 which makes
this one-line change, and also adds a test that demonstrates the issue.
Regards,
Andrew Whatson
Subject: | datetime_format_pg_format_fractional.diff |
=== modified file 'lib/DateTime/Format/Pg.pm'
--- lib/DateTime/Format/Pg.pm 2010-06-23 01:08:36 +0000
+++ lib/DateTime/Format/Pg.pm 2010-06-23 01:09:10 +0000
@@ -640,7 +640,7 @@
sub _format_fractional
{
my $ns = shift->nanosecond;
- return $ns ? sprintf(".%09d", $ns) : ''
+ return $ns ? sprintf(".%09d", "$ns") : ''
}
sub format_time
=== added file 't/format_fractional.t'
--- t/format_fractional.t 1970-01-01 00:00:00 +0000
+++ t/format_fractional.t 2010-06-23 01:22:26 +0000
@@ -0,0 +1,26 @@
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+use DateTime;
+use DateTime::Format::Pg;
+
+my $dt1 = DateTime->new(
+ year => 2010,
+ month => 6,
+ day => 21,
+ hour => 10,
+ minute => 11,
+ second => 9,
+ nanosecond => 66727999,
+ time_zone => 'Australia/Brisbane',
+
+);
+
+my $ts1 = DateTime::Format::Pg->format_datetime($dt1);
+
+my $dt2 = DateTime::Format::Pg->parse_datetime($ts1);
+my $ts2 = DateTime::Format::Pg->format_datetime($dt2);
+
+is($ts1, $ts2, 'fractional timestamptz was parsed/formatted correctly');