Skip to content

Commit

Permalink
Merge pull request #195 from rjbs/datetime-nanosecond
Browse files Browse the repository at this point in the history
DateTime filter: include nanoseconds, when nonzero
  • Loading branch information
garu authored Jul 30, 2024
2 parents 67b61a7 + 4646ab9 commit 379f0ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 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
13 changes: 10 additions & 3 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 @@ -75,7 +75,7 @@ sub test_time_piece {

sub test_datetime {
SKIP: {
skip 'DateTime not available', 3 unless eval 'use DateTime; 1';
skip 'DateTime not available', 4 unless eval 'use DateTime; 1';
my $d1 = DateTime->new(
year => 1981,
month => 9,
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 379f0ce

Please sign in to comment.