| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojar::Cron::Datetime; |
|
2
|
8
|
|
|
8
|
|
22031
|
use Mojo::Base -strict; |
|
|
8
|
|
|
|
|
17
|
|
|
|
8
|
|
|
|
|
53
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = 0.101; |
|
5
|
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
983
|
use Carp qw(carp croak); |
|
|
8
|
|
|
|
|
19
|
|
|
|
8
|
|
|
|
|
373
|
|
|
7
|
8
|
|
|
8
|
|
3232
|
use Mojar::ClassShare 'have'; |
|
|
8
|
|
|
|
|
3678
|
|
|
|
8
|
|
|
|
|
53
|
|
|
8
|
8
|
|
|
|
|
626
|
use Mojar::Cron::Util qw(balance life_to_zero normalise_local normalise_utc |
|
9
|
8
|
|
|
8
|
|
2553
|
time_to_zero zero_to_time utc_to_ts local_to_ts); |
|
|
8
|
|
|
|
|
22
|
|
|
10
|
8
|
|
|
8
|
|
54
|
use POSIX 'strftime'; |
|
|
8
|
|
|
|
|
17
|
|
|
|
8
|
|
|
|
|
45
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @TimeFields = qw(sec min hour day month year); |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Normal maxima (soft limits) |
|
15
|
|
|
|
|
|
|
%Mojar::Cron::Datetime::Max = ( |
|
16
|
|
|
|
|
|
|
sec => 59, |
|
17
|
|
|
|
|
|
|
min => 59, |
|
18
|
|
|
|
|
|
|
hour => 23, |
|
19
|
|
|
|
|
|
|
day => 30, |
|
20
|
|
|
|
|
|
|
month => 11, |
|
21
|
|
|
|
|
|
|
weekday => 6 |
|
22
|
|
|
|
|
|
|
); |
|
23
|
|
|
|
|
|
|
@Mojar::Cron::Datetime::Max = |
|
24
|
|
|
|
|
|
|
@Mojar::Cron::Datetime::Max{qw(sec min hour day month weekday)}; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Class attributes |
|
27
|
|
|
|
|
|
|
# (not usable on objects) |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Constructors |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
|
32
|
256
|
|
|
256
|
1
|
4003
|
my $class = shift; |
|
33
|
256
|
|
|
|
|
360
|
my $self; |
|
34
|
256
|
100
|
|
|
|
712
|
if (ref $class) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Clone |
|
36
|
71
|
|
|
|
|
151
|
$self = [ @$class ]; |
|
37
|
71
|
|
|
|
|
154
|
$class = ref $class; |
|
38
|
71
|
50
|
|
|
|
152
|
carp sprintf 'Useless arguments to new (%s)', join ',', @_ if @_; |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
elsif (@_ == 0) { |
|
41
|
|
|
|
|
|
|
# Zero member |
|
42
|
1
|
|
|
|
|
4
|
$self = [0,0,0, 0,0,0]; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
elsif (@_ == 1) { |
|
45
|
|
|
|
|
|
|
# Pre-generated |
|
46
|
1
|
50
|
|
|
|
5
|
croak "Non-ref argument to new ($self)" unless ref($self = shift); |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
else { |
|
49
|
183
|
|
|
|
|
392
|
$self = [ @_ ]; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
256
|
|
|
|
|
438
|
bless $self => $class; |
|
52
|
256
|
|
|
|
|
488
|
return $self->normalise; # Calculate weekday etc |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub from_string { |
|
56
|
10
|
|
|
10
|
1
|
8630
|
my ($class, $iso_date) = @_; |
|
57
|
10
|
|
66
|
|
|
50
|
$class = ref $class || $class; |
|
58
|
10
|
50
|
|
|
|
76
|
if ($iso_date |
|
59
|
|
|
|
|
|
|
=~ /^(\d{4})-(\d{2})-(\d{2})(?:T|\s)(\d{2}):(\d{2}):(\d{2})Z?$/) { |
|
60
|
10
|
|
|
|
|
50
|
return $class->new(life_to_zero($6, $5, $4, $3, $2, $1)); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
0
|
|
|
|
|
0
|
croak "Failed to parse datetime string ($iso_date)"; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub from_timestamp { |
|
66
|
171
|
|
|
171
|
0
|
2533
|
my ($class, $timestamp, $is_local) = @_; |
|
67
|
171
|
|
33
|
|
|
612
|
$class = ref $class || $class; |
|
68
|
171
|
100
|
|
|
|
631
|
my @parts = $is_local ? localtime $timestamp |
|
69
|
|
|
|
|
|
|
: gmtime $timestamp; |
|
70
|
171
|
|
|
|
|
482
|
return $class->new( time_to_zero @parts ); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
3
|
|
|
3
|
1
|
68
|
sub now { shift->from_timestamp(time, @_) } |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Public methods |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub copy { |
|
78
|
34
|
|
|
34
|
1
|
78
|
my ($self, $original) = @_; |
|
79
|
34
|
50
|
|
|
|
83
|
return unless ref $original; |
|
80
|
34
|
50
|
|
|
|
71
|
return $self->clone(@_) unless ref $self; |
|
81
|
34
|
|
|
|
|
73
|
@$self = @$original; |
|
82
|
34
|
|
|
|
|
65
|
return $self; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub reset_parts { |
|
86
|
354
|
|
|
354
|
0
|
584
|
my ($self, $end) = @_; |
|
87
|
354
|
|
|
|
|
786
|
$$self[$_] = 0 for 0 .. $end; |
|
88
|
354
|
|
|
|
|
645
|
return $self; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub weekday { |
|
92
|
176
|
|
|
176
|
0
|
267
|
my $self = shift; |
|
93
|
176
|
|
|
|
|
348
|
return +($self->normalise(@$self))[6]; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub normalise { |
|
97
|
1330
|
|
|
1330
|
1
|
1899
|
my $self = shift; |
|
98
|
1330
|
100
|
|
|
|
3356
|
my @parts = @_ ? @_ : @$self; |
|
99
|
1330
|
|
|
|
|
2903
|
@parts = time_to_zero normalise_utc zero_to_time @parts; |
|
100
|
1330
|
100
|
|
|
|
3778
|
return @parts if @_; # operating on argument |
|
101
|
|
|
|
|
|
|
|
|
102
|
1154
|
|
|
|
|
2347
|
@$self = @parts; # operating on invocant |
|
103
|
1154
|
|
|
|
|
2636
|
return $self; |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub to_timestamp { |
|
107
|
154
|
|
|
154
|
0
|
653
|
my ($self, $is_local) = @_; |
|
108
|
154
|
50
|
|
|
|
425
|
return $is_local ? local_to_ts zero_to_time @$self |
|
109
|
|
|
|
|
|
|
: utc_to_ts zero_to_time @$self; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub to_string { |
|
113
|
95
|
|
|
95
|
1
|
175
|
my $self = shift; |
|
114
|
95
|
50
|
66
|
|
|
248
|
$self = shift if @_ and ref $_[0]; |
|
115
|
95
|
|
100
|
|
|
400
|
return strftime pop || '%Y-%m-%d %H:%M:%S', zero_to_time @$self; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
|
119
|
|
|
|
|
|
|
__END__ |