Commit | Line | Data |
4664d531 |
1 | package MooseX::Types::DateTime; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
c003a0c6 |
6 | use 5.008003; |
2820aea9 |
7 | use Moose 0.41 (); |
8 | use DateTime 0.4302 (); |
9 | use DateTime::Duration 0.4302 (); |
10 | use DateTime::Locale 0.4001 (); |
11 | use DateTime::TimeZone 0.95 (); |
4664d531 |
12 | |
2820aea9 |
13 | use MooseX::Types::Moose 0.30 qw/Num HashRef Str/; |
d3c47673 |
14 | |
2820aea9 |
15 | use namespace::clean 0.08; |
d3c47673 |
16 | |
2820aea9 |
17 | use MooseX::Types 0.30 -declare => [qw( DateTime Duration TimeZone Locale Now )]; |
4664d531 |
18 | |
19 | class_type "DateTime"; |
ec3f7a50 |
20 | class_type "DateTime::Duration"; |
4664d531 |
21 | class_type "DateTime::TimeZone"; |
22 | class_type "DateTime::Locale::root" => { name => "DateTime::Locale" }; |
23 | |
6903978c |
24 | subtype DateTime, as 'DateTime'; |
25 | subtype Duration, as 'DateTime::Duration'; |
26 | subtype TimeZone, as 'DateTime::TimeZone'; |
39b3fe23 |
27 | subtype Locale, as 'DateTime::Locale'; |
28 | |
29 | subtype( Now, |
30 | as Str, |
31 | where { $_ eq 'now' }, |
f25d5b8f |
32 | ($Moose::VERSION >= 2.0100 |
33 | ? Moose::Util::TypeConstraints::inline_as { |
34 | 'no warnings "uninitialized";'. |
35 | '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"'; |
36 | } |
37 | : Moose::Util::TypeConstraints::optimize_as { |
38 | no warnings 'uninitialized'; |
39 | !ref($_[0]) and $_[0] eq 'now'; |
40 | } |
41 | ), |
39b3fe23 |
42 | ); |
6903978c |
43 | |
06eee7b1 |
44 | our %coercions = ( |
45 | DateTime => [ |
33a23d16 |
46 | from Num, via { 'DateTime'->from_epoch( epoch => $_ ) }, |
47 | from HashRef, via { 'DateTime'->new( %$_ ) }, |
48 | from Now, via { 'DateTime'->now }, |
06eee7b1 |
49 | ], |
50 | "DateTime::Duration" => [ |
33a23d16 |
51 | from Num, via { DateTime::Duration->new( seconds => $_ ) }, |
52 | from HashRef, via { DateTime::Duration->new( %$_ ) }, |
06eee7b1 |
53 | ], |
54 | "DateTime::TimeZone" => [ |
33a23d16 |
55 | from Str, via { DateTime::TimeZone->new( name => $_ ) }, |
06eee7b1 |
56 | ], |
57 | "DateTime::Locale" => [ |
58 | from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"), |
59 | via { DateTime::Locale->load($_->language_tag) }, |
60 | from Str, via { DateTime::Locale->load($_) }, |
61 | ], |
62 | ); |
63 | |
6903978c |
64 | for my $type ( "DateTime", DateTime ) { |
06eee7b1 |
65 | coerce $type => @{ $coercions{DateTime} }; |
6903978c |
66 | } |
67 | |
68 | for my $type ( "DateTime::Duration", Duration ) { |
06eee7b1 |
69 | coerce $type => @{ $coercions{"DateTime::Duration"} }; |
6903978c |
70 | } |
71 | |
72 | for my $type ( "DateTime::TimeZone", TimeZone ) { |
33a23d16 |
73 | coerce $type => @{ $coercions{"DateTime::TimeZone"} }; |
6903978c |
74 | } |
4664d531 |
75 | |
06eee7b1 |
76 | for my $type ( "DateTime::Locale", Locale ) { |
33a23d16 |
77 | coerce $type => @{ $coercions{"DateTime::Locale"} }; |
06eee7b1 |
78 | } |
4664d531 |
79 | |
80 | __PACKAGE__ |
81 | |
82 | __END__ |
83 | |
84 | =pod |
85 | |
86 | =head1 NAME |
87 | |
88 | MooseX::Types::DateTime - L<DateTime> related constraints and coercions for |
89 | Moose |
90 | |
91 | =head1 SYNOPSIS |
92 | |
6903978c |
93 | Export Example: |
94 | |
33a23d16 |
95 | use MooseX::Types::DateTime qw(TimeZone); |
6903978c |
96 | |
97 | has time_zone => ( |
98 | isa => TimeZone, |
99 | is => "rw", |
100 | coerce => 1, |
101 | ); |
102 | |
103 | Class->new( time_zone => "Africa/Timbuktu" ); |
104 | |
9347d006 |
105 | =for stopwords Namespaced |
106 | |
6903978c |
107 | Namespaced Example: |
108 | |
33a23d16 |
109 | use MooseX::Types::DateTime; |
4664d531 |
110 | |
111 | has time_zone => ( |
6903978c |
112 | isa => 'DateTime::TimeZone', |
4664d531 |
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 | |
ec3f7a50 |
130 | A class type for L<DateTime>. |
131 | |
132 | =over 4 |
133 | |
134 | =item from C<Num> |
135 | |
9347d006 |
136 | Uses L<DateTime/from_epoch>. Floating values will be used for sub-second |
137 | precision, see L<DateTime> for details. |
ec3f7a50 |
138 | |
139 | =item from C<HashRef> |
140 | |
141 | Calls L<DateTime/new> with the hash entries as arguments. |
142 | |
143 | =back |
144 | |
6903978c |
145 | =item L<Duration> |
ec3f7a50 |
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 |
4664d531 |
165 | |
166 | =item L<DateTime::Locale> |
167 | |
ec3f7a50 |
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 |
4664d531 |
175 | L<DateTime::Locale/load>. |
176 | |
ec3f7a50 |
177 | =item from L<Locale::Maktext> |
178 | |
179 | The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>. |
180 | |
4664d531 |
181 | =item L<DateTime::TimeZone> |
182 | |
ec3f7a50 |
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. |
4664d531 |
193 | |
ec3f7a50 |
194 | =back |
4664d531 |
195 | |
a43ee170 |
196 | =back |
197 | |
198 | =back |
199 | |
f83c2e2d |
200 | =head1 SEE ALSO |
201 | |
256b5640 |
202 | L<MooseX::Types::DateTime::MoreCoercions> |
f83c2e2d |
203 | |
204 | L<DateTime>, L<DateTimeX::Easy> |
205 | |
4664d531 |
206 | =head1 AUTHOR |
207 | |
208 | Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt> |
209 | |
f83c2e2d |
210 | John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt> |
211 | |
4664d531 |
212 | =head1 COPYRIGHT |
213 | |
33a23d16 |
214 | Copyright (c) 2008 Yuval Kogman. All rights reserved |
215 | This program is free software; you can redistribute |
216 | it and/or modify it under the same terms as Perl itself. |
4664d531 |
217 | |
218 | =cut |