our
$VERSION
=
'0.03'
;
has
'format'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
default
=>
"%Y-%m-%d"
);
has
'locale'
=> (
is
=>
'rw'
,
isa
=>
'Str'
);
has
'time_zone'
=> (
is
=>
'rw'
,
isa
=>
'Str'
);
has
'date_start'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
clearer
=>
'clear_date_start'
);
has
'date_end'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
clearer
=>
'clear_date_end'
);
has
'+size'
=> (
default
=>
'10'
);
my
$dp_to_dt
= {
"d"
=>
"\%e"
,
"dd"
=>
"\%1"
,
"o"
=>
"\%4"
,
"oo"
=>
"\%j"
,
"D"
=>
"\%a"
,
"DD"
=>
"\%A"
,
"m"
=>
"\%5"
,
"mm"
=>
"\%3"
,
"M"
=>
"\%b"
,
"MM"
=>
"\%B"
,
"y"
=>
"\%2"
,
"yy"
=>
"\%Y"
,
"@"
=>
"\%s"
,
};
sub
deflate {
my
$self
=
shift
;
my
$value
=
$self
->value;
return
unless
ref
$value
eq
'DateTime'
;
my
$format
=
$self
->get_strf_format;
my
$string
=
$value
->strftime(
$format
);
return
$string
;
}
sub
validate {
my
$self
=
shift
;
my
$format
=
$self
->get_strf_format;
my
$strp
= DateTime::Format::Strptime->new(
pattern
=>
$format
);
my
$dt
=
eval
{
$strp
->parse_datetime(
$self
->value ) };
unless
(
$dt
) {
$self
->add_error(
$strp
->errmsg || $@ );
return
;
}
$self
->_set_value(
$dt
);
my
$val_strp
= DateTime::Format::Strptime->new(
pattern
=>
"%Y-%m-%d"
);
if
(
$self
->date_start ) {
my
$date_start
=
$val_strp
->parse_datetime(
$self
->date_start );
die
"date_start: "
.
$val_strp
->errmsg
unless
$date_start
;
my
$cmp
= DateTime->compare(
$date_start
,
$dt
);
$self
->add_error(
"Date is too early"
)
if
$cmp
eq 1;
}
if
(
$self
->date_end ) {
my
$date_end
=
$val_strp
->parse_datetime(
$self
->date_end );
die
"date_end: "
.
$val_strp
->errmsg
unless
$date_end
;
my
$cmp
= DateTime->compare(
$date_end
,
$dt
);
$self
->add_error(
"Date is too late"
)
if
$cmp
eq -1;
}
}
sub
get_strf_format {
my
$self
=
shift
;
return
$self
->
format
if
$self
->
format
=~ /\%/;
my
$format
=
$self
->
format
;
foreach
my
$dpf
(
reverse
sort
keys
%{
$dp_to_dt
} ) {
my
$strf
=
$dp_to_dt
->{
$dpf
};
$format
=~ s/
$dpf
/
$strf
/g;
}
$format
=~ s/\%1/\
%d
/g,
$format
=~ s/\%2/\
%y
/g,
$format
=~ s/\%3/\
%m
/g,
$format
=~ s/\%4/\%{day_of_year}/g,
$format
=~ s/\%5/\%{day_of_month}/g,
return
$format
;
}
__PACKAGE__->meta->make_immutable;
1;