Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- my %coerce = (
- ISO8601TimeDurationStr, 'PT%02HH%02MM%02S.%06NS',
- ISO8601DateDurationStr, 'P%02YY%02mM%02dD',
- ISO8601DateTimeDurationStr, 'P%02YY%02mM%02dDT%02HH%02MM%02S.%06NS',
- );
- foreach my $type_name (keys %coerce) {
- my $code = sub {
- my $str = DateTime::Format::Duration->new(
- normalize => 1,
- pattern => $coerce{$type_name},
- )
- ->format_duration( shift );
- # Remove fractional seconds if there aren't any.
- $str =~ s/\.0+S$/S/;
- return $str;
- };
- coerce $type_name,
- from Duration,
- via { $code->($_) },
- from Num,
- via { $code->(to_Duration($_)) };
- # FIXME - should be able to say => via_type 'DateTime::Duration';
- # nothingmuch promised to make that syntax happen if I got
- # Stevan to approve and/or wrote a test case.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement