File Coverage

blib/lib/DateTimeX/Easy/DateParse.pm
Criterion Covered Total %
statement 36 39 92.3
branch 15 22 68.1
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 61 72 84.7


line stmt bran cond sub pod time code
1             package #
2             DateTimeX::Easy::DateParse;
3              
4             # Yar, stolen directly. Just needed to change 'local' to 'floating'
5              
6             # Copyright (C) 2005-6 Joshua Hoblitt
7             #
8             # $Id: DateParse.pm 3517 2006-09-17 23:10:10Z jhoblitt $
9              
10              
11             # ABSTRACT: DateParse fork for datetimex::easy
12 4     4   28 use strict;
  4         8  
  4         140  
13              
14 4     4   19 use vars qw($VERSION);
  4         9  
  4         238  
15             $VERSION = '0.04';
16              
17             # VERSION
18              
19 4     4   21 use DateTime;
  4         7  
  4         68  
20 4     4   18 use DateTime::TimeZone;
  4         5  
  4         79  
21 4     4   1626 use Date::Parse qw( strptime );
  4         23298  
  4         386  
22 4     4   31 use Time::Zone qw( tz_offset );
  4         7  
  4         962  
23              
24             =over
25              
26             =item parse_datetime
27              
28             =back
29              
30             =cut
31              
32             my ($class, $date, $zone) = @_;
33 9     9 1 28  
34             # str2time() calls strptime() internally so it's more efficent to use
35             # strptime() directly. However, the extra validation done by using
36             # DateTime->new() instad of DateTime->from_epoch() may make it into a net
37             # loss. In the end, it turns out that strptime()'s offset information is
38             # needed anyways.
39              
40             # CHANGED! Do not assume 'local' timezone by default!
41             my @t = strptime( $date, "floating");
42 9         220 # my @t = strptime( $date, $zone );
43              
44             return undef unless @t;
45 9 100       2993  
46             my ($ss, $mm, $hh, $day, $month, $year, $offset) = @t;
47 4         14  
48             my %p;
49 4         7 if ( $ss ) {
50 4 100       13 my $fraction = $ss - int( $ss );
51 3         6 $p{ nanosecond } = $fraction * 1e9 if $fraction;
52 3 50       9 $p{ second } = int $ss;
53 3         7 }
54             $p{ minute } = $mm if $mm;
55 4 50       16 $p{ hour } = $hh if $hh;
56 4 50       14 $p{ day } = $day if $day;
57 4 100       11 $p{ month } = $month + 1 if $month;
58 4 100       13 $p{ year } = $year ? $year + 1900 : DateTime->now->year;
59 4 50       15  
60             # unless there is an explict ZONE, Date::Parse seems to parse date only
61             # formats, eg. "1995-01-24", as being in the 'local' timezone.
62             unless ( defined $zone || defined $offset ) {
63 4 100 66     66 # CHANGED! Do not assume 'local' timezone by default!
64             return DateTime->new(
65 1         10 %p,
66             time_zone => 'floating',
67             );
68             # time_zone => 'local',
69             }
70              
71             if ( $zone ) {
72 3 50       11 if ( DateTime::TimeZone->is_valid_name( $zone ) ) {
73 0 0       0 return DateTime->new(
74 0         0 %p,
75             time_zone => $zone,
76             );
77             } else {
78             # attempt to convert Time::Zone tz's into an offset
79             return DateTime->new(
80 0         0 %p,
81             time_zone =>
82             # not an Olson timezone, no DST info
83             DateTime::TimeZone::offset_as_string( tz_offset( $zone ) ),
84             );
85             }
86             }
87              
88             return DateTime->new(
89 3         19 %p,
90             time_zone =>
91             # not an Olson timezone, no DST info
92             DateTime::TimeZone::offset_as_string( $offset ),
93             );
94             }
95              
96             1;
97