| 1 | package Dallycot::Value::DateTime; | ||||||||||||||||||||||||||||
| 2 | 30 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Date and time values | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 30 | ▶ | use experimental qw(switch); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 30 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 30 | ▶ | use parent 'Dallycot::Value::Any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 30 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 30 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 30 | ▶ | use List::Util qw(any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 30 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 30 | ▶ | use Promises qw(deferred); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 0 | my($class, %options) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | for my $k (keys %options) { | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | $options{$k} = $options{$k} -> value -> numify if blessed($options{$k}) && $options{$k}->isa('Dallycot::Value::Numeric'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($options{$k}) and $options{$k}->isa("Dallycot::Value::Numeric")
MC/DC0%0/2
builtin::blessed($options{$k}) and $options{$k}->isa("Dallycot::Value::Numeric")
builtin::blessed($options{$k})
$options{$k}->isa("Dallycot::Value::Numeric")
Truth table0%0/3
A and B
A= builtin::blessed($options{$k})B= $options{$k}->isa("Dallycot::Value::Numeric")
| |||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | my $datetime_class = delete $options{'class'}; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | $datetime_class //= 'DateTime'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$datetime_class //= "DateTime"
MC/DC0%0/1
$datetime_class //= "DateTime"
$datetime_class
Truth table0%0/2
A
A= $datetime_class
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if($options{object}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | return bless [ | ||||||||||||||||||||||||||||
| 33 | $datetime_class->from_object(object => $options{object}) | ||||||||||||||||||||||||||||
| 34 | 0 | ] => $class; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||
| 37 | return bless [ | ||||||||||||||||||||||||||||
| 38 | $datetime_class->new( | ||||||||||||||||||||||||||||
| 39 | 0 | map { $_ => $options{$_} } grep { $options{$_} } keys %options | |||||||||||||||||||||||||||
| 40 | ) | ||||||||||||||||||||||||||||
| 41 | ] => $class; | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub to_rdf { | ||||||||||||||||||||||||||
|
Subroutine
to_rdf: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 0 | my($self, $model) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # we need to record the date/time as represented in RDF, but might want to | ||||||||||||||||||||||||||||
| 49 | # record the calendar type as well | ||||||||||||||||||||||||||||
| 50 | 0 | my $literal = RDF::Trine::Node::Literal->new( | |||||||||||||||||||||||||||
| 51 | DateTime->from_object(object => $self->[0])->iso8601, | ||||||||||||||||||||||||||||
| 52 | '', | ||||||||||||||||||||||||||||
| 53 | $model -> meta_uri('xsd:dateTime') | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return $literal if $self->[0] -> isa('DateTime'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $bnode = $model->bnode; | |||||||||||||||||||||||||||
| 59 | 0 | $model -> add_type($bnode, 'loc:DateTime'); | |||||||||||||||||||||||||||
| 60 | 0 | $model -> add_connection($bnode, 'rdf:value', $literal); | |||||||||||||||||||||||||||
| 61 | 0 | my $calendar_type = (reverse split(/::/, blessed($self->[0])))[0]; | |||||||||||||||||||||||||||
| 62 | 0 | $model -> add_connection($bnode, 'loc:calendar', $calendar_type); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if($self->[0]->can('epoch')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $model -> add_connection($bnode, 'loc:epoch', $model->integer( | |||||||||||||||||||||||||||
| 65 | $self->[0]->epoch | ||||||||||||||||||||||||||||
| 66 | )); | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | return $bnode; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub now { | ||||||||||||||||||||||||||
|
Subroutine
now: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 0 | my($class, $timezone) = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | return bless [ DateTime -> now(time_zone => $timezone) ] => $class; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub in_timezone { | ||||||||||||||||||||||||||
|
Subroutine
in_timezone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my($self, $timezone) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $class = ref $self; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | return bless [ $self -> [0] -> clone -> set_time_zone($timezone) ] => $class; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # sub to_calendar { | ||||||||||||||||||||||||||||
| 89 | # my($self, $calendar_class) = @_; | ||||||||||||||||||||||||||||
| 90 | # | ||||||||||||||||||||||||||||
| 91 | # my $class = ref $self; | ||||||||||||||||||||||||||||
| 92 | # | ||||||||||||||||||||||||||||
| 93 | # return bless [ $calendar_class->from_object(object => $self->[0]) ] => $class; | ||||||||||||||||||||||||||||
| 94 | # } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub as_text { | ||||||||||||||||||||||||||
|
Subroutine
as_text: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if($self -> [0] -> can('datetime')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | return $self -> [0] -> datetime; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 0 | return $self -> [0] -> date; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub is_equal { | ||||||||||||||||||||||||||
|
Subroutine
is_equal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 0 | my ( $self, $engine, $other ) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | $d->resolve( $self->value == $other->value ); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub is_less { | ||||||||||||||||||||||||||
|
Subroutine
is_less: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ( $self, $engine, $other ) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | $d->resolve( $self->value < $other->value ); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub is_less_or_equal { | ||||||||||||||||||||||||||
|
Subroutine
is_less_or_equal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 0 | my ( $self, $engine, $other ) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | $d->resolve( $self->value <= $other->value ); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub is_greater { | ||||||||||||||||||||||||||
|
Subroutine
is_greater: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 0 | my ( $self, $engine, $other ) = @_; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | $d->resolve( $self->value > $other->value ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub is_greater_or_equal { | ||||||||||||||||||||||||||
|
Subroutine
is_greater_or_equal: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 0 | my ( $self, $engine, $other ) = @_; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | $d->resolve( $self->value >= $other->value ); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | return $d->promise; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub prepend { | ||||||||||||||||||||||||||
|
Subroutine
prepend: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 0 | my( $self, @things ) = @_; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # if durations, we add | ||||||||||||||||||||||||||||
| 161 | # if numeric, we add as seconds | ||||||||||||||||||||||||||||
| 162 | # otherwise, we can't prepend | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if(any { !$_->isa('Dallycot::Value::Numeric') && !$_->isa('Dallycot::Value::Duration') } @things) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
Condition0%0/3
$_->isa('Dallycot::Value::Numeric') || $_->isa('Dallycot::Value::Duration')
MC/DC0%0/2
$_->isa('Dallycot::Value::Numeric') || $_->isa('Dallycot::Value::Duration')
$_->isa('Dallycot::Value::Numeric')
$_->isa('Dallycot::Value::Duration')
Truth table0%0/3
A || B
A= $_->isa('Dallycot::Value::Numeric')B= $_->isa('Dallycot::Value::Duration')
| |||||||||||||||||||||||||||||
| 164 | 0 | croak 'Only durations and numeric values may be added to dates and times'; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my @durations = map { | ||||||||||||||||||||||||||||
| 168 | 0 | my $v = $_; | |||||||||||||||||||||||||||
| 169 | 0 | given(blessed $v) { | |||||||||||||||||||||||||||
| 170 | 0 | when('Dallycot::Value::Numeric') { | |||||||||||||||||||||||||||
| 171 | 0 | DateTime::Duration->new( | |||||||||||||||||||||||||||
| 172 | seconds => $v->value->numify | ||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 0 | when('Dallycot::Value::Duration') { | |||||||||||||||||||||||||||
| 176 | 0 | $v -> value; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } @things; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | my $accumulator = $self->value; | |||||||||||||||||||||||||||
| 182 | 0 | $accumulator = $accumulator + $_ for @durations; | |||||||||||||||||||||||||||
| 183 | 0 | return $self -> new(object => $accumulator, class => blessed($accumulator)); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 30 | 1; | |||||||||||||||||||||||||||