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