| 1 | package Data::Printer::Filter::DateTime; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Data::Printer::Filter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | filter 'Time::Piece' => sub { _format($_[0]->cdate , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 0 | ▶ | filter 'Time::Moment' => sub { _format($_[0]->to_string , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 9 | 0 | ▶ | filter 'DateTime::TimeZone' => sub { _format($_[0]->name , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | filter 'DateTime::Incomplete' => sub { _format($_[0]->iso8601 , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | filter 'DateTime::Tiny' => sub { _format($_[0]->as_string , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | filter 'Date' => sub { _format($_[0]->to_string , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | filter 'Date::Tiny' => sub { _format($_[0]->as_string , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | filter 'Date::Calc::Object' => sub { _format($_[0]->string(2) , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | filter 'Date::Handler' => sub { _format("$_[0]" , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | filter 'Date::Handler::Delta' => sub { _format($_[0]->AsScalar , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | filter 'Date::Simple' => sub { _format("$_[0]" , @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | filter 'Date::Manip::Obj' => sub { _format(scalar $_[0]->value, @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | filter 'Mojo::Date' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | my $date = $_[0]->can('to_datetime') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | ? $_[0]->to_datetime | ||||||||||||||||||||||||||||
| 23 | : $_[0]->to_string | ||||||||||||||||||||||||||||
| 24 | ; | ||||||||||||||||||||||||||||
| 25 | 0 | return _format($date , @_); | |||||||||||||||||||||||||||
| 26 | 1 | }; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | filter 'Class::Date::Rel' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||
| 29 | 0 | my ($obj, $ddp) = @_; | |||||||||||||||||||||||||||
| 30 | 0 | my $string = ''; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if (my $months = $obj->mon_part) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if (my $years = int($months / 12)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $string .= $years . 'Y'; | |||||||||||||||||||||||||||
| 34 | 0 | $months -= $years * 12; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ($months) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $string .= (length($string) ? ' ' : '') . $months . 'M'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if (my $seconds = $obj->sec_part) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | my $minutes = int($seconds / 60); | |||||||||||||||||||||||||||
| 42 | 0 | my $hours = int($minutes / 60); | |||||||||||||||||||||||||||
| 43 | 0 | my $days = int($hours / 24); | |||||||||||||||||||||||||||
| 44 | 0 | my $delta = 0; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($days) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $string .= (length($string) ? ' ' : '') . $days . 'D'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | $delta = $days * 24; | |||||||||||||||||||||||||||
| 48 | 0 | $hours -= $delta; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ($hours) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $string .= (length($string) ? ' ' : '') . $hours . 'h'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | $delta = $delta * 60 + $hours * 60; | |||||||||||||||||||||||||||
| 53 | 0 | $minutes -= $delta; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($minutes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $string .= (length($string) ? ' ' : '') . $minutes . 'm'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $delta = $delta * 60 + $minutes * 60; | |||||||||||||||||||||||||||
| 58 | 0 | $seconds -= $delta; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($seconds) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $string .= (length($string) ? ' ' : '') . $seconds . 's'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | return _format( $string, @_ ); | |||||||||||||||||||||||||||
| 65 | 1 | }; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | filter 'DateTime', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($obj, $ddp) = @_; | |||||||||||||||||||||||||||
| 69 | 0 | my $string = "$obj"; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (!exists $ddp->extra_config->{filter_datetime}{show_timezone} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"} or $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
MC/DC0%0/2
not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"} or $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
$ddp->extra_config->{"filter_datetime"}{"show_timezone"}
Truth table0%0/3
not(A) or B
A= not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"}B= $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
| |||||||||||||||||||||||||||||
| 71 | || $ddp->extra_config->{filter_datetime}{show_timezone} | ||||||||||||||||||||||||||||
| 72 | ) { | ||||||||||||||||||||||||||||
| 73 | 0 | $string .= ' ' . $ddp->maybe_colorize('[', 'brackets') | |||||||||||||||||||||||||||
| 74 | . $obj->time_zone->name | ||||||||||||||||||||||||||||
| 75 | . $ddp->maybe_colorize(']', 'brackets'); | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | return _format( $string, @_ ); | |||||||||||||||||||||||||||
| 78 | 1 | }; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | filter 'DateTime::Duration', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($obj, $ddp) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my @dur = $obj->in_units(qw(years months days hours minutes seconds)); | |||||||||||||||||||||||||||
| 84 | 0 | my $string = "$dur[0]y $dur[1]m $dur[2]d $dur[3]h $dur[4]m $dur[5]s"; | |||||||||||||||||||||||||||
| 85 | 0 | return _format( $string, @_ ); | |||||||||||||||||||||||||||
| 86 | 1 | }; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | filter 'Class::Date', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 89 | 0 | my ($obj, $ddp) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $string = $obj->strftime("%Y-%m-%d %H:%M:%S"); | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (!exists $ddp->extra_config->{filter_datetime}{show_timezone} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"} or $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
MC/DC0%0/2
not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"} or $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
$ddp->extra_config->{"filter_datetime"}{"show_timezone"}
Truth table0%0/3
not(A) or B
A= not exists $ddp->extra_config->{"filter_datetime"}{"show_timezone"}B= $ddp->extra_config->{"filter_datetime"}{"show_timezone"}
| |||||||||||||||||||||||||||||
| 93 | || $ddp->extra_config->{filter_datetime}{show_timezone} | ||||||||||||||||||||||||||||
| 94 | ) { | ||||||||||||||||||||||||||||
| 95 | 0 | $string .= ' ' . $ddp->maybe_colorize('[', 'brackets') | |||||||||||||||||||||||||||
| 96 | . $obj->tzdst | ||||||||||||||||||||||||||||
| 97 | . $ddp->maybe_colorize(']', 'brackets'); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 0 | return _format( $string, @_ ); | |||||||||||||||||||||||||||
| 100 | 1 | }; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | sub _time_seconds_formatter { | ||||||||||||||||||||||||||
|
Subroutine
_time_seconds_formatter: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 103 | 1 | my ($n, $counted) = @_; | |||||||||||||||||||||||||||
| 104 | 1 | my $number = sprintf("%d", $n); # does a "floor" | |||||||||||||||||||||||||||
| 105 | 1 | ▶ | $counted .= 's' if 1 != $number; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 1 | return ($number, $counted); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 2 | ▶ | filter 'Time::Seconds', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=26.6
| |||||||||||||||||||||||||||||
| 109 | 2 | my ($obj, $ddp) = @_; | |||||||||||||||||||||||||||
| 110 | 2 | my $str = ''; | |||||||||||||||||||||||||||
| 111 | 2 | ▶ | if ($obj->can('pretty')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | $str = $obj->pretty; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | # simple pretty() implementation: | ||||||||||||||||||||||||||||
| 116 | 1 | ▶ | if ($obj < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | $obj = -$obj; | |||||||||||||||||||||||||||
| 118 | 0 | $str = 'minus '; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 1 | ▶ | if ($obj >= 60) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ($obj >= 3600) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($obj >= 86400) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($days, $sd) = _time_seconds_formatter($obj->days, "day"); | |||||||||||||||||||||||||||
| 124 | 0 | $str .= "$days $sd, "; | |||||||||||||||||||||||||||
| 125 | 0 | $obj -= ($days * 86400); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | my ($hours, $sh) = _time_seconds_formatter($obj->hours, "hour"); | |||||||||||||||||||||||||||
| 128 | 0 | $str .= "$hours $sh, "; | |||||||||||||||||||||||||||
| 129 | 0 | $obj -= ($hours * 3600); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 0 | my ($mins, $sm) = _time_seconds_formatter($obj->minutes, "minute"); | |||||||||||||||||||||||||||
| 132 | 0 | $str .= "$mins $sm, "; | |||||||||||||||||||||||||||
| 133 | 0 | $obj -= ($mins * 60); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 1 | $str .= join ' ', _time_seconds_formatter($obj->seconds, "second"); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 2 | return _format($str, $obj, $ddp); | |||||||||||||||||||||||||||
| 138 | 1 | }; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 5 | ▶ | sub _format { | ||||||||||||||||||||||||||
|
Subroutine
_format: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 141 | 5 | my ($str, $obj, $ddp) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 5 | ▶ | if ($ddp->extra_config->{filter_datetime}{show_class_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 1 | $str .= ' ' . $ddp->maybe_colorize('(', 'brackets') | |||||||||||||||||||||||||||
| 145 | . Scalar::Util::blessed($obj) | ||||||||||||||||||||||||||||
| 146 | . $ddp->maybe_colorize(')', 'brackets'); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 5 | return $ddp->maybe_colorize($str, 'datetime', '#aaffaa'); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | 1; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||