blib/lib/EBook/Ishmael/Time.pm

filter: Statementstmt 96.9% 64 / 66 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 94.3% 83 / 88 CC 8 SCAR 13.4
CC
8
Cov
93%
guess_time
7.1
__ANON__
2.1
BEGIN
1.0
CRAP 3.8
« blib/lib/EBook/Ishmael/CharDet/Big5.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/zTXT.pm »
1 package EBook::Ishmael::Time;
2 18 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 our $VERSION = '2.04';
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 our @EXPORT = qw(guess_time format_locale_time format_rfc3339_time);
9  
10 18 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 my $WEEKDAY_RX = qr/(?<a>
13 (Sun | Mon | Tue | Wed | Thu | Fri | Sat) |
14 (Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday)
15 )/x;
16 18 my $MONTH_NAME_RX = qr/(?<b>
17 (
18 Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
19 ) |
20 (
21 January | Febuary | March | April | May | June | July | August |
22 September | October | November | December
23 )
24 )/x;
25 18 my $CENTURY_NUM_RX = qr/(?<C>[0-9]?[0-9])/;
26 18 my $MONTH_DAY_RX = qr/(?<d>0?[1-9]|[12][0-9]|3[01])/;
27 18 my $HOUR_24_RX = qr/(?<H>[01]?[0-9]|2[0-3])/;
28 18 my $HOUR_12_RX = qr/(?<I>0?[1-9]|1[0-2])/;
29 18 my $YEAR_DAY_RX = qr/(?<j>0?(0?[1-9]|[1-9]{2})|[1-2][1-9]{2}|3[0-6]{2})/;
30 18 my $MONTH_NUM_RX = qr/(?<m>0?[1-9]|1[0-2])/;
31 18 my $MINUTE_RX = qr/(?<M>[0-5]?[0-9])/;
32 18 my $AM_PM_RX = qr/(?<p>AM|PM>)/i;
33 18 my $SECONDS_RX = qr/(?<S>[0-5]?[0-9]|6[01])/;
34 18 my $WEEK_NUM_RX = qr/(?<U>[0-4]?[0-9]|5[0-3])/;
35 18 my $ORD_WEEK_DAY_RX = qr/(?<w>[0-6])/;
36 18 my $CENTURY_YEAR_RX = qr/(?<y>[0-9]?[0-9])/;
37 18 my $YEAR_RX = qr/(?<Y>[0-9]{1,4})/;
38 18 my $TZ_SPEC_RX = qr/(?<z>Z|[+\-][0-9]{2}:?[0-9]{2})/;
39 18 my $TZ_NAME_RX = qr/(?<Z>[A-Z]{3})/;
40 18 my $EPOCH_RX = qr/(?<s>-?[0-9]+)/;
41 18 my $HHMMSS_RX = qr/$HOUR_24_RX:$MINUTE_RX:$SECONDS_RX/;
42  
43 18 my @DATE_RXS = (
44 # strftime '%c' + '%Z'
45 qr/$WEEKDAY_RX\s+$MONTH_NAME_RX\s+$MONTH_DAY_RX\s+$HHMMSS_RX\s+$YEAR_RX\s+$TZ_NAME_RX/,
46 # strftime '%c' on my system (en_US.UTF-8)
47 qr/$WEEKDAY_RX\s+$MONTH_NAME_RX\s+$MONTH_DAY_RX\s+$HHMMSS_RX\s+$YEAR_RX/,
48 # RFC3339
49 qr/$YEAR_RX-$MONTH_NUM_RX-$MONTH_DAY_RX\x54$HHMMSS_RX$TZ_SPEC_RX/,
50 # RFC822
51 qr/$MONTH_DAY_RX\s+$MONTH_NAME_RX\s+$CENTURY_YEAR_RX\s+$HOUR_24_RX:$MINUTE_RX\s+$TZ_NAME_RX/,
52 qr/$MONTH_DAY_RX\s+$MONTH_NAME_RX\s+$CENTURY_YEAR_RX\s+$HOUR_24_RX:$MINUTE_RX\s+$TZ_SPEC_RX/,
53 # RFC1123
54 qr/$WEEKDAY_RX,\s+$MONTH_DAY_RX\s+$MONTH_NAME_RX\s+$YEAR_RX\s+$HHMMSS_RX\s+$TZ_NAME_RX/,
55 qr/$WEEKDAY_RX,\s+$MONTH_DAY_RX\s+$MONTH_NAME_RX\s+$YEAR_RX\s+$HHMMSS_RX\s+$TZ_SPEC_RX/,
56 # RFC850
57 qr/$WEEKDAY_RX,\s+$MONTH_DAY_RX-$MONTH_NAME_RX-$CENTURY_YEAR_RX\s+$HHMMSS_RX\s+$TZ_NAME_RX/,
58 # output of my date(1)
59 qr/$WEEKDAY_RX\s+$MONTH_NAME_RX\s+$MONTH_DAY_RX\s+$HHMMSS_RX\s+$AM_PM_RX\s+$TZ_NAME_RX\s+$YEAR_RX/,
60 # pdfinfo time format
61 qr/$WEEKDAY_RX\s+$MONTH_NAME_RX\s+$MONTH_DAY_RX\s+$HHMMSS_RX\s+$YEAR_RX\s+$TZ_NAME_RX/,
62 # Ruby date
63 qr/$WEEKDAY_RX\s+$MONTH_NAME_RX\s+$MONTH_DAY_RX\s+$HHMMSS_RX\s+$TZ_SPEC_RX\s+$YEAR_RX/,
64 # misc. date formats
65 qr/$MONTH_DAY_RX\.$MONTH_NUM_RX\.$CENTURY_YEAR_RX/,
66 qr/$MONTH_NUM_RX\.$MONTH_DAY_RX\.$CENTURY_YEAR_RX/,
67 qr/$MONTH_DAY_RX\/$MONTH_NUM_RX\/$CENTURY_YEAR_RX/,
68 qr/$MONTH_NUM_RX\/$MONTH_DAY_RX\/$CENTURY_YEAR_RX/,
69 qr/$MONTH_DAY_RX\.$MONTH_NUM_RX\.$YEAR_RX/,
70 qr/$MONTH_NUM_RX\.$MONTH_DAY_RX\.$YEAR_RX/,
71 qr/$MONTH_DAY_RX\/$MONTH_NUM_RX\/$YEAR_RX/,
72 qr/$MONTH_NUM_RX\/$MONTH_DAY_RX\/$YEAR_RX/,
73 qr/$YEAR_RX-$MONTH_NUM_RX-$MONTH_DAY_RX/,
74 qr/$YEAR_RX/,
75 qr/$EPOCH_RX/,
76 );
77  
78 18 my @FULL_MATCH_DATE_RXS = map { qr/^\s*$_\s*$/ } @DATE_RXS;
79  
80 my %POST_PROCS = (
81 # Some versions of Time::Piece can't handle colons in time zone specs
82 33 ▶ 'z' => sub { $_[0] eq 'Z' ? '+0000' : $_[0] =~ s/://r },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'Z' ? : 0 33 33
83 18 );
84  
85 sub guess_time {
86  
87 54 ▶ my ($str) = @_;
Subroutine guess_time: called CC=7 SCAR=19.6
Pod documented
88  
89 54 my %matches;
90 54 my $found_match = 0;
91 54 for my $rx (@FULL_MATCH_DATE_RXS) {
92 408 ▶ if ($str =~ $rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$rx/u) 54 354 408
93 54 $found_match = 1;
94 54 %matches = %+;
95 54 last;
96 }
97 }
98 54 ▶ if (!$found_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_match) 0 54 54
99 0 die "'$str' does not match any recognized date layout\n";
100 }
101  
102 54 my @codes;
103 my @parts;
104 54 for my $k (keys %matches) {
105 317 push @codes, "%$k";
106 317 ▶ if (exists $POST_PROCS{ $k }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $POST_PROCS{$k}) { } 33 284 317
107 33 push @parts, $POST_PROCS{ $k }->($matches{ $k });
108 } else {
109 284 push @parts, $matches{ $k };
110 }
111 }
112  
113 54 my $tp = eval { Time::Piece->strptime(join(' ', @parts), join(' ', @codes)) };
114 54 ▶ if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 0 54 54
115 0 die "Failed to parse '$str'\n";
116 }
117 54 return $tp->epoch;
118  
119 }
120  
121 sub format_locale_time {
122  
123 13 ▶ my $time = shift;
Subroutine format_locale_time: called CC=1 SCAR=0.0
Pod documented
124  
125 13 return gmtime($time)->strftime("%c");
126  
127 }
128  
129 sub format_rfc3339_time {
130  
131 36 ▶ my $time = shift;
Subroutine format_rfc3339_time: called CC=1 SCAR=0.0
Pod documented
132  
133 36 return gmtime($time)->strftime("%Y-%m-%dT%H:%M:%S%z");
134  
135 }
136  
137 18 1;
138  
139 =head1 NAME
140  
141 EBook::Ishmael::Time - Time-handling subroutines for ishmael
142  
143 =head1 SYNOPSIS
144  
145 use EBook::Ishmael::Time qw(guess_time);
146  
147 my $t = guess_time("01.14.2025");
148  
149 =head1 DESCRIPTION
150  
151 B<EBook::Ishmael::Time> is a module that provides various time-handling
152 subroutines for L<ishmael>. This is a private module, please consult the
153 L<ishmael> manual for user documentation.
154  
155 =head1 SUBROUTINES
156  
157 =over 4
158  
159 =item $epoch = guess_time($str)
160  
161 C<guess_time()> guesses the timestamp format of C<$str> and returns the number
162 seconds since the Unix epoch, or C<undef> if it could not identify the
163 timestamp format.
164  
165 =item $locale = format_locale_time($epoch)
166  
167 Formats the given time in the preferred format of the current locale.
168  
169 =item $rfc3339 = format_rfc3339_time($epoch)
170  
171 Formats the given time as an RFC3339 timestamp.
172  
173 =back
174  
175 =head1 AUTHOR
176  
177 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
178  
179 This project's source can be found on its
180 L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull
181 requests are welcome!
182  
183 =head1 COPYRIGHT
184  
185 Copyright (C) 2025-2026 Samuel Young
186  
187 This program is free software: you can redistribute it and/or modify
188 it under the terms of the GNU General Public License as published by
189 the Free Software Foundation, either version 3 of the License, or
190 (at your option) any later version.
191  
192 =cut
« blib/lib/EBook/Ishmael/CharDet/Big5.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/zTXT.pm »