use
5.008_001;
our
$VERSION
=
'0.08'
;
my
$unit_time
= {
DAY() => 3600 * 24 ,
WEEK() => 3600 * 24 * 7 ,
HOUR() => 3600 ,
};
my
%DEFAULTS
= (
time_unit
=> DAY() ,
output_type
=> ARRAY() ,
limit_rows
=> 0 ,
input_strftime_form
=>
'%Y-%m-%d %H:%M:%S'
,
output_strftime_form
=>
'%Y-%m-%d %H:%M:%S'
,
show_end_time
=> 0 ,
boundary_included
=> 1 ,
end_time_separate_chars
=>
'~'
,
create_summary
=> 0 ,
summary_key_name
=>
"summary"
);
Class::Accessor::Lite->mk_accessors(
keys
%DEFAULTS
);
sub
new {
my
$class
=
shift
;
my
$self
=
bless
{
%DEFAULTS
,
@_
== 1 ? %{
$_
[0] } :
@_
,
},
$class
;
$self
;
}
sub
get_list{
my
(
$self
,
$start_time
,
$end_time
) =
@_
;
my
$output_type
=
$self
->output_type;
my
$time_unit
=
$self
->time_unit;
my
$input_strftime_form
=
$self
->input_strftime_form;
my
$output_strftime_form
=
$self
->output_strftime_form;
my
$time_array
= [];
my
$show_end_time
=
$self
->show_end_time;
if
(
$time_unit
== MONTH){
my
$start_tp
= Time::Piece->strptime(
$start_time
,
$input_strftime_form
);
my
$end_tp
= Time::Piece->strptime(
$end_time
,
$input_strftime_form
);
my
(
$start_year
,
$start_month
) = (
$start_tp
->strftime(
'%Y'
) ,
$start_tp
->strftime(
'%m'
));
my
(
$end_year
,
$end_month
) = (
$end_tp
->strftime(
'%Y'
) ,
$end_tp
->strftime(
'%m'
));
if
(
$self
->boundary_included){
while
(
$start_year
<
$end_year
|| (
$start_year
==
$end_year
&&
$start_month
<=
$end_month
)){
if
(
$show_end_time
){
my
(
$next_year
,
$next_month
) = (
$start_year
,
$start_month
);
if
(++
$next_month
> 12){
$next_month
= 1;
$next_year
++;
}
push
@$time_array
,
Time::Piece->strptime(
"$start_year:$start_month"
,
'%Y:%m'
)->strftime(
'%s'
)
.
"\t"
. (Time::Piece->strptime(
"$next_year:$next_month"
,
'%Y:%m'
)->strftime(
'%s'
) - 1);
}
else
{
push
@$time_array
, Time::Piece->strptime(
"$start_year:$start_month"
,
'%Y:%m'
)->strftime(
'%s'
);
}
if
(++
$start_month
> 12){
$start_month
= 1;
$start_year
++;
}
}
}
else
{
while
(
$start_year
<
$end_year
||
$start_month
<
$end_month
){
if
(
$show_end_time
){
my
(
$next_year
,
$next_month
) = (
$start_year
,
$start_month
);
if
(++
$next_month
> 12){
$next_month
= 1;
$next_year
++;
}
push
@$time_array
,
Time::Piece->strptime(
"$start_year:$start_month"
,
'%Y:%m'
)->strftime(
'%s'
)
.
"\t"
. (Time::Piece->strptime(
"$next_year:$next_month"
,
'%Y:%m'
)->strftime(
'%s'
) - 1);
}
else
{
push
@$time_array
, Time::Piece->strptime(
"$start_year:$start_month"
,
'%Y:%m'
)->strftime(
'%s'
);
}
if
(++
$start_month
> 12){
$start_month
= 1;
$start_year
++;
}
}
}
}
else
{
my
$unit_time_value
=
$unit_time
->{
$time_unit
};
die
'set time unit'
unless
$unit_time_value
;
my
$start_tp_time
= Time::Piece->strptime(
$start_time
,
$input_strftime_form
)->strftime(
'%s'
);
my
$start_posix_time
= Time::Piece->strptime(
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'
) ,
'%Y-%m-%d %H:%M:%S'
)
->strftime(
'%s'
);
my
$end_posix_time
= Time::Piece->strptime(
$end_time
,
$input_strftime_form
)->strftime(
'%s'
);
if
(
$self
->boundary_included){
if
(
$show_end_time
){
push
@$time_array
,
$start_posix_time
.
"\t"
. (
$start_posix_time
+
$unit_time_value
- 1);
}
else
{
push
@$time_array
,
$start_posix_time
;
}
while
(
$start_posix_time
<
$end_posix_time
){
$start_posix_time
+=
$unit_time_value
;
if
(
$show_end_time
){
push
@$time_array
,
$start_posix_time
.
"\t"
. (
$start_posix_time
+
$unit_time_value
- 1);
}
else
{
push
@$time_array
,
$start_posix_time
;
}
}
}
else
{
while
(
$start_posix_time
<
$end_posix_time
){
if
(
$show_end_time
){
push
@$time_array
,
$start_posix_time
.
"\t"
. (
$start_posix_time
+
$unit_time_value
- 1);
}
else
{
push
@$time_array
,
$start_posix_time
;
}
$start_posix_time
+=
$unit_time_value
;
}
}
}
my
$end_time_separate_chars
=
$self
->end_time_separate_chars;
if
(
$output_type
== ARRAY){
if
(
$show_end_time
){
return
[
map
{
my
(
$t1
,
$t2
) =
split
(
"\t"
,
$_
);
localtime
(
$t1
)->strftime(
$output_strftime_form
)
.
$end_time_separate_chars
.
localtime
(
$t2
)->strftime(
$output_strftime_form
)
}
@$time_array
];
}
else
{
return
[
map
{
localtime
(
$_
)->strftime(
$output_strftime_form
)}
@$time_array
];
}
}
elsif
(
$output_type
== HASH){
if
(
$show_end_time
){
return
{
map
{
my
(
$t1
,
$t2
) =
split
(
"\t"
,
$_
);
localtime
(
$t1
)->strftime(
$output_strftime_form
)
.
$end_time_separate_chars
.
localtime
(
$t2
)->strftime(
$output_strftime_form
) => {}
}
@$time_array
};
}
else
{
return
{
map
{
localtime
(
$_
)->strftime(
$output_strftime_form
) => {}}
@$time_array
};
}
}
elsif
(
$output_type
== ROWS){
my
%args
=
%$self
;
Time::List::Rows->new(
%args
,
time_array
=>
$time_array
);
}
else
{
die
'set output type'
;
}
}
1;
1;