lib/DateTime/Format/DateParse.pm

filter: Statementstmt 91.1% 41 / 45 Branchbran 76.9% 20 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 83.3% 70 / 84 CC 15 SCAR 25.5
CC
15
Cov
84%
parse_datetime
17.5
BEGIN
1.0
BEGIN
1.0
CRAP 12.8
↑ Summary
1 package DateTime::Format::DateParse;
2  
3 # Copyright (C) 2005-6 Joshua Hoblitt
4 #
5 # $Id: DateParse.pm 4429 2010-04-10 19:14:33Z jhoblitt $
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 $VERSION = '0.05';
11  
12 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Date::Parse qw( strptime );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Time::Zone qw( tz_offset );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 260 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=15 SCAR=28.6
Pod documented
18 260 my ($class, $date, $zone) = @_;
19  
20 # str2time() calls strptime() internally so it's more efficent to use
21 # strptime() directly. However, the extra validation done by using
22 # DateTime->new() instad of DateTime->from_epoch() may make it into a net
23 # loss. In the end, it turns out that strptime()'s offset information is
24 # needed anyways.
25 260 my @t = strptime( $date, $zone );
26  
27 260 ▶ return undef unless @t;
Branch: 1 branch
BranchTrueFalseTotal
unless @t 0 260 260
28  
29 260 my ($ss, $mm, $hh, $day, $month, $year, $offset) = @t;
30  
31 260 my %p;
32 260 ▶ if ( $ss ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ss) 230 30 260
33 230 my $fraction = $ss - int( $ss );
34 230 ▶ if ($fraction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fraction) 4 226 230
35 4 my $nano = $fraction * 1e9;
36 4 ▶ if ( $nano != int( $nano ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nano != int $nano) 4 0 4
37 4 ▶ $nano++ if $nano - int( $nano ) >= 0.5;
Branch: 1 branch
BranchTrueFalseTotal
if $nano - int($nano) >= "0.5" 1 3 4
38 }
39 4 $p{ nanosecond } = int( $nano );
40 }
41 230 $p{ second } = int $ss;
42 }
43 260 ▶ $p{ minute } = $mm if $mm;
Branch: 1 branch
BranchTrueFalseTotal
if $mm 249 11 260
44 260 ▶ $p{ hour } = $hh if $hh;
Branch: 1 branch
BranchTrueFalseTotal
if $hh 250 10 260
45 260 ▶ $p{ day } = $day if $day;
Branch: 1 branch
BranchTrueFalseTotal
if $day 259 1 260
46 260 ▶ $p{ month } = $month + 1 if $month;
Branch: 1 branch
BranchTrueFalseTotal
if $month 248 12 260
47 260 ▶ $p{ year } = $year ? $year + 1900 : DateTime->now->year;
Branch: 1 branch
BranchTrueFalseTotal
$year ? : 257 3 260
48  
49 # unless there is an explict ZONE, Date::Parse seems to parse date only
50 # formats, eg. "1995-01-24", as being in the 'local' timezone.
51 260 ▶ unless ( defined $zone || defined $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $zone or defined $offset) 0 260 260
Condition33%1/3
defined $zone or defined $offset
l!l&&r!l&&!r
02600
MC/DC0%0/2
defined $zone or defined $offset
defined $zone defined $offset
Truth table33%1/3
A or B
A= defined $zoneB= defined $offset
ABresult
1X1
011
000
52 0 return DateTime->new(
53 %p,
54 time_zone => 'local',
55 );
56 }
57  
58 260 ▶ if ( $zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone) 0 260 260
59 0 ▶ if ( DateTime::TimeZone->is_valid_name( $zone ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('DateTime::TimeZone'->is_valid_name($zone)) { } 0 0 0
60 0 return DateTime->new(
61 %p,
62 time_zone => $zone,
63 );
64 } else {
65 # attempt to convert Time::Zone tz's into an offset
66 0 return DateTime->new(
67 %p,
68 time_zone =>
69 # not an Olson timezone, no DST info
70 DateTime::TimeZone::offset_as_string( tz_offset( $zone ) ),
71 );
72 }
73 }
74  
75 260 return DateTime->new(
76 %p,
77 time_zone =>
78 # not an Olson timezone, no DST info
79 DateTime::TimeZone::offset_as_string( $offset ),
80 );
81 }
82  
83 3 1;
84  
85 __END__
↑ Summary