Advertisement
three18ti

MooseX/Types/ISO8601.pm exerpt

Mar 27th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.98 KB | None | 0 0
  1. {
  2.     my %coerce = (
  3.         ISO8601TimeDurationStr, 'PT%02HH%02MM%02S.%06NS',
  4.         ISO8601DateDurationStr, 'P%02YY%02mM%02dD',
  5.         ISO8601DateTimeDurationStr, 'P%02YY%02mM%02dDT%02HH%02MM%02S.%06NS',
  6.     );
  7.  
  8.     foreach my $type_name (keys %coerce) {
  9.  
  10.         my $code = sub {
  11.             my $str = DateTime::Format::Duration->new(
  12.                 normalize => 1,
  13.                 pattern   => $coerce{$type_name},
  14.             )
  15.             ->format_duration( shift );
  16.  
  17.             # Remove fractional seconds if there aren't any.
  18.             $str =~ s/\.0+S$/S/;
  19.             return $str;
  20.         };
  21.  
  22.         coerce $type_name,
  23.         from Duration,
  24.             via { $code->($_) },
  25.         from Num,
  26.             via { $code->(to_Duration($_)) };
  27.             # FIXME - should be able to say => via_type 'DateTime::Duration';
  28.             # nothingmuch promised to make that syntax happen if I got
  29.             # Stevan to approve and/or wrote a test case.
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement