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 = '2026-07-21'; # DATE
14 13 our $DIST = 'Org-Parser'; # DIST
15 13 our $VERSION = '0.563'; # 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 316 ▶ before $_ => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
21 316 my $self = shift;
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 1 ▶sub clear_parse_result {
Subroutine clear_parse_result: called CC=3 SCAR=11.0
Pod documented
33 1 my $self = shift;
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 18 ▶sub as_string {
Subroutine as_string: called CC=9 SCAR=42.1
Pod documented
42 18 my ($self) = @_;
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 0 ▶sub as_text {
Subroutine as_text: not called CC=1 SCAR=6.9
Pod documented
80 0 goto \&as_string;
81 }
82  
83 70 ▶sub _parse_timestamp {
Subroutine _parse_timestamp: called CC=30 SCAR=35.4
84 70 require DateTime;
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") 70 0 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 table28%2/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__
↑ Summary Document.pm »