File Coverage

blib/lib/Date/Parser/Date.pm
Criterion Covered Total %
statement 56 72 77.7
branch 15 30 50.0
condition 17 45 37.7
subroutine 9 11 81.8
pod 6 6 100.0
total 103 164 62.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Date::Parser::Date;
4              
5 1     1   11 use strict;
  1         1  
  1         34  
6 1     1   4 use warnings;
  1         2  
  1         22  
7              
8 1     1   978 use Time::Local;
  1         3023  
  1         1175  
9              
10             require Date::Format;
11              
12             our $VERSION = 0.4;
13              
14             sub new {
15 6     6 1 18 my ($class, %opts) = @_;
16              
17 6         15 my $self = bless {}, $class;
18 6         14 $self->{year} = $opts{year};
19 6         10 $self->{month} = $opts{month};
20 6         9 $self->{day} = $opts{day};
21 6         9 $self->{hour} = $opts{hour};
22 6         9 $self->{min} = $opts{min};
23 6         9 $self->{sec} = $opts{sec};
24 6         8 $self->{unixtime} = $opts{unixtime};
25              
26 6 100 33     11 if (defined $self->unixtime && (!defined $self->{year} ||
      66        
27             !defined $self->{month} || !defined $self->{day} ||
28             !defined $self->{hour} || !defined $self->{min} ||
29             !defined $self->{sec})) {
30 3         8 $self->_populate_from_unixtime;
31             }
32              
33 6 50 33     73 if (defined $self->{year} && defined $self->{month} &&
      33        
      33        
      33        
      33        
34             defined $self->{day} && defined $self->{hour} &&
35             defined $self->{min} && defined $self->{sec}) {
36 6         14 $self->{unixtime} = $self->_get_unixtime;
37             }
38              
39             # use existing time or set it to noon
40 6 50 33     80 if (defined $self->{year} && defined $self->{month} &&
      33        
      33        
      33        
41             defined $self->{day} && (!defined $self->{hour} ||
42             !defined $self->{min} || !defined $self->{sec})) {
43 0 0       0 $self->{hour} = 11 unless (defined $self->{hour});
44 0 0       0 $self->{min} = 0 unless (defined $self->{min});
45 0 0       0 $self->{sec} = 0 unless (defined $self->{sec});
46 0         0 $self->{unixtime} = $self->_get_unixtime;
47             }
48              
49             # if only time is defined, set for today.. just expect hour:min at least
50 6 0 33     38 if ((!defined $self->{year} || !defined $self->{month} || !defined $self->{day})
      33        
      33        
51             && defined $self->{hour} && defined $self->{min}) {
52 0         0 $self->{year} = Date::Format::time2str("%Y", time);
53 0         0 $self->{month} = Date::Format::time2str("%L", time);
54 0         0 $self->{day} = Date::Parser::_strip_zero(time2str("%d", time));
55 0 0       0 $self->{sec} = 0 unless (defined $self->{sec});
56 0         0 $self->{unixtime} = $self->_get_unixtime;
57             }
58              
59 6 50       12 unless (defined $self->{unixtime}) {
60 0         0 warn "couldn't resolve date";
61 0         0 return;
62             }
63              
64 6         18 return $self;
65             }
66              
67             sub _populate_from_unixtime {
68 3     3   4 my ($self) = @_;
69              
70 3         75 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
71             localtime($self->{unixtime});
72              
73 3         7 $self->{year} = $year + 1900;
74 3         5 $self->{month} = $mon;
75 3         6 $self->{day} = $mday;
76 3         3 $self->{hour} = $hour;
77 3         4 $self->{min} = $min;
78 3         4 $self->{sec} = $sec;
79              
80 3         6 return $self;
81             }
82              
83             sub time2str {
84 0     0 1 0 my ($self, $format) = @_;
85              
86 0         0 Date::Format::time2str($format, $self->unixtime);
87             }
88              
89             sub unixtime {
90 70     70 1 1134 my ($self) = @_;
91            
92 70   66     289 $self->{unixtime} ||= $self->_get_unixtime;
93             }
94              
95             sub calc {
96 3     3 1 8 my ($self, $other) = @_;
97              
98 3         5 my $unixtime;
99 3 100       6 if ($self->cmp($other) == 0) {
    100          
    50          
100 1         3 $unixtime = $self->unixtime;
101             } elsif ($self->cmp($other) == -1) {
102 1         2 $unixtime = $self->unixtime + (($other->unixtime - $self->unixtime) / 2);
103 1         2 $unixtime = int($unixtime);
104             } elsif ($self->cmp($other) == 1) {
105 1         3 $unixtime = $self->unixtime - (($self->unixtime - $other->unixtime) / 2);
106 1         2 $unixtime = int($unixtime);
107             }
108              
109 3         12 my $new = Date::Parser::Date->new(unixtime => int($unixtime));
110              
111 3         7 return $new;
112             }
113              
114             sub cmp {
115 13     13 1 954 my ($self, $other) = @_;
116              
117 13 100       27 return -1 if ($self->unixtime < $other->unixtime);
118 8 100       17 return 0 if ($self->unixtime == $other->unixtime);
119 6 50       14 return 1 if ($self->unixtime > $other->unixtime);
120             }
121              
122             sub _get_unixtime {
123 9     9   9 my ($self) = @_;
124              
125 9         8 my $unixtime;
126 9         12 eval { $unixtime = timelocal($self->{sec}, $self->{min}, $self->{hour}, $self->{day}, $self->{month}, $self->{year}) };
  9         37  
127              
128 9         471 return $unixtime;
129             }
130              
131             sub assign {
132 0     0 1   my ($self, $key, $value) = @_;
133              
134 0           $self->{$key} = $value;
135              
136 0           return $self;
137             }
138              
139             1;
140              
141             __END__