File Coverage

blib/lib/Time/List.pm
Criterion Covered Total %
statement 82 96 85.4
branch 29 40 72.5
condition 5 9 55.5
subroutine 9 9 100.0
pod 2 2 100.0
total 127 156 81.4


line stmt bran cond sub pod time code
1             package Time::List;
2 7     7   102686 use 5.008_001;
  7         20  
  7         230  
3 7     7   32 use strict;
  7         10  
  7         208  
4 7     7   28 use warnings;
  7         10  
  7         219  
5 7     7   3995 use Time::Piece;
  7         75434  
  7         35  
6 7     7   3240 use Time::List::Rows;
  7         19  
  7         238  
7 7     7   45 use Class::Accessor::Lite;
  7         10  
  7         39  
8 7     7   317 use Time::List::Constant;
  7         12  
  7         128  
9              
10             our $VERSION = '0.13';
11              
12             my $unit_time = {
13             DAY() => 3600 * 24 ,
14             WEEK() => 3600 * 24 * 7 ,
15             HOUR() => 3600 ,
16             };
17              
18             my %DEFAULTS = (
19             time_unit => DAY() ,
20             output_type => ARRAY() ,
21             limit_rows => 0 ,
22             input_strftime_form => '%Y-%m-%d %H:%M:%S',
23             output_strftime_form => '%Y-%m-%d %H:%M:%S',
24             show_end_time => 0 ,
25             boundary_included => 1 ,
26             end_time_separate_chars => '~' ,
27             create_summary => 0 ,
28             summary_key_name => "summary" ,
29             filter => undef ,
30             filter_keys => [] ,
31             );
32              
33             Class::Accessor::Lite->mk_accessors(keys %DEFAULTS);
34              
35             sub new {
36 8     8 1 17 my $class = shift;
37            
38 0         0 my $self = bless {
39             %DEFAULTS,
40 8 50       124 @_ == 1 ? %{ $_[0] } : @_,
41             }, $class;
42 8         26 $self;
43             }
44              
45             sub get_list{
46 24     24 1 9104 my ($self , $start_time , $end_time) = @_;
47              
48 24         76 my $output_type = $self->output_type;
49 24         140 my $time_unit = $self->time_unit;
50 24         97 my $input_strftime_form = $self->input_strftime_form;
51 24         114 my $output_strftime_form = $self->output_strftime_form;
52            
53 24         78 my $time_array = [];
54 24         58 my $show_end_time = $self->show_end_time;
55 24 100       104 if($time_unit == MONTH){
56 11         34 my $start_tp = Time::Piece->strptime($start_time , $input_strftime_form);
57 11         231 my $end_tp = Time::Piece->strptime($end_time , $input_strftime_form);
58            
59 11         195 my ($start_year , $start_month) = ($start_tp->strftime('%Y') , $start_tp->strftime('%m'));
60            
61 11         451 my ($end_year , $end_month) = ($end_tp->strftime('%Y') , $end_tp->strftime('%m'));
62              
63 11 50       128 if($self->boundary_included){
64 11   100     103 while($start_year < $end_year || ($start_year == $end_year && $start_month <= $end_month)){
      66        
65 41 100       48 if($show_end_time){
66 20         24 my ( $next_year , $next_month) = ($start_year , $start_month);
67 20 100       34 if(++$next_month > 12){
68 2         3 $next_month = 1;
69 2         2 $next_year++;
70             }
71 20         53 push @$time_array ,
72             Time::Piece->strptime("$start_year:$start_month" , '%Y:%m')->strftime('%s')
73             . "\t"
74             . (Time::Piece->strptime("$next_year:$next_month" , '%Y:%m')->strftime('%s') - 1);
75             }else{
76 21         56 push @$time_array , Time::Piece->strptime("$start_year:$start_month" , '%Y:%m')->strftime('%s');
77             }
78 41 100       1359 if(++$start_month > 12){
79 2         1 $start_month = 1;
80 2         10 $start_year ++;
81             }
82             }
83             }else{
84 0   0     0 while($start_year < $end_year || $start_month < $end_month){
85 0 0       0 if($show_end_time){
86 0         0 my ( $next_year , $next_month) = ($start_year , $start_month);
87 0 0       0 if(++$next_month > 12){
88 0         0 $next_month = 1;
89 0         0 $next_year++;
90             }
91 0         0 push @$time_array ,
92             Time::Piece->strptime("$start_year:$start_month" , '%Y:%m')->strftime('%s')
93             . "\t"
94             . (Time::Piece->strptime("$next_year:$next_month" , '%Y:%m')->strftime('%s') - 1);
95             }else{
96 0         0 push @$time_array , Time::Piece->strptime("$start_year:$start_month" , '%Y:%m')->strftime('%s');
97             }
98 0 0       0 if(++$start_month > 12){
99 0         0 $start_month = 1;
100 0         0 $start_year ++;
101             }
102             }
103              
104             }
105             }else{
106 13         33 my $unit_time_value = $unit_time->{$time_unit};
107 13 50       33 die 'set time unit' unless $unit_time_value;
108 13         59 my $start_tp_time = Time::Piece->strptime($start_time , $input_strftime_form)->strftime('%s');
109 13 100       1003 my $start_posix_time = Time::Piece->strptime(
110             localtime($start_tp_time + $unit_time_value - 1 , '%s')->strftime($time_unit == HOUR() ? '%Y-%m-%d %H:00:00' : '%Y-%m-%d 00:00:00') ,
111             '%Y-%m-%d %H:%M:%S')
112             ->strftime('%s');
113 13         501 my $end_posix_time = Time::Piece->strptime($end_time , $input_strftime_form)->strftime('%s');
114            
115 13 100       359 if($self->boundary_included){
116 12 100       69 if($show_end_time){
117 4         22 push @$time_array ,
118             $start_posix_time
119             . "\t"
120             . ($start_posix_time + $unit_time_value - 1);
121             }else{
122 8         22 push @$time_array , $start_posix_time;
123             }
124              
125 12         42 while($start_posix_time < $end_posix_time){
126 164         133 $start_posix_time += $unit_time_value;
127 164 100       180 if($show_end_time){
128 36         97 push @$time_array ,
129             $start_posix_time
130             . "\t"
131             . ($start_posix_time + $unit_time_value - 1);
132             }else{
133 128         309 push @$time_array , $start_posix_time;
134             }
135             }
136             }else{
137 1         10 while($start_posix_time < $end_posix_time){
138 24 50       19 if($show_end_time){
139 0         0 push @$time_array ,
140             $start_posix_time
141             . "\t"
142             . ($start_posix_time + $unit_time_value - 1);
143             }else{
144 24         20 push @$time_array , $start_posix_time;
145             }
146 24         37 $start_posix_time += $unit_time_value;
147             }
148             }
149             }
150              
151 24         84 my $end_time_separate_chars = $self->end_time_separate_chars;
152 24 100       137 if($output_type == ARRAY){
    100          
    50          
153 14 100       31 if($show_end_time){
154 37         1979 return [map{
155 6         14 my ($t1 , $t2) = split("\t" , $_);
156 37         87 localtime($t1)->strftime($output_strftime_form)
157             . $end_time_separate_chars
158             . localtime($t2)->strftime($output_strftime_form)
159             }@$time_array];
160             }else{
161 8         16 return [map{localtime($_)->strftime($output_strftime_form)}@$time_array];
  83         2593  
162             }
163             }elsif($output_type == HASH){
164 6 100       15 if($show_end_time){
165             return {
166 20         692 map{
167 2         4 my ($t1 , $t2) = split("\t" , $_);
168 20         32 localtime($t1)->strftime($output_strftime_form)
169             . $end_time_separate_chars
170             . localtime($t2)->strftime($output_strftime_form) => {}
171             }@$time_array};
172             }else{
173 4         8 return {map{localtime($_)->strftime($output_strftime_form) => {}}@$time_array};
  68         1263  
174             }
175             }elsif($output_type == ROWS){
176 4         26 my %args = %$self;
177 4         21 return Time::List::Rows->new(%args , time_array => $time_array);
178             }else{
179 0           die 'set output type';
180             }
181             }
182              
183             1;
184              
185              
186             1;
187             __END__