File Coverage

blib/lib/MooseX/Types/DateTime/MySQL.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::DateTime::MySQL;
2 2         14 use MooseX::Types -declare => [qw/
3             MySQLDateTime
4             MySQLDate
5             MySQLTime
6 2     2   477625 /];
  2         343841  
7 2     2   6832 use MooseX::Types::DateTime qw/ DateTime Duration /;
  2         125181  
  2         11  
8 2     2   2855 use MooseX::Types::Moose qw/ Str Int /;
  2         3  
  2         9  
9 2     2   7660 use DateTime::Format::MySQL;
  2         33850  
  2         52  
10 2     2   12 use DateTime::Duration;
  2         2  
  2         27  
11 2     2   1014 use DateTime::Format::Duration;
  2         7705  
  2         105  
12 2     2   11 use namespace::clean;
  2         2  
  2         13  
13              
14             our $VERSION = '0.003';
15              
16             subtype MySQLDateTime,
17             as Str,
18             where { /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/ };
19              
20             subtype MySQLDate,
21             as Str,
22             where { /^\d{4}-\d{2}-\d{2}$/ };
23              
24             subtype MySQLTime,
25             as Str,
26             where { /^-?\d?\d{2}:\d{2}:\d{2}$/ };
27              
28             coerce MySQLDateTime,
29             from DateTime,
30             via { DateTime::Format::MySQL->format_datetime($_) };
31              
32             coerce MySQLDate,
33             from DateTime,
34             via { DateTime::Format::MySQL->format_date($_) };
35              
36             coerce MySQLTime,
37             from DateTime,
38             via { DateTime::Format::MySQL->format_time($_) };
39              
40             {
41             my $formatter = DateTime::Format::Duration->new(pattern => "%H:%M:%S", normalize => 1);
42             coerce MySQLTime,
43             from Int,
44             via { $formatter->format_duration(DateTime::Duration->new(seconds => $_)) };
45             }
46              
47             coerce DateTime,
48             from MySQLDateTime,
49             via { DateTime::Format::MySQL->parse_datetime($_) };
50              
51             coerce DateTime,
52             from MySQLDate,
53             via { DateTime::Format::MySQL->parse_date($_) };
54              
55             coerce MySQLDateTime,
56             from MySQLDate,
57             via { $_ . " 00:00:00" };
58              
59             1;
60              
61             =head1 NAME
62              
63             MooseX::Types::DateTime::MySQL - Joins MooseX::Types::DateTime and DateTime::Format::MySQL
64              
65             =head1 DESCRIPTION
66              
67             Defines a C<MySQLDateTime> type which will round-trip coerce to the C<DateTime> type
68             from L<MooseX::Types::DateTime>.
69              
70             =head1 TYPES
71              
72             =head2 MySQLDateTime
73              
74             Coerces to and from C<DateTime>.
75              
76             =head2 MySQLDate
77              
78             Coerces to and from C<DateTime> and will coerce to C<MySQLDateTime>.
79              
80             =head2 MySQLTime
81              
82             Coerces from C<Int> (duration) and C<DateTime> (by extracting only the time part).
83              
84             =head1 BUGS
85              
86             =over
87              
88             =item The MySQL datetime detection regex is imperfect as it just looks for C< \d >s, rather than things which are known valid months.
89              
90             =item Probably more
91              
92             =back
93              
94             =head1 AUTHOR
95              
96             Tomas Doran (t0m) C<<bobtfish@bobtfish.net>>
97              
98             =head1 COPYRIGHT
99              
100             Copyright state51.
101              
102             =head1 LICENSE
103              
104             Copyright 2009 the above author(s).
105              
106             This sofware is free software, and is licensed under the same terms as perl itself.
107              
108             =cut
109