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