blib/lib/Types/DateTime.pm

filter: Statementstmt 97.5% 78 / 80 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.0% 18 / 20 Podpod n/a totaltotal 91.4% 107 / 117 CC 8 SCAR 7.4
CC
8
Cov
93%
__ANON__
5.1
__ANON__
3.0
__ANON__
2.0
CRAP 2.1
↑ Summary
1 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Types::DateTime;
6  
7 4 our $AUTHORITY = 'cpan:TOBYINK';
8 4 our $VERSION = '0.002';
9  
10 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use DateTime::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use Type::Library -base, -declare => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 DateTime Duration TimeZone Locale Now
19 DateTimeWithZone DateTimeUTC
20 4 );
21 4 ▶use Types::Standard qw( Num Str HashRef InstanceOf );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # This stuff for compat with MooseX::Types::DateTime
25  
26 4 class_type(DateTime, { class => 'DateTime' });
27 4 class_type(Duration, { class => 'DateTime::Duration' });
28 4 class_type(TimeZone, { class => 'DateTime::TimeZone' });
29 4 declare Locale,
30 as InstanceOf['DateTime::Locale::root','DateTime::Locale::FromData'];
31 4 enum(Now, ['now']);
32  
33 4 coerce DateTime,
34 from Num, q{ 'DateTime'->from_epoch(epoch => $_) },
35 from HashRef, q{ exists($_->{epoch}) ? 'DateTime'->from_epoch(%$_) : 'DateTime'->new(%{$_}) },
36 from Now, q{ 'DateTime'->now },
37 from InstanceOf['DateTime::Tiny'], q{ $_->DateTime };
38  
39 4 coerce Duration,
40 from Num, q{ 'DateTime::Duration'->new(seconds => $_) },
41 from HashRef, q{ 'DateTime::Duration'->new(%{$_}) };
42  
43 4 coerce TimeZone,
44 from Str, q{ 'DateTime::TimeZone'->new(name => $_) };
45  
46 4 coerce Locale,
47 from InstanceOf['Locale::Maketext'], q{ 'DateTime::Locale'->load($_->language_tag) },
48 from Str, q{ 'DateTime::Locale'->load($_) };
49  
50 # Time zone stuff
51  
52 declare DateTimeWithZone,
53 as DateTime,
54 coercion => 1, # inherit coercions
55 0 ▶ where { not($_ ->time_zone->is_floating) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 54 ▶ inline_as { (undef, "not($_\->time_zone->is_floating)") },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 4 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
58 4 my $zone = TimeZone->assert_coerce(shift);
59 0 ▶ sub { $_[0]->time_zone eq $zone };
Subroutine __ANON__: not called CC=1 SCAR=1.2
60 },
61 4 ▶ coercion_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 4 my $parent = shift;
63 4 my $child = shift;
64 4 my $zone = TimeZone->assert_coerce(shift);
65
66 4 my $c = 'Type::Coercion'->new(type_constraint => $child);
67 $c->add_type_coercions(
68 2 ▶ $parent->coercibles, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
69 2 my $dt = DateTime->coerce($_);
70 2 ▶ return $_ unless DateTime->check($dt);
Branch: 1 branch
BranchTrueFalseTotal
unless &DateTime()->check($dt) 0 2 2
71 2 $dt->set_time_zone($zone);
72 2 return $dt;
73 },
74 4 );
75 4 $c;
76 4 };
77  
78 4 declare DateTimeUTC, as DateTimeWithZone['UTC'], coercion => 1;
79  
80 # Stringy coercions. No sugar for this stuff ;-)
81  
82 __PACKAGE__->meta->add_coercion({
83 name => 'Format',
84 type_constraint => DateTime,
85 4 ▶ coercion_generator => sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
86 4 my ($self, $target, $format) = @_;
87 4 ▶ $format = use_module("DateTime::Format::$format")->new
Branch: 1 branch
BranchTrueFalseTotal
unless ref $format 4 0 4
88 unless ref($format);
89
90 4 my $timezone;
91 4 ▶ if ($target->is_a_type_of(DateTimeWithZone))
Branch: 1 branch
BranchTrueFalseTotal
if ($target->is_a_type_of(&DateTimeWithZone())) 1 3 4
92 {
93 my ($paramd_type) = grep {
94 1 ▶ $_->is_parameterized and $_->parent==DateTimeWithZone
Condition66%2/3
$_->is_parameterized && $_->parent == &DateTimeWithZone()
!ll&&!rl&&r
801
MC/DC50%1/2
$_->is_parameterized && $_->parent == &DateTimeWithZone()
$_->is_parameterized $_->parent == &DateTimeWithZone()
Truth table66%2/3
A && B
A= $_->is_parameterizedB= $_->parent == &DateTimeWithZone()
ABresult
0X0
100
111
95 } ($target, $target->parents);
96 1 ▶ $timezone = TimeZone->assert_coerce($paramd_type->type_parameter)
Branch: 1 branch
BranchTrueFalseTotal
if $paramd_type 1 0 1
97 if $paramd_type;
98 }
99
100 return (
101 Str,
102 4 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
103 4 my $dt = eval { $format->parse_datetime($_) };
104 4 ▶ return $_ unless $dt;
Branch: 1 branch
BranchTrueFalseTotal
unless $dt 0 4 4
105 4 ▶ $dt->set_time_zone($timezone) if $timezone;
Branch: 1 branch
BranchTrueFalseTotal
if $timezone 2 2 4
106 4 $dt;
107 },
108 4 );
109 },
110 4 });
111  
112 __PACKAGE__->meta->add_coercion({
113 name => 'Strftime',
114 type_constraint => Str,
115 2 ▶ coercion_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 2 my ($self, $target, $format) = @_;
117 2 my $format_quoted = B::perlstring($format);
118 return (
119 2 DateTime->coercibles,
120 qq{\$_->strftime($format_quoted)},
121 );
122 },
123 4 });
124  
125 4 __PACKAGE__->meta->add_coercion({
126 name => 'ToISO8601',
127 type_constraint => Str,
128 type_coercion_map => [
129 DateTime->coercibles,
130 q{$_->iso8601},
131 ],
132 });
133  
134 4 1;
135  
136 __END__
↑ Summary