Element/Timestamp.pm

filter: Statementstmt 85.7% 84 / 98 Branchbran 55.0% 33 / 60 Conditioncond 77.7% 21 / 27 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 74.6% 159 / 213 CC 42 SCAR 34.9
CC
42
Cov
75%
as_string
67.4
_parse_timestamp
34.6
clear_parse_result
3.0
CRAP 32.8
↑ Summary Document.pm »
1 package Org::Element::Timestamp;
2  
3 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶no if $] >= 5.021_006, warnings => "locale";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 extends 'Org::Element';
9 13 with 'Org::ElementRole';
10 13 with 'Org::ElementRole::Inline';
11  
12 13 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13 13 our $DATE = '2023-11-06'; # DATE
14 13 our $DIST = 'Org-Parser'; # DIST
15 13 our $VERSION = '0.561'; # VERSION
16  
17 13 my @attrs = (qw/datetime has_time event_duration recurrence is_active/);
18 13 for (@attrs) {
19 65 has $_ => (is => 'rw', clearer=>"clear_$_");
20 before $_ => sub {
21 316 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
22 316 ▶ return unless defined $self->_is_parsed; # never been parsed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_is_parsed 234 82 316
23 82 ▶ $self->_parse_timestamp($self->_str)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_parsed 1 81 82
24 unless $self->_is_parsed; # has been reset, re-set
25 65 };
26 }
27  
28 13 has _repeater => (is => 'rw'); # stores the raw repeater spec, for as_string
29 13 has _warning_period => (is => 'rw'); # raw warning period spec, for as_string
30 13 has _is_parsed => (is => 'rw');
31  
32 sub clear_parse_result {
33 1 ▶ my $self = shift;
Subroutine clear_parse_result: called CC=3 SCAR=11.0
Pod documented
34 1 ▶ return unless defined $self->_is_parsed; # never been parsed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_is_parsed 0 1 1
35 1 for (@attrs) { my $m = "clear_$_"; $self->$m }
36 1 $self->_is_parsed(0);
37 }
38  
39 13 our @dow = (undef, qw(Mon Tue Wed Thu Fri Sat Sun));
40  
41 sub as_string {
42 18 ▶ my ($self) = @_;
Subroutine as_string: called CC=9 SCAR=42.1
Pod documented
43 18 ▶ return $self->_str if $self->_str;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_str 18 0 18
44 0 my $dt = $self->datetime;
45 0 my ($hour2, $min2);
46 0 ▶ if ($self->event_duration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->event_duration) 0 0 0
47 0 my $hour = $dt->hour;
48 0 my $min = $dt->minute;
49 0 my $mins = $self->event_duration / 60;
50 0 $min2 = $min + $mins;
51 0 my $hours = int ($min2 / 60);
52 0 $hour2 = $hour + $hours;
53 0 $min2 = $min2 % 60;
54 }
55 0 ▶ join("",
Branch: 6 branches
BranchTrueFalseTotal
$self->is_active ? : 0 0 0
$self->has_time ? : 0 0 0
defined $hour2 ? : 0 0 0
$self->_repeater ? : 0 0 0
$self->_warning_period ? : 0 0 0
$self->is_active ? : 0 0 0
56 $self->is_active ? "<" : "[",
57 $dt->ymd, " ",
58 $dow[$dt->day_of_week],
59 $self->has_time ? (
60 " ",
61 sprintf("%02d:%02d", $dt->hour, $dt->minute),
62 defined($hour2) ? (
63 "-",
64 sprintf("%02d:%02d", $hour2, $min2),
65 ) : (),
66 $self->_repeater ? (
67 " ",
68 $self->_repeater,
69 ) : (),
70 $self->_warning_period ? (
71 " ",
72 $self->_warning_period,
73 ) : (),
74 ) : (),
75 $self->is_active ? ">" : "]",
76 );
77 }
78  
79 sub as_text {
80 0 ▶ goto \&as_string;
Subroutine as_text: not called CC=1 SCAR=6.9
Pod documented
81 }
82  
83 sub _parse_timestamp {
84 70 ▶ require DateTime;
Subroutine _parse_timestamp: called CC=30 SCAR=35.4
85 70 require DateTime::Event::Recurrence;
86 70 my ($self, $str, $opts) = @_;
87 70 $self->_is_parsed(undef); # to avoid deep recursion
88 70 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
1456
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
89 70 ▶ $opts->{allow_event_duration} //= 1;
Condition100%2/2
$opts->{"allow_event_duration"} //= 1
l!l
1456
MC/DC100%1/1
$opts->{"allow_event_duration"} //= 1
$opts->{"allow_event_duration"}
Truth table100%2/2
A
A= $opts->{"allow_event_duration"}
Aresult
11
00
90 70 ▶ $opts->{allow_repeater} //= 1;
Condition100%2/2
$opts->{"allow_repeater"} //= 1
l!l
1456
MC/DC100%1/1
$opts->{"allow_repeater"} //= 1
$opts->{"allow_repeater"}
Truth table100%2/2
A
A= $opts->{"allow_repeater"}
Aresult
11
00
91  
92 70 my $num_re = qr/\d+(?:\.\d+)?/;
93  
94 70 my $dow_re = qr/\w{1,3} | # common, chinese 四, english thu
95 \w{2,3}\. # french mer., german Mi.
96 /x;
97  
98 70 ▶ $str =~ /^(?<open_bracket> \[|<)
Branch: 1 branch
BranchTrueFalseTotal
$str =~ m(^(?<open_bracket> \[|<) (?<year> \d{4})-(?<mon> \d{2})-(?<day> \d{2}) (?: (?:\s* (?<dow> $dow_re) \s*)? (?:\s+ (?<hour> \d{2}):(?<min> \d{2}) (?:- (?<event_duration> (?<hour2> \d{2}):(?<min2> \d{2})) )? )? (?:\s+(?<repeater> (?<repeater_prefix> \+\+|\.\+|\+) (?<repeater_interval> $num_re) (?<repeater_unit> [dwmy]) (?:/(?<repeater_interval_max> $num_re) (?<repeater_unit_max> [dwmy]))? ) )? (?:\s+(?<warning_period> - (?<warning_period_interval> $num_re) (?<warning_period_unit> [dwmy]) ) )? )? \s* (?<close_bracket> \]|>) $)lx or $self->die("Can't parse timestamp string: $str") 0 70 70
99 (?<year> \d{4})-(?<mon> \d{2})-(?<day> \d{2})
100 (?:
101 (?:\s* (?<dow> $dow_re) \s*)?
102 (?:\s+
103 (?<hour> \d{2}):(?<min> \d{2})
104 (?:-
105 (?<event_duration>
106 (?<hour2> \d{2}):(?<min2> \d{2}))
107 )?
108 )?
109 (?:\s+(?<repeater>
110 (?<repeater_prefix> \+\+|\.\+|\+)
111 (?<repeater_interval> $num_re)
112 (?<repeater_unit> [dwmy])
113 (?:\/(?<repeater_interval_max> $num_re)
114 (?<repeater_unit_max> [dwmy]))?
115 )
116 )?
117 (?:\s+(?<warning_period>
118 -
119 (?<warning_period_interval> $num_re)
120 (?<warning_period_unit> [dwmy])
121 )
122 )?
123 )?
124 \s* (?<close_bracket> \]|>)
125 $/x
126 or $self->die("Can't parse timestamp string: $str");
127 # just for sanity. usually doesn't happen though because Document gives us
128 # either "[...]" or "<...>"
129 $self->die("Mismatch open/close brackets in timestamp: $str")
130 if $+{open_bracket} eq '<' && $+{close_bracket} eq ']' ||
131 70 ▶ $+{open_bracket} eq '[' && $+{close_bracket} eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if $+{"open_bracket"} eq "<" and $+{"close_bracket"} eq "]" or $+{"open_bracket"} eq "[" and $+{"close_bracket"} eq ">" 0 70 70
Condition55%5/9
$+{"open_bracket"} eq "<" and $+{"close_bracket"} eq "]" or $+{"open_bracket"} eq "[" and $+{"close_bracket"} eq ">"
l!l&&r!l&&!r
0070
$+{"open_bracket"} eq "<" and $+{"close_bracket"} eq "]"
!ll&&!rl&&r
33370
$+{"open_bracket"} eq "[" and $+{"close_bracket"} eq ">"
!ll&&!rl&&r
37330
MC/DC0%0/4
$+{"open_bracket"} eq "<" and $+{"close_bracket"} eq "]" or $+{"open_bracket"} eq "[" and $+{"close_bracket"} eq ">"
$+{"open_bracket"} eq "<" $+{"close_bracket"} eq "]" $+{"open_bracket"} eq "[" $+{"close_bracket"} eq ">"
Truth table14%1/7
A and B or C and D
A= $+{"open_bracket"} eq "<"B= $+{"close_bracket"} eq "]"C= $+{"open_bracket"} eq "["D= $+{"close_bracket"} eq ">"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
132 $self->die("Duration not allowed in timestamp: $str")
133 70 ▶ if !$opts->{allow_event_duration} && $+{event_duration};
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"allow_event_duration"} and $+{"event_duration"} 1 69 70
Condition100%3/3
not $opts->{"allow_event_duration"} and $+{"event_duration"}
!ll&&!rl&&r
56131
MC/DC100%2/2
not $opts->{"allow_event_duration"} and $+{"event_duration"}
not $opts->{"allow_event_duration"} $+{"event_duration"}
Truth table100%3/3
not(A) and B
A= not $opts->{"allow_event_duration"}B= $+{"event_duration"}
ABresult
0X0
100
111
134 $self->die("Repeater ($+{repeater}) not allowed in timestamp: $str")
135 69 ▶ if !$opts->{allow_repeater} && $+{repeater};
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"allow_repeater"} and $+{"repeater"} 1 68 69
Condition100%3/3
not $opts->{"allow_repeater"} and $+{"repeater"}
!ll&&!rl&&r
56121
MC/DC100%2/2
not $opts->{"allow_repeater"} and $+{"repeater"}
not $opts->{"allow_repeater"} $+{"repeater"}
Truth table100%3/3
not(A) and B
A= not $opts->{"allow_repeater"}B= $+{"repeater"}
ABresult
0X0
100
111
136  
137 68 ▶ $self->is_active($+{open_bracket} eq '<' ? 1:0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->is_active 68 0 68
$+{'open_bracket'} eq '<' ? : 35 33 68
138 unless defined $self->is_active;
139  
140 68 ▶ if ($+{event_duration} && !defined($self->event_duration)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"event_duration"} and not defined $self->event_duration) 3 65 68
Condition66%2/3
$+{"event_duration"} and not defined $self->event_duration
!ll&&!rl&&r
6503
MC/DC50%1/2
$+{"event_duration"} and not defined $self->event_duration
$+{"event_duration"} not defined $self->event_duration
Truth table66%2/3
A and not(B)
A= $+{"event_duration"}B= not defined $self->event_duration
ABresult
0X0
100
111
141 $self->event_duration(
142 ($+{hour2}-$+{hour})*3600 +
143 3 ($+{min2} -$+{min} )*60
144 );
145 }
146  
147 68 my %dt_args = (year => $+{year}, month=>$+{mon}, day=>$+{day});
148 68 ▶ if (defined($+{hour})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+{'hour'}) { } 20 48 68
149 20 $dt_args{hour} = $+{hour};
150 20 $dt_args{minute} = $+{min};
151 20 $self->has_time(1);
152 } else {
153 48 $self->has_time(0);
154 }
155 68 ▶ if ($self->document->time_zone) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document->time_zone) 1 67 68
156 1 $dt_args{time_zone} = $self->document->time_zone;
157 }
158 #use Data::Dump; dd \%dt_args;
159 68 my $dt = DateTime->new(%dt_args);
160  
161 67 ▶ if ($+{repeater} && !$self->recurrence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"repeater"} and not $self->recurrence) 12 55 67
Condition66%2/3
$+{"repeater"} and not $self->recurrence
!ll&&!rl&&r
55012
MC/DC50%1/2
$+{"repeater"} and not $self->recurrence
$+{"repeater"} not $self->recurrence
Truth table66%2/3
A and not(B)
A= $+{"repeater"}B= not $self->recurrence
ABresult
0X0
100
111
162 12 my $r;
163 12 my $i = $+{repeater_interval};
164 12 my $u = $+{repeater_unit};
165 12 ▶ if ($u eq 'd') {
Branch: 4 branches
BranchTrueFalseTotal
if ($u eq 'd') { } 2 10 12
elsif ($u eq 'w') { } 3 7 10
elsif ($u eq 'm') { } 4 3 7
elsif ($u eq 'y') { } 3 0 3
166 2 $r = DateTime::Event::Recurrence->daily(
167 interval=>$i, start=>$dt);
168 } elsif ($u eq 'w') {
169 3 $r = DateTime::Event::Recurrence->weekly(
170 interval=>$i, start=>$dt);
171 } elsif ($u eq 'm') {
172 4 $r = DateTime::Event::Recurrence->monthly(
173 interval=>$i, start=>$dt);
174 } elsif ($u eq 'y') {
175 3 $r = DateTime::Event::Recurrence->yearly(
176 interval=>$i, start=>$dt);
177 } else {
178 0 $self->die("BUG: Unknown repeater unit $u in timestamp $str");
179 }
180 12 $self->recurrence($r);
181 12 $self->_repeater($+{repeater});
182 }
183  
184 67 ▶ if ($+{warning_period}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"warning_period"}) 1 66 67
185 1 my $i = $+{warning_period_interval};
186 1 my $u = $+{warning_period_unit};
187 1 ▶ if ($u eq 'd') {
Branch: 4 branches
BranchTrueFalseTotal
if ($u eq 'd') { } 1 0 1
elsif ($u eq 'w') { } 0 0 0
elsif ($u eq 'm') { } 0 0 0
elsif ($u eq 'y') { } 0 0 0
188 } elsif ($u eq 'w') {
189 } elsif ($u eq 'm') {
190 } elsif ($u eq 'y') {
191 } else {
192 0 $self->die("BUG: Unknown warning period unit $u in timestamp $str");
193 }
194 1 $self->_warning_period($+{warning_period});
195 }
196  
197 67 $self->_is_parsed(1);
198 67 $self->datetime($dt);
199 }
200  
201 13 1;
202 # ABSTRACT: Represent Org timestamp
203  
204 __END__
205  
206 =pod
207  
208 =encoding UTF-8
209  
210 =head1 NAME
211  
212 Org::Element::Timestamp - Represent Org timestamp
213  
214 =head1 VERSION
215  
216 This document describes version 0.561 of Org::Element::Timestamp (from Perl distribution Org-Parser), released on 2023-11-06.
217  
218 =head1 DESCRIPTION
219  
220 Derived from L<Org::Element>.
221  
222 Supported formats:
223  
224 =over
225  
226 =item * C<[...]> and C<< <...> >> (active) forms
227  
228 =item * basic date: C<[2013-10-27 Sun]>
229  
230 =item * event duration: C<[2011-03-23 Wed 10:12-11:23]>
231  
232 =item * repeater: C<[2011-03-23 Wed +3m]> including C<++> and C<.+>
233  
234 =item * habit-style repeater: C<[2011-03-23 Wed 10:12 +1d/2d]>
235  
236 =item * warning period: C<< <2011-05-25 Wed +17.1m -13.2d> >>
237  
238 =back
239  
240 =head1 BUGS AND LIMITATIONS
241  
242 =over
243  
244 =item * Habit-style repeater (e.g. 2d/3d) is not yet represented in C<recurrence>
245  
246 The recurrence object currently will still only include 2d (without the maximum
247 interval).
248  
249 =back
250  
251 =head1 ATTRIBUTES
252  
253 =head2 datetime => DATETIME_OBJ
254  
255 =head2 has_time => BOOL
256  
257 =head2 event_duration => INT
258  
259 Event duration in seconds, e.g. for event timestamp like this:
260  
261 <2011-03-23 10:15-13:25>
262  
263 event_duration is 7200+600=7800 (2 hours 10 minutes).
264  
265 =head2 recurrence => DateTime::Event::Recurrence object
266  
267 =head2 is_active => BOOL
268  
269 =head1 METHODS
270  
271 =head2 $el->clear_parse_result
272  
273 Clear parse result.
274  
275 Since the DateTime::Set::ICal (recurrence) object contains coderefs (and thus
276 poses problem to serialization), an option is provided to remove parse result.
277 You can do this prior to serializing the object.
278  
279 Timestamp will automatically be parsed again from _str when one of the
280 attributes is accessed.
281  
282 =head2 as_string => str
283  
284 From L<Org::Element>.
285  
286 =head2 as_text => str
287  
288 From L<Org::ElementRole::Inline>.
289  
290 =head1 HOMEPAGE
291  
292 Please visit the project's homepage at L<https://metacpan.org/release/Org-Parser>.
293  
294 =head1 SOURCE
295  
296 Source repository is at L<https://github.com/perlancar/perl-Org-Parser>.
297  
298 =head1 AUTHOR
299  
300 perlancar <perlancar@cpan.org>
301  
302 =head1 CONTRIBUTING
303  
304  
305 To contribute, you can send patches by email/via RT, or send pull requests on
306 GitHub.
307  
308 Most of the time, you don't need to build the distribution yourself. You can
309 simply modify the code, then test via:
310  
311 % prove -l
312  
313 If you want to build the distribution (e.g. to try to install it locally on your
314 system), you can install L<Dist::Zilla>,
315 L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
316 L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
317 Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
318 that are considered a bug and can be reported to me.
319  
320 =head1 COPYRIGHT AND LICENSE
321  
322 This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
323  
324 This is free software; you can redistribute it and/or modify it under
325 the same terms as the Perl 5 programming language system itself.
326  
327 =head1 BUGS
328  
329 Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Org-Parser>
330  
331 When submitting a bug or request, please include a test-file or a
332 patch to an existing test-file that illustrates the bug or desired
333 feature.
334  
335 =cut
↑ Summary Document.pm »