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 5     5   31 use strict;
  5         8  
  5         157  
13              
14 5     5   25 use vars qw($VERSION);
  5         19  
  5         271  
15             $VERSION = '0.04';
16              
17             our $VERSION = '0.090'; # VERSION
18              
19 5     5   22 use DateTime;
  5         9  
  5         86  
20 5     5   18 use DateTime::TimeZone;
  5         8  
  5         108  
21 5     5   1846 use Date::Parse qw( strptime );
  5         26284  
  5         333  
22 5     5   54 use Time::Zone qw( tz_offset );
  5         12  
  5         1072  
23              
24              
25             my ($class, $date, $zone) = @_;
26 9     9 1 19  
27             # str2time() calls strptime() internally so it's more efficent to use
28             # strptime() directly. However, the extra validation done by using
29             # DateTime->new() instad of DateTime->from_epoch() may make it into a net
30             # loss. In the end, it turns out that strptime()'s offset information is
31             # needed anyways.
32              
33             # CHANGED! Do not assume 'local' timezone by default!
34             my @t = strptime( $date, "floating");
35 9         159 # my @t = strptime( $date, $zone );
36              
37             return undef unless @t;
38 9 100       2019  
39             my ($ss, $mm, $hh, $day, $month, $year, $offset) = @t;
40 4         11  
41             my %p;
42 4         6 if ( $ss ) {
43 4 100       8 my $fraction = $ss - int( $ss );
44 3         6 $p{ nanosecond } = $fraction * 1e9 if $fraction;
45 3 50       5 $p{ second } = int $ss;
46 3         5 }
47             $p{ minute } = $mm if $mm;
48 4 50       10 $p{ hour } = $hh if $hh;
49 4 50       8 $p{ day } = $day if $day;
50 4 100       9 $p{ month } = $month + 1 if $month;
51 4 100       10 $p{ year } = $year ? $year + 1900 : DateTime->now->year;
52 4 50       12  
53             # unless there is an explict ZONE, Date::Parse seems to parse date only
54             # formats, eg. "1995-01-24", as being in the 'local' timezone.
55             unless ( defined $zone || defined $offset ) {
56 4 100 66     14 # CHANGED! Do not assume 'local' timezone by default!
57             return DateTime->new(
58 1         7 %p,
59             time_zone => 'floating',
60             );
61             # time_zone => 'local',
62             }
63              
64             if ( $zone ) {
65 3 50       5 if ( DateTime::TimeZone->is_valid_name( $zone ) ) {
66 0 0       0 return DateTime->new(
67 0         0 %p,
68             time_zone => $zone,
69             );
70             } else {
71             # attempt to convert Time::Zone tz's into an offset
72             return DateTime->new(
73 0         0 %p,
74             time_zone =>
75             # not an Olson timezone, no DST info
76             DateTime::TimeZone::offset_as_string( tz_offset( $zone ) ),
77             );
78             }
79             }
80              
81             return DateTime->new(
82 3         11 %p,
83             time_zone =>
84             # not an Olson timezone, no DST info
85             DateTime::TimeZone::offset_as_string( $offset ),
86             );
87             }
88              
89             1;
90              
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             DateTimeX::Easy::DateParse - DateParse fork for datetimex::easy
99              
100             =head1 VERSION
101              
102             version 0.090
103              
104             =over
105              
106             =item parse_datetime
107              
108             =back
109              
110             =head1 AUTHOR
111              
112             Robert Krimen <rokr@cpan.org>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2022 by Robert Krimen and others, see the git log.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut