Noss/Timestamp.pm

filter: Statementstmt 100% 40 / 40 Branchbran 68.7% 11 / 16 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 85.9% 67 / 78 CC 13 SCAR 19.6
CC
13
Cov
88%
mail
10.7
rfc3339
4.0
BEGIN
1.0
CRAP 7.1
« Noss/OPML.pm ↑ Summary Noss/Util.pm »
1 package WWW::Noss::Timestamp;
2 6 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 our $VERSION = '2.05';
6  
7 6 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 my %MONTHS = (
10 'jan' => '01',
11 'feb' => '02',
12 'mar' => '03',
13 'apr' => '04',
14 'may' => '05',
15 'jun' => '06',
16 'jul' => '07',
17 'aug' => '08',
18 'sep' => '09',
19 'oct' => '10',
20 'nov' => '11',
21 'dec' => '12',
22 );
23  
24 # Regex taken from the loose parser in the DateTime::Format::Mail module.
25 6 my $mail_rx = qr{
26 ^ \s*
27 # Optional week day name
28 (?i:
29 (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun|[A-Z][a-z][a-z]) ,? # Day name + comma
30 )?
31 \s*
32 (?<dom>\d{1,2}) # Day of month
33 [-\s]*
34 (?i: (?<month> Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ) # month
35 [-\s]*
36 (?<year>(?:\d\d)?\d\d) # year
37 \s+
38 (?<hour>\d?\d):(?<min>\d?\d) (?: :(?<sec>\d?\d) )? # hour:min:sec
39 # Optional time zone
40 (?:
41 \s+ "? (?<tz>
42 [+-] \d{4} # standard
43 | [A-Z]+ # obsolete (ignored)
44 | GMT [+-] \d+ # empirical (converted)
45 | [A-Z]+\d+ # wierd emprical (ignored)
46 | [a-zA-Z/]+ # linux (ignored)
47 | [+-]{0,2} \d{3,5} # corrupted standard
48 ) "?
49 )?
50 (?: \s+ \([^\)]+\) )? # friendly tz name; empirical
51 \s* \.? $
52 }x;
53  
54 # Regex adapted from DateTime::Format::RFC3339.
55 6 my $rfc3339_rx = qr{
56 ^
57 # yyyy-mm-dd
58 (?<year> \d{4})-(?<month> \d{2})-(?<dom> \d{2})
59 T # date/time seperator
60 # hh:mm:ss
61 (?<hour> \d{2}):(?<min> \d{2}):(?<sec> \d{2})
62 # nanoseconds (ignored)
63 (?: \. \d{1,9}\d*)?
64 (?<tz>
65 Z # UTC (zulu)
66 | [+-]\d{2}:\d{2}
67 )
68 $
69 }x;
70  
71 2301 ▶sub mail {
Subroutine mail: called CC=10 SCAR=23.7
Pod documented
72  
73 2301 my ($class, $time) = @_;
74  
75 2301 ▶ $time =~ $mail_rx or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$time =~ /$mail_rx/u or return undef 2301 0 2301
76  
77 2301 my $dom = sprintf "%02d", $+{ dom };
78 2301 my $month = $MONTHS{ lc $+{ month } };
79 my $year =
80 length $+{ year } == 4
81 ? $+{ year }
82 2301 ▶ : $+{ year } >= 69
Branch: 2 branches
BranchTrueFalseTotal
length $+{'year'} == 4 ? : 2299 2 2301
$+{'year'} >= 69 ? : 0 2 2
83 ? "19$+{ year }"
84 : "20$+{ year }";
85 2301 ▶ my $hour = sprintf "%02d", $+{ hour } // 0;
Condition50%1/2
$+{"hour"} // 0
l!l
23010
MC/DC0%0/1
$+{"hour"} // 0
$+{"hour"}
Truth table50%1/2
A
A= $+{"hour"}
Aresult
11
00
86 2301 ▶ my $min = sprintf "%02d", $+{ min } // 0;
Condition50%1/2
$+{"min"} // 0
l!l
23010
MC/DC0%0/1
$+{"min"} // 0
$+{"min"}
Truth table50%1/2
A
A= $+{"min"}
Aresult
11
00
87 2301 ▶ my $sec = sprintf "%02d", $+{ sec } // 0;
Condition100%2/2
$+{"sec"} // 0
l!l
23001
MC/DC100%1/1
$+{"sec"} // 0
$+{"sec"}
Truth table100%2/2
A
A= $+{"sec"}
Aresult
11
00
88 my $tz =
89 2301 ▶ (defined $+{ tz } and $+{ tz } =~ /^([+-])(\d{4})$/)
Branch: 1 branch
BranchTrueFalseTotal
defined $+{'tz'} && $+{'tz'} =~ /^([+-])(\d{4})$/u ? : 2298 3 2301
Condition66%2/3
defined $+{"tz"} and $+{"tz"} =~ /^([+-])(\d{4})$/u
!ll&&!rl&&r
032298
MC/DC50%1/2
defined $+{"tz"} and $+{"tz"} =~ /^([+-])(\d{4})$/u
defined $+{"tz"} $+{"tz"} =~ /^([+-])(\d{4})$/u
Truth table66%2/3
A and B
A= defined $+{"tz"}B= $+{"tz"} =~ /^([+-])(\d{4})$/u
ABresult
0X0
100
111
90 ? $1 . sprintf "%04d", $2
91 : '-0000';
92  
93 2301 my $tp = eval {
94 2301 Time::Piece->strptime(
95 join(' ', $dom, $month, $year, $hour, $min, $sec, $tz),
96 '%d %m %Y %H %M %S %z',
97 );
98 };
99  
100 2301 ▶ return defined $tp ? $tp->epoch : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $tp ? : 2301 0 2301
101  
102 }
103  
104 3713 ▶sub rfc3339 {
Subroutine rfc3339: called CC=4 SCAR=13.9
Pod documented
105  
106 3713 my ($class, $time) = @_;
107  
108 3713 ▶ $time =~ $rfc3339_rx or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$time =~ /$rfc3339_rx/u or return undef 3713 0 3713
109  
110 3713 my $year = $+{ year };
111 3713 my $month = $+{ month };
112 3713 my $dom = $+{ dom };
113 3713 my $hour = $+{ hour };
114 3713 my $min = $+{ min };
115 3713 my $sec = $+{ sec };
116 my $tz =
117 $+{ tz } eq 'Z'
118 ? '-0000'
119 3713 ▶ : $+{ tz } =~ s/://gr;
Branch: 1 branch
BranchTrueFalseTotal
$+{'tz'} eq 'Z' ? : 253 3460 3713
120  
121 3713 my $tp = eval {
122 3713 Time::Piece->strptime(
123 join(' ', $year, $month, $dom, $hour, $min, $sec, $tz),
124 '%Y %m %d %H %M %S %z'
125 );
126 };
127  
128 3713 ▶ return defined $tp ? $tp->epoch : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $tp ? : 3713 0 3713
129  
130 }
131  
132 6 1;
133  
134 =head1 NAME
135  
136 WWW::Noss::Timestamp - Parse timestamps
137  
138 =head1 USAGE
139  
140 use WWW::Noss::Timestamp;
141  
142 my $epoch = WWW::Noss::Timestamp->rfc3339(
143 '2025-07-12T00:23:00Z'
144 );
145  
146 =head1 DESCRIPTION
147  
148 B<WWW::Noss::Timestamp> is a module that provides methods for parsing various
149 timestamp formats used by RSS and Atom feeds. This is a private module, please
150 consult the L<noss(1)> manual for user documentation.
151  
152 =head1 METHODS
153  
154 Each method is invoked as a class method. Methods will return the timestamp's
155 seconds since the Unix epoch or C<undef> on failure.
156  
157 =over 4
158  
159 =item $epoch = WWW::Noss::Timestamp->mail($str)
160  
161 Parse RFC2822/822 timestamps, used by RSS feeds. This is a lenient parser that
162 is capable of parsing some non-standard timestamps.
163  
164 =item $epoch = WWW::Noss::Timestamp->rfc3339($str)
165  
166 Parse RFC3339 timestamps, used by Atom feeds.
167  
168 =back
169  
170 =head1 AUTHOR
171  
172 Written by Samuel Young, E<lt>samyoung@posteo.comE<gt>.
173  
174 This project's source can be found on its
175 L<Codeberg page|https://codeberg.org/1-1sam/noss.git>. Comments and pull
176 requests are welcome!
177  
178 =head1 COPYRIGHT
179  
180 Copyright (C) 2025-2026 Samuel Young
181  
182 This program is free software: you can redistribute it and/or modify
183 it under the terms of the GNU General Public License as published by
184 the Free Software Foundation, either version 3 of the License, or
185 (at your option) any later version.
186  
187 =head1 SEE ALSO
188  
189 L<noss>
190  
191 =cut
192  
193 # vim: expandtab shiftwidth=4
« Noss/OPML.pm ↑ Summary Noss/Util.pm »