File Coverage

blib/lib/MooseX/Types/Time/Piece.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 36 36 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Time::Piece;
2              
3 2     2   601719 use strict;
  2         5  
  2         69  
4 2     2   11 use warnings;
  2         3  
  2         48  
5 2     2   30375 use namespace::autoclean;
  2         35028  
  2         11  
6              
7             our $VERSION = '0.10';
8              
9 2     2   132 use Carp ();
  2         4  
  2         31  
10 2     2   1504 use Time::Piece ();
  2         15456  
  2         50  
11 2     2   15 use Time::Seconds ();
  2         4  
  2         38  
12 2     2   13 use Try::Tiny;
  2         5  
  2         166  
13              
14 2     2   2021 use MooseX::Types -declare => [qw( Time Duration )];
  2         1757072  
  2         18  
15 2     2   10809 use MooseX::Types::Moose qw( ArrayRef Num Str );
  2         31763  
  2         24  
16              
17             class_type 'Time::Piece';
18             class_type 'Time::Seconds';
19              
20             subtype Time, as 'Time::Piece';
21             subtype Duration, as 'Time::Seconds';
22              
23             my $DEFAULT_FORMAT = '%a, %d %b %Y %H:%M:%S %Z';
24             my $ISO_FORMAT = '%Y-%m-%dT%H:%M:%S';
25              
26             for my $type ( 'Time::Piece', Time )
27             {
28             coerce $type,
29             from Num, via
30             {
31             Time::Piece->new($_)
32             },
33             from Str, via
34             {
35             my $time = $_;
36             return try {
37             Time::Piece->strptime( $time, $ISO_FORMAT );
38             } catch {
39             # error message from strptime does say much
40             Carp::confess "Error parsing time '$time' with format '$ISO_FORMAT'";
41             };
42             },
43             from ArrayRef, via
44             {
45             my @args = @$_;
46             return try {
47             Time::Piece->strptime(@args);
48             } catch {
49             $args[1] ||= $DEFAULT_FORMAT; # if only 1 arg
50             Carp::confess "Error parsing time '$args[0]' with format '$args[1]'";
51             };
52             };
53             }
54              
55             for my $type ( 'Time::Seconds', Duration )
56             {
57             coerce $type,
58             from Num, via { Time::Seconds->new($_) };
59             }
60              
61             1;
62              
63             __END__
64              
65             =head1 NAME
66              
67             MooseX::Types::Time::Piece - Time::Piece type and coercions for Moose
68              
69             =head1 SYNOPSIS
70              
71             package Foo;
72              
73             use Moose;
74             use MooseX::Types::Time::Piece qw( Time Duration );
75              
76             has 'time' => (
77             is => 'rw',
78             isa => Time,
79             coerce => 1,
80             );
81              
82             has 'duration' => (
83             is => 'rw',
84             isa => Duration,
85             coerce => 1,
86             );
87              
88             # ...
89              
90             my $f = Foo->new;
91             $f->time( Time::Piece->new ) # no coercion
92             $f->time( time() ); # coerce from Num
93             $f->time( '2012-12-31T23:59:59' ); # coerce from Str
94             $f->time( ['2012-12-31', '%Y-%m-%d'] ); # coerce from ArrayRef
95             $f->duration( Time::Seconds::ONE_DAY * 2 );
96              
97             =head1 DESCRIPTION
98              
99             This module provides L<Moose> type constraints and coercions for using
100             L<Time::Piece> objects as Moose attributes.
101              
102             =head1 EXPORTS
103              
104             The following type constants provided by L<MooseX::Types> must be explicitly
105             imported. The full class name may also be used (as strings with quotes) without
106             importing the constant declarations.
107              
108             =head2 Time
109              
110             A class type for L<Time::Piece>.
111              
112             =over
113              
114             =item coerce from C<Num>
115              
116             The number is interpreted as the seconds since the system epoch
117             as accepted by L<localtime()|perlfunc/localtime>.
118              
119             =item coerce from C<Str>
120              
121             The string is expected to be in ISO 8601 date/time format,
122             e.g. C<'2012-12-31T23:59:59'>. See also L<Time::Piece/YYYY-MM-DDThh:mm:ss>.
123              
124             =item coerce from C<ArrayRef>
125              
126             The arrayref is expected to contain 2 string values, the time and the time format,
127             as accepted by L<strptime()|Time::Piece/"Date Parsing">.
128              
129             =back
130              
131             An exception is thrown during coercion if the given time does not match the
132             expected/given format, or the given time or format is invalid.
133              
134             =head2 Duration
135              
136             A class type for L<Time::Seconds>.
137              
138             =over
139              
140             =item coerce from C<Num>
141              
142             The number is interpreted as seconds in duration.
143              
144             =back
145              
146             =head1 SEE ALSO
147              
148             L<Time::Piece>, L<MooseX::Types>
149              
150             =head1 AUTHOR
151              
152             Steven Lee, C<< <stevenl at cpan.org> >>
153              
154             =head1 LICENSE AND COPYRIGHT
155              
156             Copyright (C) 2012 Steven Lee
157              
158             This program is free software; you can redistribute it and/or modify it
159             under the same terms as Perl itself.
160              
161             =cut