line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojar::Cron::Util; |
2
|
8
|
|
|
8
|
|
35586
|
use Mojo::Base -strict; |
|
8
|
|
|
|
|
19
|
|
|
8
|
|
|
|
|
49
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = 0.051; |
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
1011
|
use Carp 'croak'; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
332
|
|
7
|
8
|
|
|
8
|
|
49
|
use Exporter 'import'; |
|
8
|
|
|
|
|
21
|
|
|
8
|
|
|
|
|
276
|
|
8
|
8
|
|
|
8
|
|
3349
|
use POSIX qw(mktime strftime); |
|
8
|
|
|
|
|
45602
|
|
|
8
|
|
|
|
|
47
|
|
9
|
8
|
|
|
8
|
|
13788
|
use Time::Local 'timegm'; |
|
8
|
|
|
|
|
10744
|
|
|
8
|
|
|
|
|
9439
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
12
|
|
|
|
|
|
|
time_to_zero zero_to_time cron_to_zero zero_to_cron life_to_zero zero_to_life |
13
|
|
|
|
|
|
|
balance normalise_utc normalise_local date_today date_next date_previous |
14
|
|
|
|
|
|
|
date_dow utc_to_ts local_to_ts ts_to_utc ts_to_local local_to_utc utc_to_local |
15
|
|
|
|
|
|
|
tz_offset |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Public functions |
19
|
|
|
|
|
|
|
|
20
|
1504
|
|
|
1504
|
1
|
5266
|
sub time_to_zero { @_[0..2], $_[3] - 1, @_[4..$#_] } |
21
|
1582
|
|
|
1582
|
1
|
8378
|
sub zero_to_time { @_[0..2], $_[3] + 1, @_[4..$#_] } |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
0
|
1
|
0
|
sub cron_to_zero { @_[0..2], $_[3] - 1, $_[4] - 1, @_[5..$#_] } |
24
|
0
|
|
|
0
|
1
|
0
|
sub zero_to_cron { @_[0..2], $_[3] + 1, $_[4] + 1, @_[5..$#_] } |
25
|
|
|
|
|
|
|
|
26
|
11
|
|
|
11
|
1
|
2203
|
sub life_to_zero { @_[0..2], $_[3] - 1, $_[4] - 1, $_[5] - 1900, @_[6..$#_] } |
27
|
1
|
|
|
1
|
1
|
15
|
sub zero_to_life { @_[0..2], $_[3] + 1, $_[4] + 1, $_[5] + 1900, @_[6..$#_] } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub balance { |
30
|
1332
|
|
|
1332
|
1
|
2542
|
my @parts = @_; |
31
|
1332
|
|
|
|
|
2300
|
my @Max = (59, 59, 23, undef, 11); |
32
|
|
|
|
|
|
|
# Bring values within range for sec, min, hour, month (zero-based) |
33
|
1332
|
|
|
|
|
2123
|
for (0,1,2,4) { |
34
|
5328
|
|
|
|
|
10099
|
$parts[$_] += $Max[$_] + 1, --$parts[$_ + 1] while $parts[$_] < 0; |
35
|
5328
|
|
|
|
|
10686
|
$parts[$_] -= $Max[$_] + 1, ++$parts[$_ + 1] while $parts[$_] > $Max[$_]; |
36
|
|
|
|
|
|
|
} |
37
|
1332
|
|
|
|
|
3367
|
return @parts; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub normalise_utc { |
41
|
1332
|
|
|
1332
|
1
|
2351
|
my @parts = balance @_; |
42
|
1332
|
|
|
|
|
2273
|
my $days = $parts[3] - 1; # could be negative |
43
|
1332
|
|
|
|
|
3244
|
my $ts = timegm @parts[0..2], 1, @parts[4..$#parts]; # first of the month |
44
|
1332
|
|
|
|
|
29879
|
$ts += $days * 24 * 60 * 60; |
45
|
1332
|
|
|
|
|
4121
|
return gmtime $ts; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub normalise_local { |
49
|
0
|
|
|
0
|
1
|
0
|
my @parts = balance @_; |
50
|
0
|
|
|
|
|
0
|
my $days = 0; |
51
|
0
|
0
|
0
|
|
|
0
|
if ($parts[3] < 1 or 28 < $parts[3] && $parts[4] == 1 or 30 < $parts[3]) { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
52
|
0
|
|
|
|
|
0
|
$days = $parts[3] - 1; # possibly negative |
53
|
0
|
|
|
|
|
0
|
$parts[3] = 1; |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
0
|
my $ts = mktime @parts; |
56
|
0
|
|
|
|
|
0
|
$ts += $days * 24 * 60 * 60; |
57
|
0
|
|
|
|
|
0
|
return localtime $ts; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
3
|
|
|
3
|
1
|
2159
|
sub date_today { strftime '%Y-%m-%d', localtime } |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub date_next { |
63
|
3
|
50
|
|
3
|
1
|
192
|
strftime '%Y-%m-%d', 0,0,0, $3 + 1, $2 - 1, $1 - 1900 |
64
|
|
|
|
|
|
|
if shift =~ /^(\d{4})-(\d{2})-(\d{2})\b/; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub date_previous { |
68
|
0
|
0
|
|
0
|
1
|
0
|
strftime '%Y-%m-%d', 0,0,0, $3 - 1, $2 - 1, $1 - 1900 |
69
|
|
|
|
|
|
|
if shift =~ /^(\d{4})-(\d{2})-(\d{2})\b/; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub date_dow { |
73
|
0
|
0
|
|
0
|
0
|
0
|
strftime '%u', 0,0,0, $3 + 1, $2 - 1, $1 - 1900 |
74
|
|
|
|
|
|
|
if shift =~ /^(\d{4})-(\d{2})-(\d{2})\b/; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
155
|
|
|
155
|
1
|
321
|
sub utc_to_ts { timegm @_ } |
78
|
0
|
|
|
0
|
1
|
0
|
sub local_to_ts { mktime @_ } |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
0
|
1
|
0
|
sub ts_to_utc { gmtime $_[0] } |
81
|
1
|
|
|
1
|
1
|
445
|
sub ts_to_local { localtime $_[0] } |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
1
|
1
|
54
|
sub local_to_utc { gmtime mktime @_ } |
84
|
0
|
|
|
0
|
1
|
0
|
sub utc_to_local { localtime timegm @_ } |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my %UnitFactor = ( |
87
|
|
|
|
|
|
|
S => 1, |
88
|
|
|
|
|
|
|
M => 60, |
89
|
|
|
|
|
|
|
H => 60 * 60, |
90
|
|
|
|
|
|
|
d => 60 * 60 * 24, |
91
|
|
|
|
|
|
|
w => 60 * 60 * 24 * 7, |
92
|
|
|
|
|
|
|
m => 60 * 60 * 24 * 30, |
93
|
|
|
|
|
|
|
y => 60 * 60 * 24 * 365 |
94
|
|
|
|
|
|
|
); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub str_to_delta { |
97
|
0
|
|
|
0
|
1
|
0
|
my ($str) = @_; |
98
|
0
|
0
|
|
|
|
0
|
return 0 unless $str; |
99
|
0
|
0
|
|
|
|
0
|
return $str if $str =~ /^[-+]?\d+S?$/; |
100
|
0
|
0
|
|
|
|
0
|
return $1 * $UnitFactor{$2} if $str =~ /^([-+]?\d+)([MHdwmy])$/; |
101
|
0
|
|
|
|
|
0
|
croak qq{Failed to interpret time period ($str)}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub tz_offset { |
105
|
6
|
|
66
|
6
|
1
|
3312
|
my $now = shift // time; |
106
|
6
|
|
|
|
|
196
|
my ($lm, $lh, $ly, $ld) = (localtime $now)[1, 2, 5, 7]; |
107
|
6
|
|
|
|
|
30
|
my ($um, $uh, $uy, $ud) = (gmtime $now)[1, 2, 5, 7]; |
108
|
6
|
|
33
|
|
|
35
|
my $min = $lm - $um + 60 * ($lh - $uh) + 60 * 24 * ($ly - $uy || $ld - $ud); |
109
|
6
|
|
|
|
|
13
|
return _format_offset($min); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Private function |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# This is simply to aid unit testing |
115
|
|
|
|
|
|
|
sub _format_offset { |
116
|
10
|
|
|
10
|
|
1740
|
my $min = shift; |
117
|
10
|
100
|
|
|
|
27
|
my $sign = $min < 0 ? '-' : '+'; |
118
|
10
|
|
|
|
|
17
|
$min = abs $min; |
119
|
10
|
|
|
|
|
27
|
my $hr = int(($min + 0.5) / 60); |
120
|
10
|
|
|
|
|
16
|
$min = $min - 60 * $hr; |
121
|
10
|
|
|
|
|
79
|
return sprintf '%s%02u%02u', $sign, abs($hr), abs($min); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
__END__ |