blib/lib/Dallycot/Library/Core/DateTime.pm

filter: Statementstmt 49.7% 84 / 169 Branchbran 9.2% 5 / 54 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 72.4% 21 / 29 Podpod 0.0% 0 / 1 totaltotal 40.2% 110 / 273 CC 32 SCAR 36.6
CC
32
Cov
38%
__ANON__
110.0
__ANON__
90.0
__ANON__
42.0
CRAP 38.7
« blib/lib/Dallycot/TextResolver/Request.pm ↑ Summary blib/lib/Dallycot/Model.pm »
1 package Dallycot::Library::Core::DateTime;
2 9 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Core library of useful date/time functions
5  
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use Dallycot::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use DateTime::Calendar::Mayan;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use DateTime::Calendar::Hebrew;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use DateTime::Calendar::Julian;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use DateTime::Calendar::Pataphysical;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use DateTime::Calendar::Hijri;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Hack to get the Islamic calendar convertable
24  
25 0 ▶sub DateTime::Calendar::Hijri::clone { $_[0] }
Subroutine clone: not called CC=1 SCAR=6.9
Pod undocumented
26  
27 9 ▶use DateTime::Format::Flexible;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶use List::Util qw(all any);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 9 ▶use Dallycot::Library::Core ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 9 ▶use Dallycot::Value::DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use Dallycot::Value::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 9 ns 'http://www.dallycot.net/ns/date-time/1.0#';
37  
38 # uses 'http://www.dallycot.net/ns/loc/1.0#',
39 # 'http://www.dallycot.net/ns/core/1.0#';
40  
41 #====================================================================
42 #
43 # Basic string functions
44  
45 9 my %CALENDAR_ARGS = (
46 Gregorian => {
47 class => 'DateTime',
48 date_names => [qw(year month day hour minute second)],
49 duration_names => [qw(years months days hours minutes seconds)],
50 time_zone => 1
51 },
52 Hebrew => {
53 class => 'DateTime::Calendar::Hebrew',
54 date_names => [qw(year month day)],
55 time_zone => 1
56 },
57 Julian => {
58 class => 'DateTime::Calendar::Julian',
59 date_names => [qw(year month day hour minute second)],
60 time_zone => 1
61 },
62 Jewish => {
63 class => 'DateTime::Calendar::Hebrew',
64 date_names => [qw(year month day)],
65 time_zone => 1
66 },
67 Hijri => {
68 class => 'DateTime::Calendar::Hijri',
69 date_names => [qw(year month day)],
70 time_zone => 0
71 },
72 Islamic => {
73 class => 'DateTime::Calendar::Hijri',
74 date_names => [qw(year month day)],
75 time_zone => 0
76 },
77 Mayan => {
78 class => 'DateTime::Calendar::Mayan',
79 date_names => [qw(baktun katun tun uinal kin)],
80 time_zone => 0
81 },
82 Pataphysical => {
83 class => 'DateTime::Calendar::Pataphysical',
84 date_names => [qw(year month day)],
85 time_zone => 0
86 },
87 );
88  
89 define
90 'date' => (
91 hold => 0,
92 arity => 1,
93 options => {
94 timezone => Dallycot::Value::String->new("UTC"),
95 calendar => Dallycot::Value::String->new("Gregorian")
96 }
97 ),
98 0 ▶ sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
99 0 my ( $engine, $options, $vector ) = @_;
100  
101 0 ▶ if(!$vector -> isa('Dallycot::Value::Vector')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vector->isa("Dallycot::Value::Vector")) 0 0 0
102 0 croak 'The argument for date must be a vector of numerics';
103 }
104 0 ▶ if(!all { $_ -> isa('Dallycot::Value::Numeric') } $vector->values) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&all(sub { $_->isa("Dallycot::Value::Numeric"); } , $vector->values)) 0 0 0
105 0 croak 'The argument for date must be a vector of numerics';
106 }
107 0 my @valid_calendars = grep { defined $CALENDAR_ARGS{$_}{date_names} } keys %CALENDAR_ARGS;
108  
109 0 ▶ if(!$options->{calendar}->isa('Dallycot::Value::String')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"calendar"}->isa("Dallycot::Value::String")) 0 0 0
110 0 croak 'The calendar option for date must be one of ' . join(', ', @valid_calendars);
111 }
112 0 ▶ if(!$options->{timezone}->isa('Dallycot::Value::String') && !$options->{timezone}->isa('Dallycot::Value::Undefined')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"timezone"}->isa("Dallycot::Value::String") or $options->{"timezone"}->isa("Dallycot::Value::Undefined")) 0 0 0
Condition0%0/3
$options->{"timezone"}->isa("Dallycot::Value::String") or $options->{"timezone"}->isa("Dallycot::Value::Undefined")
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"timezone"}->isa("Dallycot::Value::String") or $options->{"timezone"}->isa("Dallycot::Value::Undefined")
$options->{"timezone"}->isa("Dallycot::Value::String") $options->{"timezone"}->isa("Dallycot::Value::Undefined")
Truth table0%0/3
A or B
A= $options->{"timezone"}->isa("Dallycot::Value::String")B= $options->{"timezone"}->isa("Dallycot::Value::Undefined")
ABresult
1X1
011
000
113 0 croak 'The timezone option for date must be a string or nil';
114 }
115  
116 0 my $calendar = $options->{calendar}->value;
117 0 ▶ if(!any { $_ eq $calendar } @valid_calendars) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_ eq $calendar; } , @valid_calendars)) 0 0 0
118 0 croak 'The calendar option for date must be one of ' . join(', ', @valid_calendars);
119 }
120  
121 0 my @values = map { $_ -> value -> numify } $vector -> values;
122 0 my @arg_names = @{$CALENDAR_ARGS{$calendar}{date_names}};
123 0 my $class = $CALENDAR_ARGS{$calendar}{class};
124  
125 0 ▶ $#arg_names = $#values if $#values < $#arg_names;
Branch: 1 branch
BranchTrueFalseTotal
if $#values < $#arg_names 0 0 0
126 0 my %args;
127  
128 0 @args{@arg_names} = @values;
129  
130 0 ▶ if($options->{timezone}->isa('Dallycot::Value::String') && $CALENDAR_ARGS{$calendar}{time_zone}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"timezone"}->isa("Dallycot::Value::String") and $CALENDAR_ARGS{$calendar}{"time_zone"}) 0 0 0
Condition0%0/3
$options->{"timezone"}->isa("Dallycot::Value::String") and $CALENDAR_ARGS{$calendar}{"time_zone"}
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"timezone"}->isa("Dallycot::Value::String") and $CALENDAR_ARGS{$calendar}{"time_zone"}
$options->{"timezone"}->isa("Dallycot::Value::String") $CALENDAR_ARGS{$calendar}{"time_zone"}
Truth table0%0/3
A and B
A= $options->{"timezone"}->isa("Dallycot::Value::String")B= $CALENDAR_ARGS{$calendar}{"time_zone"}
ABresult
0X0
100
111
131 0 $args{time_zone} = $options->{timezone}->value;
132 }
133  
134 0 return Dallycot::Value::DateTime -> new(
135 object => $class -> new(%args),
136 class => $class
137 );
138 9 };
139  
140 define
141 'calendar-convert' => (
142 hold => 0,
143 arity => [1,2],
144 options => {}
145 0 ▶ ), sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
146 0 my ( $engine, $options, $date, $calendar ) = @_;
147  
148 0 ▶ if($calendar && !$calendar->isa('Dallycot::Value::String')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calendar and not $calendar->isa("Dallycot::Value::String")) 0 0 0
Condition0%0/3
$calendar and not $calendar->isa("Dallycot::Value::String")
!ll&&!rl&&r
000
MC/DC0%0/2
$calendar and not $calendar->isa("Dallycot::Value::String")
$calendar not $calendar->isa("Dallycot::Value::String")
Truth table0%0/3
A and not(B)
A= $calendarB= not $calendar->isa("Dallycot::Value::String")
ABresult
0X0
100
111
149 0 croak 'The calendar argument to calendar-convert must be a string';
150 }
151  
152 0 ▶ if($calendar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calendar) { } 0 0 0
153 0 $calendar = $calendar->value;
154 }
155 else {
156 0 $calendar = 'Gregorian';
157 }
158  
159 0 ▶ if(!$CALENDAR_ARGS{$calendar}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CALENDAR_ARGS{$calendar}) 0 0 0
160 0 croak 'Calendar-convert only supports ' . join(', ', sort keys %CALENDAR_ARGS);
161 }
162  
163 0 ▶ if(!$date -> isa('Dallycot::Value::DateTime')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date->isa("Dallycot::Value::DateTime")) 0 0 0
164 0 croak 'Calendar-convert expects a date object as its first argument';
165 }
166  
167 return Dallycot::Value::DateTime->new(
168 object => $date->value,
169 class => $CALENDAR_ARGS{$calendar}{class}
170 0 );
171 9 };
172  
173 define
174 'duration' => (
175 hold => 0,
176 arity => [1,2],
177 options => {
178 calendar => Dallycot::Value::String->new("Gregorian")
179 }
180 ),
181 1 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=25.1
182 1 my ( $engine, $options, $vector, $target ) = @_;
183  
184 1 ▶ if(defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 0 1 1
185 0 ▶ if(!$vector->isa('Dallycot::Value::DateTime') || !$target->isa('Dallycot::Value::DateTime')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vector->isa("Dallycot::Value::DateTime") and $target->isa("Dallycot::Value::DateTime")) 0 0 0
Condition0%0/3
$vector->isa("Dallycot::Value::DateTime") and $target->isa("Dallycot::Value::DateTime")
!ll&&!rl&&r
000
MC/DC0%0/2
$vector->isa("Dallycot::Value::DateTime") and $target->isa("Dallycot::Value::DateTime")
$vector->isa("Dallycot::Value::DateTime") $target->isa("Dallycot::Value::DateTime")
Truth table0%0/3
A and B
A= $vector->isa("Dallycot::Value::DateTime")B= $target->isa("Dallycot::Value::DateTime")
ABresult
0X0
100
111
186 0 croak 'Both arguments for duration must be dates';
187 }
188 0 return Dallycot::Value::Duration->new(
189 object => ($target->value - $vector->value)
190 );
191 }
192  
193 1 ▶ if(!$vector -> isa('Dallycot::Value::Vector')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vector->isa("Dallycot::Value::Vector")) 0 1 1
194 0 croak 'The argument for duration must be a vector of numerics';
195 }
196 1 ▶ if(!all { $_ -> isa('Dallycot::Value::Numeric') } $vector->values) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless (&all(sub { $_->isa("Dallycot::Value::Numeric"); } , $vector->values)) 0 1 1
197 0 croak 'The argument for duration must be a vector of numerics';
198 }
199  
200 1 my @valid_calendars = grep { defined $CALENDAR_ARGS{$_}{duration_names} } keys %CALENDAR_ARGS;
201  
202 1 ▶ if(!$options->{calendar}->isa('Dallycot::Value::String')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"calendar"}->isa("Dallycot::Value::String")) 0 1 1
203 0 croak 'The calendar option for duration must be one of ' . join(', ', @valid_calendars);
204 }
205  
206 1 my $calendar = $options->{calendar}->value;
207 1 ▶ if(!any { $_ eq $calendar } @valid_calendars) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_ eq $calendar; } , @valid_calendars)) 0 1 1
208 0 croak 'The calendar option for duration must be one of ' . join(', ', @valid_calendars);
209 }
210  
211 1 my @values = map { $_ -> value -> numify } $vector -> values;
212 1 my @arg_names = @{$CALENDAR_ARGS{$calendar}{duration_names}};
213 1 my $class = $CALENDAR_ARGS{$calendar}{class};
214  
215 1 $#arg_names = $#values;
216 1 my %args;
217  
218 1 @args{@arg_names} = @values;
219  
220 1 return Dallycot::Value::Duration -> new(
221 %args
222 );
223 9 };
224  
225 define now => (
226 hold => 0,
227 arity => 0,
228 options => {
229 timezone => Dallycot::Value::String->new("UTC")
230 }
231 0 ▶), sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 0 my( $engine, $options ) = @_;
233  
234 return Dallycot::Value::DateTime -> now(
235 $options->{timezone}->value
236 0 );
237 9 };
238  
239 define 'convert-timezone' => (
240 hold => 0,
241 arity => [2],
242 options => {}
243 0 ▶), sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
244 0 my( $engine, $options, $datetime, $timezone ) = @_;
245  
246 0 ▶ if(!$datetime -> isa('Dallycot::Value::DateTime')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($datetime->isa("Dallycot::Value::DateTime")) 0 0 0
247 0 croak 'in-timezone expects a date/time value as its first argument';
248 }
249 0 ▶ if(!$timezone -> isa('Dallycot::Value::String')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timezone->isa("Dallycot::Value::String")) 0 0 0
250 0 croak 'in-timezone expects a string as its second argument';
251 }
252  
253 0 return $datetime -> in_timezone($timezone -> value);
254 9 };
255  
256 define 'parse-datetime' => (
257 hold => 0,
258 arity => 1,
259 options => {
260 language => undef, # <String>
261 european => undef, # Boolean
262 base => undef, # DateTime
263 'month-year' => undef, # Boolean
264 }
265 0 ▶), sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
266 0 my( $engine, $options, $string ) = @_;
267  
268 0 my %parse_options;
269  
270 0 ▶ if($options->{language}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'language'}) { } 0 0 0
271 0 given(blessed $options->{language}) {
272 0 when('Dallycot::Value::Vector') {
273 $parse_options{lang} =
274 0 grep { $_ }
275 0 map { $_ -> value }
276 0 grep { $_ -> isa('Dallycot::Value::String') }
277 0 $options->{language}->values;
278 }
279 0 when('Dallycot::Value::String') {
280 0 $parse_options{lang} = [ $options->{language}->value ];
281 }
282 0 default {
283 0 croak "parse-datetime expects the language option to be a vector of String";
284 }
285 }
286 }
287 else {
288 0 $parse_options{lang} = [ $string -> lang ];
289 }
290 0 ▶ if($options->{european}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"european"}) 0 0 0
291 0 ▶ if($options->{european}->isa('Dallycot::Value::Boolean')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'european'}->isa('Dallycot::Value::Boolean')) { } 0 0 0
292 0 $parse_options{european} = $options->{european}->value;
293 }
294 else {
295 0 croak "parse-datetime expects the european option to be a Boolean";
296 }
297 }
298 0 ▶ if($options->{base}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"base"}) 0 0 0
299 0 ▶ if($options->{base}->isa('Dallycot::Value::DateTime')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'base'}->isa('Dallycot::Value::DateTime')) { } 0 0 0
300 0 $parse_options{base} = DateTime->from_object(object => $options->{base}->[0]);
301 }
302 else {
303 0 croak "parse-datetime expects the base option to be a date/time object";
304 }
305 }
306 0 ▶ if($options->{'month-year'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"month-year"}) 0 0 0
307 0 ▶ if($options->{'month-year'}->isa('Dallycot::Value::Boolean')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'month-year'}->isa('Dallycot::Value::Boolean')) { } 0 0 0
308 0 $parse_options{MMYY} = $options->{'month-year'}->value;
309 }
310 else {
311 0 croak "parse-datetime expects the month-year option to be a Boolean";
312 }
313 }
314  
315 0 my $value = eval { DateTime::Format::Flexible->parse_datetime($string->value, %parse_options) };
316 0 ▶ if($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 0 0
317 0 return Dallycot::Value::DateTime->new(object => $value);
318 }
319 else {
320 0 return Dallycot::Value::Undefined->new;
321 }
322 9 };
323  
324 9 1;
« blib/lib/Dallycot/TextResolver/Request.pm ↑ Summary blib/lib/Dallycot/Model.pm »