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   32 use strict;
  4         17  
  4         179  
13              
14 4     4   24 use vars qw($VERSION);
  4         7  
  4         269  
15             $VERSION = '0.04';
16              
17             # VERSION
18              
19 4     4   21 use DateTime;
  4         5  
  4         81  
20 4     4   19 use DateTime::TimeZone;
  4         7  
  4         102  
21 4     4   2337 use Date::Parse qw( strptime );
  4         36720  
  4         393  
22 4     4   36 use Time::Zone qw( tz_offset );
  4         10  
  4         1426  
23              
24             =over
25              
26             =item parse_datetime
27              
28             =back
29              
30             =cut
31              
32             sub parse_datetime {
33 9     9 1 40 my ($class, $date, $zone) = @_;
34              
35             # str2time() calls strptime() internally so it's more efficent to use
36             # strptime() directly. However, the extra validation done by using
37             # DateTime->new() instad of DateTime->from_epoch() may make it into a net
38             # loss. In the end, it turns out that strptime()'s offset information is
39             # needed anyways.
40              
41             # CHANGED! Do not assume 'local' timezone by default!
42 9         347 my @t = strptime( $date, "floating");
43             # my @t = strptime( $date, $zone );
44              
45 9 100       4981 return undef unless @t;
46              
47 4         20 my ($ss, $mm, $hh, $day, $month, $year, $offset) = @t;
48              
49 4         11 my %p;
50 4 100       15 if ( $ss ) {
51 3         11 my $fraction = $ss - int( $ss );
52 3 50       13 $p{ nanosecond } = $fraction * 1e9 if $fraction;
53 3         14 $p{ second } = int $ss;
54             }
55 4 50       21 $p{ minute } = $mm if $mm;
56 4 50       21 $p{ hour } = $hh if $hh;
57 4 100       16 $p{ day } = $day if $day;
58 4 100       19 $p{ month } = $month + 1 if $month;
59 4 50       19 $p{ year } = $year ? $year + 1900 : DateTime->now->year;
60              
61             # unless there is an explict ZONE, Date::Parse seems to parse date only
62             # formats, eg. "1995-01-24", as being in the 'local' timezone.
63 4 100 66     43 unless ( defined $zone || defined $offset ) {
64             # CHANGED! Do not assume 'local' timezone by default!
65 1         13 return DateTime->new(
66             %p,
67             time_zone => 'floating',
68             );
69             # time_zone => 'local',
70             }
71              
72 3 50       13 if ( $zone ) {
73 0 0       0 if ( DateTime::TimeZone->is_valid_name( $zone ) ) {
74 0         0 return DateTime->new(
75             %p,
76             time_zone => $zone,
77             );
78             } else {
79             # attempt to convert Time::Zone tz's into an offset
80 0         0 return DateTime->new(
81             %p,
82             time_zone =>
83             # not an Olson timezone, no DST info
84             DateTime::TimeZone::offset_as_string( tz_offset( $zone ) ),
85             );
86             }
87             }
88              
89 3         44 return DateTime->new(
90             %p,
91             time_zone =>
92             # not an Olson timezone, no DST info
93             DateTime::TimeZone::offset_as_string( $offset ),
94             );
95             }
96              
97             1;
98              
99             __END__