Skip to content
Browse files

Fix bug with sub seconds and DateTime->now() not returning nanoseconds

  • Loading branch information...
1 parent dc39871 commit 8b51e0d6f6d6568aa9781a1e9aa82bcf6660ada0 @2shortplanks committed
Showing with 24 additions and 2 deletions.
  1. +6 −0 README
  2. +12 −0 lib/Test/Recent.pm
  3. +6 −2 t/01owa.t
View
6 README
@@ -54,6 +54,12 @@ COPYRIGHT
under the same terms as Perl itself.
BUGS
+ This module ignores sub-seconds. This is primarily because the current
+ implementation of DateTime's "now" method does not return nanoseconds,
+ meaning that technically "now" returns a time that is in the past and
+ might occur before a timestamp you hand in that contained nanoseconds
+ (and therefore would erroneously be not concidered "recent")
+
Bugs should be reported via this distribution's CPAN RT queue. This can
be found at <https://rt.cpan.org/Dist/Display.html?Test-Recent>
View
12 lib/Test/Recent.pm
@@ -44,6 +44,12 @@ sub occured_within_ago($$) {
my $time = _datetime($value);
return unless defined $time;
+ # forget the nanoseconds in the time passed to us. This is necessary
+ # because DateTime->now() doesn't return nanoseconds, so if we don't
+ # forget nanoseconds what is passed in might actually be mistaken
+ # for something in the future
+ $time = $time->clone->set_nanosecond(0);
+
my $duration = shift;
unless (blessed $duration && $duration->isa("DateTime::Duration")) {
$duration = DateTime::Duration->new(
@@ -155,6 +161,12 @@ and/or modify it under the same terms as Perl itself.
=head1 BUGS
+This module ignores sub-seconds. This is primarily because the current
+implementation of DateTime's C<now> method does not return nanoseconds, meaning
+that technically C<now> returns a time that is B<in the past> and might
+occur before a timestamp you hand in that contained nanoseconds (and therefore
+would erroneously be not concidered "recent")
+
Bugs should be reported via this distribution's
CPAN RT queue. This can be found at
L<https://rt.cpan.org/Dist/Display.html?Test-Recent>
View
8 t/01owa.t
@@ -2,7 +2,7 @@
use strict;
-use Test::More tests => 20;
+use Test::More tests => 22;
use Test::Recent qw(occured_within_ago);
ok(defined &occured_within_ago, "exported");
@@ -47,4 +47,8 @@ ok occured_within_ago('2012-05-23T06:36:29-04', "10s"), "past";
ok !occured_within_ago('2012-05-23T06:36:19-04', "10s"), "too past";
# test postgres style timezones
-ok occured_within_ago('2012-05-23 06:36:29.987215-04','10s'), "postgres";
+ok occured_within_ago('2012-05-23 06:36:29.987215-04','10s'), "postgres";
+
+# test we cope with something that has nanoseconds
+ok occured_within_ago('2012-05-23T10:36:30.987215','10s'), "nanoseconds (low)";
+ok occured_within_ago('2012-05-23T10:36:30.187215','10s'), "nanoseconds (high)";

0 comments on commit 8b51e0d

Please sign in to comment.
Something went wrong with that request. Please try again.