File Coverage

File:local/lib/perl5/Data/Printer/Filter/DateTime.pm
Coverage:59.3%

linestmtbrancondsubtimecode
1package Data::Printer::Filter::DateTime;
2
2
2
2
5
1
30
use strict;
3
2
2
2
3
2
17
use warnings;
4
2
2
2
4
1
5
use Data::Printer::Filter;
5
2
2
2
4
1
495
use Term::ANSIColor;
6
7filter 'Time::Piece', sub {
8    return _format($_[0]->cdate, @_ );
9};
10
11filter 'DateTime', sub {
12    my ($obj, $p) = @_;
13    my $string = "$obj";
14    if ( not exists $p->{datetime}{show_timezone} or $p->{datetime}{show_timezone} ) {
15        $string .= ' [' . $obj->time_zone->name . ']';
16    }
17    return _format( $string, @_ );
18};
19
20# DateTime::TimeZone filters
21filter '-class' => sub {
22    my ($obj, $properties) = @_;
23
24    if ( $obj->isa('DateTime::TimeZone' ) ) {
25        return $obj->name;
26    }
27    else {
28        return;
29    }
30};
31
32filter 'DateTime::Incomplete', sub {
33    return _format( $_[0]->iso8601, @_ );
34};
35
36filter 'DateTime::Duration', sub {
37    my ($object, $p) = @_;
38
39    my @dur = $object->in_units(
40         qw(years months days hours minutes seconds)
41    );
42
43    my $string = "$dur[0]y $dur[1]m $dur[2]d $dur[3]h $dur[4]m $dur[5]s";
44
45    return _format( $string, @_ );
46};
47
48filter 'DateTime::Tiny', sub {
49    return _format( $_[0]->as_string, @_ );
50};
51
52filter 'Date::Calc::Object', sub {
53    return _format( $_[0]->string(2), @_ );
54};
55
56filter 'Date::Pcalc::Object', sub {
57    return _format( $_[0]->string(2), @_ );
58};
59
60filter 'Date::Handler', sub {
61    return _format( "$_[0]", @_ );
62};
63
64filter 'Date::Handler::Delta', sub {
65    return _format( $_[0]->AsScalar, @_ );
66};
67
68
69sub _format {
70
0
    my ($str, $obj, $p) = @_;
71
72
0
    if ( $p->{datetime}{show_class_name} ) {
73
0
        $str .= ' (' . ref($obj) . ')';
74    }
75
76
0
    my $color = $p->{color}{datetime};
77
0
    $color = 'bright_green' unless defined $color;
78
79
0
    return colored( $str, $color );
80}
81
821;
83