line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cikl::Util::TimeHelpers; |
2
|
1
|
|
|
1
|
|
367805
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
4
|
1
|
|
|
1
|
|
1689
|
use DateTime::Format::DateParse; |
|
1
|
|
|
|
|
13089
|
|
|
1
|
|
|
|
|
41
|
|
5
|
1
|
|
|
1
|
|
1390
|
use DateTime::Format::Strptime; |
|
1
|
|
|
|
|
10443
|
|
|
1
|
|
|
|
|
874
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
8
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
9
|
|
|
|
|
|
|
normalize_timestamp |
10
|
|
|
|
|
|
|
create_strptime_parser |
11
|
|
|
|
|
|
|
create_default_timestamp_parser |
12
|
|
|
|
|
|
|
) ] ); |
13
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
14
|
|
|
|
|
|
|
our @EXPORT = qw//; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Cikl::Util::TimeHelpers - Time helpers for Cikl |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Cikl::Util::TimeHelpers qw/normalize_timestamp/; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $dt = DateTime->now() |
25
|
|
|
|
|
|
|
$dt = normalize_timestamp($dt); |
26
|
|
|
|
|
|
|
warn $dt; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=over |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item normalize_timestamp($ts) |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Take in a timestamp (see DateTime::Format::DateParse), does a little extra normalizing and returns a DateTime object |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub normalize_timestamp { |
37
|
13
|
|
|
13
|
1
|
35793
|
my $dt = shift; |
38
|
13
|
|
66
|
|
|
88
|
my $now = shift || time(); # better perf in loops if we can pass the default now value |
39
|
|
|
|
|
|
|
|
40
|
13
|
50
|
|
|
|
39
|
if (!defined($dt)) { |
41
|
|
|
|
|
|
|
# Default to now. |
42
|
0
|
|
|
|
|
0
|
return $now; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
13
|
100
|
|
|
|
46
|
if(ref($dt) eq 'DateTime'){ |
46
|
1
|
|
|
|
|
10
|
return $dt->epoch(); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# already epoch |
50
|
12
|
100
|
|
|
|
52
|
if($dt =~ /^\d{10}$/) { |
51
|
1
|
|
|
|
|
6
|
return $dt ; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
11
|
100
|
|
|
|
36
|
if($dt =~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/) { |
55
|
1
|
|
|
|
|
17
|
my $ret = DateTime::Format::DateParse->parse_datetime($dt, "UTC"); |
56
|
1
|
50
|
|
|
|
806
|
if ($ret) { |
57
|
1
|
|
|
|
|
53
|
return $ret->epoch(); |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
0
|
return undef; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# something else |
63
|
10
|
100
|
|
|
|
80
|
if($dt =~ /^\d+$/){ |
|
|
100
|
|
|
|
|
|
64
|
5
|
100
|
|
|
|
19
|
if($dt =~ /^\d{8}$/){ |
65
|
3
|
|
|
|
|
8
|
$dt.= 'T00:00:00Z'; |
66
|
3
|
|
|
|
|
4
|
$dt = eval { DateTime::Format::DateParse->parse_datetime($dt, "UTC") }; |
|
3
|
|
|
|
|
20
|
|
67
|
3
|
100
|
|
|
|
2989
|
unless($dt){ |
68
|
2
|
|
|
|
|
7
|
return $now; |
69
|
|
|
|
|
|
|
} |
70
|
1
|
|
|
|
|
55
|
return $dt->epoch(); |
71
|
|
|
|
|
|
|
} else { |
72
|
2
|
|
|
|
|
7
|
return $now; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} elsif($dt =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\S+)?$/) { |
75
|
1
|
|
|
|
|
9
|
my ($year,$month,$day,$hour,$min,$sec,$tz) = ($1,$2,$3,$4,$5,$6,$7); |
76
|
1
|
|
50
|
|
|
14
|
$dt = DateTime::Format::DateParse->parse_datetime($year.'-'.$month.'-'.$day.' '.$hour.':'.$min.':'.$sec,$tz || "UTC"); |
77
|
1
|
|
|
|
|
546
|
return $dt->epoch(); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
4
|
|
|
|
|
13
|
$dt =~ s/_/ /g; |
81
|
4
|
|
|
|
|
30
|
$dt = DateTime::Format::DateParse->parse_datetime($dt, "UTC"); |
82
|
4
|
100
|
|
|
|
2641
|
return undef unless($dt); |
83
|
3
|
|
|
|
|
181
|
return $dt->epoch(); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=back |
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub create_default_timestamp_parser { |
90
|
0
|
|
|
0
|
0
|
0
|
return \&normalize_timestamp; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub create_strptime_parser { |
94
|
1
|
|
|
1
|
0
|
709
|
my $pattern = shift; |
95
|
1
|
|
50
|
|
|
6
|
my $zone = shift || "UTC"; |
96
|
1
|
50
|
|
|
|
14
|
my $parser = DateTime::Format::Strptime->new( |
97
|
|
|
|
|
|
|
pattern => $pattern, |
98
|
|
|
|
|
|
|
time_zone => $zone |
99
|
|
|
|
|
|
|
) or die("Invalid datetime format: '$pattern', zone: '$zone'"); |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
return sub { |
102
|
1
|
|
|
1
|
|
6
|
my $str = shift; |
103
|
1
|
|
33
|
|
|
8
|
my $now = shift || time(); |
104
|
1
|
50
|
|
|
|
8
|
my $t = $parser->parse_datetime($str) or die($!); |
105
|
1
|
|
|
|
|
1082
|
return $t->epoch(); |
106
|
1
|
|
|
|
|
663
|
}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |