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