$VERSION
=
'0.05'
;
sub
parse_datetime {
my
(
$class
,
$date
,
$zone
) =
@_
;
my
@t
= strptime(
$date
,
$zone
);
return
undef
unless
@t
;
my
(
$ss
,
$mm
,
$hh
,
$day
,
$month
,
$year
,
$offset
) =
@t
;
my
%p
;
if
(
$ss
) {
my
$fraction
=
$ss
-
int
(
$ss
);
if
(
$fraction
) {
my
$nano
=
$fraction
* 1e9;
if
(
$nano
!=
int
(
$nano
) ) {
$nano
++
if
$nano
-
int
(
$nano
) >= 0.5;
}
$p
{ nanosecond } =
int
(
$nano
);
}
$p
{ second } =
int
$ss
;
}
$p
{ minute } =
$mm
if
$mm
;
$p
{ hour } =
$hh
if
$hh
;
$p
{ day } =
$day
if
$day
;
$p
{ month } =
$month
+ 1
if
$month
;
$p
{ year } =
$year
?
$year
+ 1900 : DateTime->now->year;
unless
(
defined
$zone
||
defined
$offset
) {
return
DateTime->new(
%p
,
time_zone
=>
'local'
,
);
}
if
(
$zone
) {
if
( DateTime::TimeZone->is_valid_name(
$zone
) ) {
return
DateTime->new(
%p
,
time_zone
=>
$zone
,
);
}
else
{
return
DateTime->new(
%p
,
time_zone
=>
DateTime::TimeZone::offset_as_string( tz_offset(
$zone
) ),
);
}
}
return
DateTime->new(
%p
,
time_zone
=>
DateTime::TimeZone::offset_as_string(
$offset
),
);
}
1;