Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime filter: include nanoseconds, when nonzero #195

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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