line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Time::Piece::Plus; |
2
|
13
|
|
|
13
|
|
376743
|
use strict; |
|
13
|
|
|
|
|
36
|
|
|
13
|
|
|
|
|
500
|
|
3
|
13
|
|
|
13
|
|
66
|
use warnings; |
|
13
|
|
|
|
|
26
|
|
|
13
|
|
|
|
|
346
|
|
4
|
13
|
|
|
13
|
|
486
|
use 5.010_001; |
|
13
|
|
|
|
|
46
|
|
|
13
|
|
|
|
|
868
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
7
|
|
|
|
|
|
|
|
8
|
13
|
|
|
13
|
|
11528
|
use parent qw/Time::Piece/; |
|
13
|
|
|
|
|
4284
|
|
|
13
|
|
|
|
|
65
|
|
9
|
|
|
|
|
|
|
|
10
|
13
|
|
|
13
|
|
252202
|
use Scalar::Util (); |
|
13
|
|
|
|
|
34
|
|
|
13
|
|
|
|
|
1469
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub import { |
13
|
12
|
|
|
12
|
|
124
|
my $class = shift; |
14
|
12
|
|
|
|
|
40
|
my $caller = caller; |
15
|
12
|
|
|
|
|
35
|
for my $method (qw(localtime gmtime)) { |
16
|
|
|
|
|
|
|
my $code = sub { |
17
|
18
|
100
|
66
|
18
|
|
24185
|
my $invocant = $_[0] && Scalar::Util::blessed($_[0]) ? shift : $class; |
18
|
18
|
|
|
|
|
458
|
$invocant->$method(@_) |
19
|
24
|
|
|
|
|
97
|
}; |
20
|
|
|
|
|
|
|
{ |
21
|
13
|
|
|
13
|
|
68
|
no strict 'refs'; |
|
13
|
|
|
|
|
40
|
|
|
13
|
|
|
|
|
989
|
|
|
24
|
|
|
|
|
38
|
|
22
|
24
|
|
|
|
|
36
|
*{"$caller\::$method"} = $code; ## no critic |
|
24
|
|
|
|
|
9501
|
|
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
13
|
|
|
13
|
|
71
|
use Time::Seconds; |
|
13
|
|
|
|
|
31
|
|
|
13
|
|
|
|
|
1142
|
|
28
|
13
|
|
|
13
|
|
12443
|
use Data::Validator; |
|
13
|
|
|
|
|
505161
|
|
|
13
|
|
|
|
|
6368
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub get_object { |
31
|
19
|
|
|
19
|
0
|
34
|
my $invocant = shift; |
32
|
|
|
|
|
|
|
|
33
|
19
|
100
|
|
|
|
91
|
my $object = ref $invocant ? $invocant : $invocant->localtime; |
34
|
19
|
|
|
|
|
403
|
return $object; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub reparse { |
38
|
5
|
|
|
5
|
|
216
|
state $validator = Data::Validator->new( |
39
|
|
|
|
|
|
|
format_string => {isa => 'Str'}, |
40
|
|
|
|
|
|
|
parse_string => {isa => 'Str', default => sub{$_[2]->{format_string}}}, |
41
|
5
|
|
|
5
|
0
|
25
|
)->with(qw(Method)); |
42
|
5
|
|
|
|
|
610
|
my ($self, $args) = $validator->validate(@_); |
43
|
|
|
|
|
|
|
|
44
|
5
|
|
|
|
|
74
|
$self->strptime($self->strftime($args->{format_string}), $args->{parse_string}); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub get_is_local { |
48
|
6
|
|
|
6
|
0
|
8
|
my $invocant = shift; |
49
|
|
|
|
|
|
|
|
50
|
6
|
50
|
|
|
|
29
|
return ref $invocant ? $invocant->[10] : 1; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub get_time_diff { |
54
|
6
|
|
|
6
|
0
|
9
|
my $self = shift; |
55
|
6
|
|
|
|
|
29
|
return $self->sec + $self->min * 60 + $self->hour * 3600; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub get_method_name { |
59
|
6
|
|
|
6
|
0
|
11
|
my $invocant = shift; |
60
|
|
|
|
|
|
|
|
61
|
6
|
50
|
|
|
|
20
|
return $invocant->get_is_local ? 'localtime' : 'gmtime'; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub yesterday { |
65
|
2
|
|
|
2
|
1
|
520
|
my $invocant = shift; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
6
|
my $self = $invocant->get_object; |
68
|
2
|
|
|
|
|
14
|
my $epoch = $self->epoch; |
69
|
2
|
|
|
|
|
12
|
my $method = $self->get_method_name; |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
6
|
$self->$method($epoch - ONE_DAY - $self->get_time_diff); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub tomorrow { |
75
|
2
|
|
|
2
|
1
|
387
|
my $invocant = shift; |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
8
|
my $self = $invocant->get_object; |
78
|
2
|
|
|
|
|
8
|
my $epoch = $self->epoch; |
79
|
2
|
|
|
|
|
13
|
my $method = $self->get_method_name; |
80
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
12
|
$invocant->$method($epoch + ONE_DAY - $self->get_time_diff); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub today { |
85
|
2
|
|
|
2
|
1
|
197
|
my $invocant = shift; |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
8
|
my $self = $invocant->get_object; |
88
|
2
|
|
|
|
|
20
|
my $epoch = $self->epoch; |
89
|
2
|
|
|
|
|
138
|
my $method = $self->get_method_name; |
90
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
7
|
$invocant->$method($epoch - $self->get_time_diff); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my %TRUNCATE_FORMAT = ( |
95
|
|
|
|
|
|
|
minute => '%Y%m%d%H%M00', |
96
|
|
|
|
|
|
|
hour => '%Y%m%d%H0000', |
97
|
|
|
|
|
|
|
day => '%Y%m%d000000', |
98
|
|
|
|
|
|
|
month => '%Y%m01000000', |
99
|
|
|
|
|
|
|
year => '%Y0101000000', |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
|
102
|
13
|
|
|
13
|
|
256
|
use Mouse::Util::TypeConstraints; |
|
13
|
|
|
|
|
29
|
|
|
13
|
|
|
|
|
101
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
enum 'Time::Piece::Plus::ColumTypes' => keys %TRUNCATE_FORMAT; |
105
|
|
|
|
|
|
|
|
106
|
13
|
|
|
13
|
|
1314
|
no Mouse::Util::TypeConstraints; |
|
13
|
|
|
|
|
32
|
|
|
13
|
|
|
|
|
67
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub truncate { |
109
|
5
|
|
|
5
|
1
|
14656
|
state $validator = Data::Validator->new( |
110
|
|
|
|
|
|
|
to => {isa => 'Time::Piece::Plus::ColumTypes'}, |
111
|
|
|
|
|
|
|
)->with(qw(Method)); |
112
|
5
|
|
|
|
|
6591
|
my ($self, $args) = $validator->validate(@_); |
113
|
5
|
|
|
|
|
319
|
my $format = $TRUNCATE_FORMAT{$args->{to}}; |
114
|
5
|
|
|
|
|
15
|
$self = $self->get_object; |
115
|
5
|
|
|
|
|
16
|
return $self->reparse(format_string => $format); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub parse_mysql_date { |
119
|
5
|
|
|
5
|
1
|
8267
|
state $validator = Data::Validator->new( |
120
|
|
|
|
|
|
|
str => {isa => 'Str'}, |
121
|
|
|
|
|
|
|
as_localtime => {isa => 'Bool', default => 1}, |
122
|
|
|
|
|
|
|
)->with(qw(Method)); |
123
|
5
|
|
|
|
|
6833
|
my ($class, $args) = $validator->validate(@_); |
124
|
|
|
|
|
|
|
|
125
|
5
|
50
|
|
|
|
354
|
return if $args->{str} eq "0000-00-00"; |
126
|
|
|
|
|
|
|
|
127
|
5
|
100
|
|
|
|
32
|
my $self = $args->{as_localtime} ? $class->localtime() : $class->gmtime(); |
128
|
5
|
|
|
|
|
325
|
my $parsed = $self->strptime($args->{str}, '%Y-%m-%d'); |
129
|
|
|
|
|
|
|
|
130
|
5
|
|
|
|
|
153
|
return $parsed; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub parse_mysql_datetime { |
134
|
5
|
|
|
5
|
1
|
8343
|
state $validator = Data::Validator->new( |
135
|
|
|
|
|
|
|
str => {isa => 'Str'}, |
136
|
|
|
|
|
|
|
as_localtime => {isa => 'Bool', default => 1}, |
137
|
|
|
|
|
|
|
)->with(qw(Method)); |
138
|
5
|
|
|
|
|
8780
|
my ($class, $args) = $validator->validate(@_); |
139
|
|
|
|
|
|
|
|
140
|
5
|
50
|
|
|
|
327
|
return if $args->{str} eq "0000-00-00 00:00:00"; |
141
|
|
|
|
|
|
|
|
142
|
5
|
100
|
|
|
|
31
|
my $self = $args->{as_localtime} ? $class->localtime() : $class->gmtime(); |
143
|
5
|
|
|
|
|
294
|
my $parsed = $self->strptime($args->{str}, '%Y-%m-%d %H:%M:%S'); |
144
|
|
|
|
|
|
|
|
145
|
5
|
|
|
|
|
148
|
return $parsed; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub mysql_date { |
149
|
4
|
|
|
4
|
1
|
1288
|
my ($self, ) = @_; |
150
|
4
|
|
|
|
|
15
|
$self = $self->get_object; |
151
|
4
|
|
|
|
|
35
|
return $self->strftime("%Y-%m-%d"); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
sub mysql_datetime { |
154
|
4
|
|
|
4
|
1
|
890
|
my ($self, ) = @_; |
155
|
4
|
|
|
|
|
19
|
$self = $self->get_object; |
156
|
4
|
|
|
|
|
52
|
return $self->strftime("%Y-%m-%d %H:%M:%S"); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub add { |
160
|
5
|
|
|
5
|
1
|
9214
|
my ($self, @args) = @_; |
161
|
5
|
100
|
|
|
|
35
|
return $self->SUPER::add(@args) if @args <= 1; |
162
|
|
|
|
|
|
|
|
163
|
4
|
|
|
|
|
15
|
my %args = @args; |
164
|
4
|
|
|
|
|
16
|
my $seconds = $self->_calc_seconds(%args); |
165
|
4
|
|
|
|
|
18
|
$self = $self->SUPER::add($seconds); |
166
|
|
|
|
|
|
|
|
167
|
4
|
100
|
|
|
|
243
|
$self = $self->add_months($args{months}) if $args{months}; |
168
|
4
|
100
|
|
|
|
145
|
$self = $self->add_years($args{years}) if $args{years}; |
169
|
|
|
|
|
|
|
|
170
|
4
|
|
|
|
|
109
|
$self; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub subtract { |
174
|
5
|
|
|
5
|
1
|
8166
|
my ($self, @args) = @_; |
175
|
5
|
100
|
|
|
|
26
|
return $self->SUPER::subtract(@args) if @args <= 1; |
176
|
|
|
|
|
|
|
|
177
|
4
|
|
|
|
|
16
|
my %args = @args; |
178
|
4
|
|
|
|
|
13
|
my $seconds = $self->_calc_seconds(%args); |
179
|
4
|
|
|
|
|
14
|
$self = $self->SUPER::subtract($seconds); |
180
|
|
|
|
|
|
|
|
181
|
4
|
100
|
|
|
|
205
|
$self = $self->add_months(-1 * $args{months}) if $args{months}; |
182
|
4
|
100
|
|
|
|
107
|
$self = $self->add_years(-1 * $args{years}) if $args{years}; |
183
|
|
|
|
|
|
|
|
184
|
4
|
|
|
|
|
96
|
$self; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _calc_seconds { |
188
|
8
|
|
|
8
|
|
11
|
my $self = shift; |
189
|
|
|
|
|
|
|
|
190
|
8
|
|
|
|
|
66
|
state $validator = Data::Validator->new( |
191
|
|
|
|
|
|
|
seconds => {isa => 'Int', optional => 1}, |
192
|
|
|
|
|
|
|
minutes => {isa => 'Int', optional => 1}, |
193
|
|
|
|
|
|
|
hours => {isa => 'Int', optional => 1}, |
194
|
|
|
|
|
|
|
days => {isa => 'Int', optional => 1}, |
195
|
|
|
|
|
|
|
months => {isa => 'Int', optional => 1}, |
196
|
|
|
|
|
|
|
years => {isa => 'Int', optional => 1}, |
197
|
|
|
|
|
|
|
); |
198
|
8
|
|
|
|
|
639
|
my $args = $validator->validate(@_); |
199
|
|
|
|
|
|
|
|
200
|
8
|
|
|
|
|
495
|
my $seconds = 0; |
201
|
8
|
100
|
|
|
|
24
|
$seconds += $args->{seconds} if exists $args->{seconds}; |
202
|
8
|
100
|
|
|
|
28
|
$seconds += ONE_MINUTE * $args->{minutes} if exists $args->{minutes}; |
203
|
8
|
100
|
|
|
|
22
|
$seconds += ONE_HOUR * $args->{hours} if exists $args->{hours}; |
204
|
8
|
100
|
|
|
|
22
|
$seconds += ONE_DAY * $args->{days} if exists $args->{days}; |
205
|
|
|
|
|
|
|
|
206
|
8
|
|
|
|
|
20
|
$seconds; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
1; |
211
|
|
|
|
|
|
|
__END__ |