blib/lib/DateTime/Format/W3CDTF.pm

filter: Statementstmt 90.9% 60 / 66 Branchbran 84.3% 27 / 32 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 91.6% 11 / 12 Podpod 100% 4 / 4 totaltotal 89.0% 114 / 128 CC 20 SCAR 18.8
CC
20
Cov
89%
parse_datetime
11.0
format_datetime
7.9
_offset_as_string
4.0
CRAP 6.5
↑ Summary
1 =head1 NAME
2  
3 DateTime::Format::W3CDTF - Parse and format W3CDTF datetime strings
4  
5 =head1 VERSION
6  
7 This document describes DateTime::Format::W3CDTF version 0.09
8  
9 =head1 SYNOPSIS
10  
11 use DateTime::Format::W3CDTF;
12  
13 my $w3c = DateTime::Format::W3CDTF->new(strict => 1);
14 my $dt = $w3c->parse_datetime( '2003-02-15T13:50:05-05:00' );
15  
16 # 2003-02-15T13:50:05-05:00
17 $w3c->format_datetime($dt);
18  
19 =head1 DESCRIPTION
20  
21 This module understands the W3CDTF date/time format, an ISO 8601 profile,
22 defined at http://www.w3.org/TR/NOTE-datetime. This format as the native
23 date format of RSS 1.0.
24  
25 It can be used to parse these formats in order to create the appropriate
26 objects.
27  
28 =cut
29  
30 package DateTime::Format::W3CDTF;
31  
32 4 ▶use v5.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 $VERSION = '0.09';
39  
40 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 26 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
45 26 my $class = shift;
46  
47 26 return bless {@_}, $class;
48 }
49  
50 36 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=11 SCAR=24.0
Pod documented
51 36 my ( $self, $date ) = @_;
52  
53 # Dummy self if called as class method.
54 36 ▶ $self = $self->new() unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 18 18 36
55  
56 36 my @fields = qw/ year month day hour minute second fraction time_zone /;
57 36 ▶ my @values =
Branch: 1 branch
BranchTrueFalseTotal
my(@values) = $date =~ /^(\d\d\d\d) # Year (?:-(\d\d) # -Month (?:-(\d\d) # -Day (?:T (\d\d):(\d\d) # Hour:Minute (?: :(\d\d) # :Second (\.\d+)? # .Fractional_Second )? ( Z # UTC | [+-]\d\d:\d\d # Hour:Minute TZ offset (?::\d\d)? # :Second TZ offset )?)?)?)?$/x or die "Invalid W3CDTF datetime string ($date)" 20 16 36
58 ( $date =~ /^(\d\d\d\d) # Year
59 (?:-(\d\d) # -Month
60 (?:-(\d\d) # -Day
61 (?:T
62 (\d\d):(\d\d) # Hour:Minute
63 (?:
64 :(\d\d) # :Second
65 (\.\d+)? # .Fractional_Second
66 )?
67 ( Z # UTC
68 | [+-]\d\d:\d\d # Hour:Minute TZ offset
69 (?::\d\d)? # :Second TZ offset
70 )?)?)?)?$/x )
71 or die "Invalid W3CDTF datetime string ($date)";
72 20 my %p;
73 20 ▶ for ( my $i=0; $i < @values; $i++ ) { # Oh how I wish Perl had zip
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 160 20 180
74 160 ▶ next unless defined $values[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values[$i] 52 108 160
75 108 $p{$fields[$i]} = $values[$i];
76 }
77
78 die "Invalid W3CDTF datetime string without timezone ($date)"
79 20 ▶ if $self->{strict} && $p{hour} && !$p{time_zone};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} and $p{"hour"} and not $p{"time_zone"} 2 18 20
Condition100%6/6
$self->{"strict"} and $p{"hour"} and not $p{"time_zone"}
!ll&&!rl&&r
1352
$self->{"strict"} and $p{"hour"}
!ll&&!rl&&r
1037
MC/DC100%3/3
$self->{"strict"} and $p{"hour"} and not $p{"time_zone"}
$self->{"strict"} $p{"hour"} not $p{"time_zone"}
Truth table100%4/4
A and B and not(C)
A= $self->{"strict"}B= $p{"hour"}C= not $p{"time_zone"}
ABCresult
0XX0
10X0
1100
1111
80  
81 ### support for YYYY-MM-DDT24:00:00 as a syntactic form for 00:00:00 on the day following YYYY-MM-DD
82 ### this is allowed in xsd dateTime syntactic forms, but not W3CDTF.
83 # my $next_day = 0;
84 # if (defined($p{hour}) and defined($p{minute}) and defined($p{second})) {
85 # if ($p{hour} eq '24') {
86 # if ($p{second} eq '00' and $p{minute} eq '00') {
87 # $p{hour} = '00';
88 # $next_day++;
89 # } else {
90 # die "Cannot use hour value '24' with non-zero minutes and seconds\n";
91 # }
92 # }
93 # }
94
95 18 ▶ if ( !$p{time_zone} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $p{'time_zone'}) { } 8 10 18
elsif ($p{'time_zone'} eq 'Z') { } 4 6 10
96 8 $p{time_zone} = 'floating';
97 } elsif ( $p{time_zone} eq 'Z' ) {
98 4 $p{time_zone} = 'UTC';
99 }
100  
101 18 ▶ if ( $p{fraction} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"fraction"}) 4 14 18
102 4 $p{nanosecond} = floor($p{fraction} * 1_000_000_000);
103 delete $p{fraction}
104 4 }
105  
106 18 my $dt = DateTime->new( %p );
107 # if ($next_day) {
108 # $dt->add( day => 1 );
109 # }
110 18 return $dt;
111 }
112  
113 7 ▶sub format_datetime {
Subroutine format_datetime: called CC=7 SCAR=20.6
Pod documented
114 7 my ( $self, $dt ) = @_;
115  
116 # Dummy self if called as class method.
117 7 ▶ $self = {} unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 7 7
118  
119 7 my $base = sprintf(
120 '%04d-%02d-%02dT%02d:%02d:%02d',
121 $dt->year, $dt->month, $dt->day,
122 $dt->hour, $dt->minute, $dt->second
123 );
124  
125 7 ▶ if ( $dt->nanosecond ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->nanosecond) 0 7 7
126 0 my $secs = sprintf "%f", $dt->nanosecond / 1_000_000_000;
127 0 $secs =~ s/^0//;
128 0 $base .= $secs;
129 }
130  
131 7 my $tz = $dt->time_zone;
132  
133 7 ▶ return $base . 'Z' if $tz->is_utc;
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_utc 0 7 7
134  
135 7 my $offset = $dt->offset;
136  
137 7 ▶ if ($tz->is_floating or !defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tz->is_floating or not defined $offset) 2 5 7
Condition66%2/3
$tz->is_floating or not defined $offset
l!l&&r!l&&!r
205
MC/DC50%1/2
$tz->is_floating or not defined $offset
$tz->is_floating not defined $offset
Truth table66%2/3
A or not(B)
A= $tz->is_floatingB= not defined $offset
ABresult
1X1
011
000
138 die qq[Strict W3CDTF format does not support "floating" timezones]
139 2 ▶ if $self->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} 1 1 2
140 1 return $base;
141 }
142  
143 5 return $base . _offset_as_string($offset);
144 }
145  
146 0 ▶sub format_date {
Subroutine format_date: not called CC=1 SCAR=6.9
Pod documented
147 0 my ( $self, $dt ) = @_;
148  
149 0 my $base = sprintf( '%04d-%02d-%02d', $dt->year, $dt->month, $dt->day );
150 0 return $base;
151 }
152  
153 # minor offset_as_string variant w/ :
154 #
155 5 ▶sub _offset_as_string {
Subroutine _offset_as_string: called CC=4 SCAR=14.0
156 5 my $offset = shift;
157  
158 5 ▶ return undef unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 5 5
159  
160 5 ▶ my $sign = $offset < 0 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$offset < 0 ? : 4 1 5
161  
162 5 my $hours = $offset / ( 60 * 60 );
163 5 $hours = abs($hours) % 24;
164  
165 5 my $mins = ( $offset % ( 60 * 60 ) ) / 60;
166  
167 5 my $secs = $offset % 60;
168  
169 return (
170 5 ▶ $secs
Branch: 1 branch
BranchTrueFalseTotal
$secs ? : 0 5 5
171 ? sprintf( '%s%02d:%02d:%02d', $sign, $hours, $mins, $secs )
172 : sprintf( '%s%02d:%02d', $sign, $hours, $mins )
173 );
174 }
175  
176 4 1;
177  
178 __END__
↑ Summary