Skip to content

Commit

Permalink
DateTime filter: include nanoseconds, when nonzero
Browse files Browse the repository at this point in the history
We could show this always, but it would be a change and it seems a
little friendlier to omit zero nanoseconds, since so many duration will
be 0ns, and it won't be interesting.

On the other hand, ignoring some differences isn't a very good way to
present the data!
  • Loading branch information
rjbs committed Jul 29, 2024
1 parent f3d86fc commit cfe310e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/Data/Printer/Filter/DateTime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ filter 'DateTime', sub {
filter 'DateTime::Duration', sub {
my ($obj, $ddp) = @_;

my @dur = $obj->in_units(qw(years months days hours minutes seconds));
my @dur = $obj->in_units(qw(years months days hours minutes seconds nanoseconds));
my $string = "$dur[0]y $dur[1]m $dur[2]d $dur[3]h $dur[4]m $dur[5]s";

if ($dur[6] > 0) {
$string .= " $dur[6]ns";
}

return _format( $string, @_ );
};

Expand Down
11 changes: 9 additions & 2 deletions t/100-filter_datetime.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strict;
use warnings;
use Test::More tests => 21;
use Test::More tests => 22;
use Data::Printer::Object;

my $has_timepiece;
Expand Down Expand Up @@ -103,14 +103,21 @@ sub test_datetime {
is( $ddp->parse($d1), '1981-09-29T00:00:00', 'DateTime without TZ data' );

my $diff;
skip 'DateTime::Duration not available', 1
skip 'DateTime::Duration not available', 2
unless eval { $diff = $d2 - $d1; $diff && $diff->isa('DateTime::Duration') };

$ddp = Data::Printer::Object->new(
colored => 0,
filters => ['DateTime'],
);
is( $ddp->parse($diff), '3y 1m 16d 0h 0m 0s', 'DateTime::Duration' );

my $diff_plus_9ns = $diff->clone->add(nanoseconds => 9);
is(
$ddp->parse($diff_plus_9ns),
'3y 1m 16d 0h 0m 0s 9ns',
'DateTime::Duration'
);
};
}

Expand Down

0 comments on commit cfe310e

Please sign in to comment.