blib/lib/MooseX/Types/DateTime/W3C.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 96.6% 58 / 60 CC 5 SCAR 8.1
CC
5
Cov
96%
__ANON__
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.3
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package MooseX::Types::DateTime::W3C;
4 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 $MooseX::Types::DateTime::W3C::AUTHORITY = 'cpan:AJGB';
6 }
7 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 $MooseX::Types::DateTime::W3C::VERSION = '1.103360';
9 }
10 #ABSTRACT: W3C DateTime format type constraint
11  
12  
13 3 ▶use MooseX::Types -declare =>[qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 DateTimeW3C
15 3 )];
16 3 ▶use MooseX::Types::Moose qw( Str Num );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use DateTime::TimeZone 1.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 subtype DateTimeW3C,
22 as Str,
23 42 ▶ where { /^
Subroutine __ANON__: called CC=1 SCAR=0.0
24 \d{4} # year
25 (?:\-\d{2})? # month
26 (?:\-\d{2})? # day
27 (?:
28 T
29 \d{2} # hours
30 :
31 \d{2} # minutes
32 (?:
33 :\d{2} # seconds
34 (?:
35 \.\d+ # fraction of second
36 )?
37 )?
38 (?:
39 Z|[\+\-]\d{2}:\d{2} # time zone
40 )?
41 )?
42 3 $/x };
43  
44 {
45 3 ▶ my $_dt_format = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
46 20 my $dt = shift;
47 20 my $datetime = sprintf('%04d-%02d-%02dT%02d:%02d:%02d',
48 $dt->year, $dt->month, $dt->day,
49 $dt->hour, $dt->minute, $dt->second
50 );
51 20 ▶ if ( my $ns = $dt->nanosecond ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $dt->nanosecond) 3 17 20
52 3 $ns =~ s/0+$//;
53 3 $datetime .= ".$ns";
54 }
55 20 my $tz = $dt->time_zone;
56  
57 20 ▶ return $datetime if $tz->is_floating;
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_floating 2 18 20
58 18 ▶ return $datetime .'Z' if $tz->is_utc;
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_utc 9 9 18
59  
60 9 ▶ if ( DateTime::TimeZone->offset_as_string($dt->offset) =~
Branch: 1 branch
BranchTrueFalseTotal
if ("DateTime::TimeZone"->offset_as_string($dt->offset) =~ /^([\+\-]\d{2})(\d{2})/) 9 0 9
61 /^([\+\-]\d{2})(\d{2})/ ) {
62 9 return "$datetime$1:$2";
63 }
64 0 return $datetime;
65 3 };
66  
67 3 class_type 'DateTime';
68 coerce DateTimeW3C,
69 from 'DateTime',
70 15 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 15 $_dt_format->($_)
72 },
73 from Num,
74 5 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 5 $_dt_format->( DateTime->from_epoch(epoch => $_) )
76 }
77 3 ;
78 }
79  
80  
81 3 1;
82  
83  
84 __END__
↑ Summary