line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojar::Cron::Util; |
2
|
8
|
|
|
8
|
|
216172
|
use Mojo::Base -strict; |
|
8
|
|
|
|
|
22
|
|
|
8
|
|
|
|
|
46
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = 0.051; |
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
1002
|
use Carp 'croak'; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
324
|
|
7
|
8
|
|
|
8
|
|
39
|
use Exporter 'import'; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
242
|
|
8
|
8
|
|
|
8
|
|
3443
|
use POSIX qw(mktime strftime); |
|
8
|
|
|
|
|
46100
|
|
|
8
|
|
|
|
|
40
|
|
9
|
8
|
|
|
8
|
|
14821
|
use Time::Local 'timegm'; |
|
8
|
|
|
|
|
15850
|
|
|
8
|
|
|
|
|
12045
|
|
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
|
4310
|
sub time_to_zero { @_[0..2], $_[3] - 1, @_[4..$#_] } |
21
|
1582
|
|
|
1582
|
1
|
6875
|
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
|
2384
|
sub life_to_zero { @_[0..2], $_[3] - 1, $_[4] - 1, $_[5] - 1900, @_[6..$#_] } |
27
|
1
|
|
|
1
|
1
|
17
|
sub zero_to_life { @_[0..2], $_[3] + 1, $_[4] + 1, $_[5] + 1900, @_[6..$#_] } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub balance { |
30
|
1332
|
|
|
1332
|
1
|
1958
|
my @parts = @_; |
31
|
1332
|
|
|
|
|
1701
|
my @Max = (59, 59, 23, undef, 11); |
32
|
|
|
|
|
|
|
# Bring values within range for sec, min, hour, month (zero-based) |
33
|
1332
|
|
|
|
|
1723
|
for (0,1,2,4) { |
34
|
5328
|
|
|
|
|
7400
|
$parts[$_] += $Max[$_] + 1, --$parts[$_ + 1] while $parts[$_] < 0; |
35
|
5328
|
|
|
|
|
7918
|
$parts[$_] -= $Max[$_] + 1, ++$parts[$_ + 1] while $parts[$_] > $Max[$_]; |
36
|
|
|
|
|
|
|
} |
37
|
1332
|
|
|
|
|
2693
|
return @parts; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub normalise_utc { |
41
|
1332
|
|
|
1332
|
1
|
1841
|
my @parts = balance @_; |
42
|
1332
|
|
|
|
|
1634
|
my $days = $parts[3] - 1; # could be negative |
43
|
1332
|
|
|
|
|
2933
|
my $ts = timegm @parts[0..2], 1, @parts[4..$#parts]; # first of the month |
44
|
1332
|
|
|
|
|
24726
|
$ts += $days * 24 * 60 * 60; |
45
|
1332
|
|
|
|
|
3357
|
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
|
2288
|
sub date_today { strftime '%Y-%m-%d', localtime } |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub date_next { |
63
|
3
|
50
|
|
3
|
1
|
137
|
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
|
281
|
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
|
354
|
sub ts_to_local { localtime $_[0] } |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
1
|
1
|
37
|
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
|
4212
|
my $now = shift // time; |
106
|
6
|
|
|
|
|
128
|
my ($lm, $lh, $ly, $ld) = (localtime $now)[1, 2, 5, 7]; |
107
|
6
|
|
|
|
|
29
|
my ($um, $uh, $uy, $ud) = (gmtime $now)[1, 2, 5, 7]; |
108
|
6
|
|
33
|
|
|
28
|
my $min = $lm - $um + 60 * ($lh - $uh) + 60 * 24 * ($ly - $uy or $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
|
|
2107
|
my $min = shift; |
117
|
10
|
100
|
|
|
|
23
|
my $sign = $min < 0 ? '-' : '+'; |
118
|
10
|
|
|
|
|
12
|
$min = abs $min; |
119
|
10
|
|
|
|
|
24
|
my $hr = int(($min + 0.5) / 60); |
120
|
10
|
|
|
|
|
14
|
$min = $min - 60 * $hr; |
121
|
10
|
|
|
|
|
76
|
return sprintf '%s%02u%02u', $sign, abs($hr), abs($min); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
__END__ |