add a coercions table
[gitmo/MooseX-Types-DateTime.git] / lib / MooseX / Types / DateTime.pm
1 #!/usr/bin/perl
2
3 package MooseX::Types::DateTime;
4
5 use strict;
6 use warnings;
7
8 our $VERSION = "0.01";
9
10 use DateTime ();
11 use DateTime::Locale ();
12 use DateTime::TimeZone ();
13
14 use MooseX::Types::Moose qw/Num HashRef Str/;
15 use MooseX::Types -declare => [qw( DateTime Duration TimeZone Locale Now )];
16
17 class_type "DateTime";
18 class_type "DateTime::Duration";
19 class_type "DateTime::TimeZone";
20 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
21
22 subtype DateTime, as 'DateTime';
23 subtype Duration, as 'DateTime::Duration';
24 subtype TimeZone, as 'DateTime::TimeZone';
25 subtype Locale,   as 'DateTime::Locale';
26
27 subtype( Now,
28     as Str,
29     where { $_ eq 'now' },
30     Moose::Util::TypeConstraints::optimize_as {
31         no warnings 'uninitialized';
32         !ref($_[0]) and $_[0] eq 'now';
33     },
34 );
35
36 our %coercions = (
37     DateTime => [
38                 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
39                 from HashRef, via { 'DateTime'->new( %$_ ) },
40                 from Now, via { 'DateTime'->now },
41     ],
42     "DateTime::Duration" => [
43                 from Num, via { DateTime::Duration->new( seconds => $_ ) },
44                 from HashRef, via { DateTime::Duration->new( %$_ ) },
45     ],
46     "DateTime::TimeZone" => [
47                 from Str, via { DateTime::TimeZone->new( name => $_ ) },
48     ],
49     "DateTime::Locale" => [
50         from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
51         via { DateTime::Locale->load($_->language_tag) },
52         from Str, via { DateTime::Locale->load($_) },
53     ],
54 );
55
56 for my $type ( "DateTime", DateTime ) {
57     coerce $type => @{ $coercions{DateTime} };
58 }
59
60 for my $type ( "DateTime::Duration", Duration ) {
61     coerce $type => @{ $coercions{"DateTime::Duration"} };
62 }
63
64 for my $type ( "DateTime::TimeZone", TimeZone ) {
65         coerce $type => @{ $coercions{"DateTime::TimeZone"} };
66 }
67
68 for my $type ( "DateTime::Locale", Locale ) {
69         coerce $type => @{ $coercions{"DateTime::Locale"} };
70 }
71
72 __PACKAGE__
73
74 __END__
75
76 =pod
77
78 =head1 NAME
79
80 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
81 Moose
82
83 =head1 SYNOPSIS
84
85 Export Example:
86
87         use MooseX::Types::DateTime qw(TimeZone);
88
89     has time_zone => (
90         isa => TimeZone,
91         is => "rw",
92         coerce => 1,
93     );
94
95     Class->new( time_zone => "Africa/Timbuktu" );
96
97 Namespaced Example:
98
99         use MooseX::Types::DateTime;
100
101     has time_zone => (
102         isa => 'DateTime::TimeZone',
103         is => "rw",
104         coerce => 1,
105     );
106
107     Class->new( time_zone => "Africa/Timbuktu" );
108
109 =head1 DESCRIPTION
110
111 This module packages several L<Moose::Util::TypeConstraints> with coercions,
112 designed to work with the L<DateTime> suite of objects.
113
114 =head1 CONSTRAINTS
115
116 =over 4
117
118 =item L<DateTime>
119
120 A class type for L<DateTime>.
121
122 =over 4
123
124 =item from C<Num>
125
126 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
127 percision, see L<DateTime> for details.
128
129 =item from C<HashRef>
130
131 Calls L<DateTime/new> with the hash entries as arguments.
132
133 =back
134
135 =item L<Duration>
136
137 A class type for L<DateTime::Duration>
138
139 =over 4
140
141 =item from C<Num>
142
143 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
144
145 Note that due to leap seconds, DST changes etc this may not do what you expect.
146 For instance passing in C<86400> is not always equivalent to one day, although
147 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
148 for more details.
149
150 =item from C<HashRef>
151
152 Calls L<DateTime::Duration/new> with the hash entries as arguments.
153
154 =back
155
156 =item L<DateTime::Locale>
157
158 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
159
160 =over 4
161
162 =item from C<Str>
163
164 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
165 L<DateTime::Locale/load>.
166
167 =item from L<Locale::Maktext>
168
169 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
170
171 =item L<DateTime::TimeZone>
172
173 A class type for L<DateTime::TimeZone>.
174
175 =over 4
176
177 =item from C<Str>
178
179 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
180 details on the allowed values.
181
182 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
183
184 =back
185
186 =head1 VERSION CONTROL
187
188 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
189 #moose for commit bits.
190
191 =head1 AUTHOR
192
193 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
194
195 =head1 COPYRIGHT
196
197         Copyright (c) 2008 Yuval Kogman. All rights reserved
198         This program is free software; you can redistribute
199         it and/or modify it under the same terms as Perl itself.
200
201 =cut