Commit | Line | Data |
4664d531 |
1 | #!/usr/bin/perl |
2 | |
3 | package MooseX::Types::DateTime; |
4 | |
5 | use strict; |
6 | use warnings; |
7 | |
96293a4d |
8 | our $VERSION = "0.01"; |
4664d531 |
9 | |
10 | use DateTime (); |
11 | use DateTime::Locale (); |
12 | use DateTime::TimeZone (); |
13 | |
6903978c |
14 | use MooseX::Types::Moose qw/Num HashRef Str/; |
39b3fe23 |
15 | use MooseX::Types -declare => [qw( DateTime Duration TimeZone Locale Now )]; |
4664d531 |
16 | |
17 | class_type "DateTime"; |
ec3f7a50 |
18 | class_type "DateTime::Duration"; |
4664d531 |
19 | class_type "DateTime::TimeZone"; |
20 | class_type "DateTime::Locale::root" => { name => "DateTime::Locale" }; |
21 | |
6903978c |
22 | subtype DateTime, as 'DateTime'; |
23 | subtype Duration, as 'DateTime::Duration'; |
24 | subtype TimeZone, as 'DateTime::TimeZone'; |
39b3fe23 |
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 | ); |
6903978c |
35 | |
06eee7b1 |
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 | |
6903978c |
56 | for my $type ( "DateTime", DateTime ) { |
06eee7b1 |
57 | coerce $type => @{ $coercions{DateTime} }; |
6903978c |
58 | } |
59 | |
60 | for my $type ( "DateTime::Duration", Duration ) { |
06eee7b1 |
61 | coerce $type => @{ $coercions{"DateTime::Duration"} }; |
6903978c |
62 | } |
63 | |
64 | for my $type ( "DateTime::TimeZone", TimeZone ) { |
06eee7b1 |
65 | coerce $type => @{ $coercions{"DateTime::TimeZone"} }; |
6903978c |
66 | } |
4664d531 |
67 | |
06eee7b1 |
68 | for my $type ( "DateTime::Locale", Locale ) { |
69 | coerce $type => @{ $coercions{"DateTime::Locale"} }; |
70 | } |
4664d531 |
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 | |
6903978c |
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 | |
4664d531 |
99 | use MooseX::Types::DateTime; |
100 | |
101 | has time_zone => ( |
6903978c |
102 | isa => 'DateTime::TimeZone', |
4664d531 |
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 | |
ec3f7a50 |
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 | |
6903978c |
135 | =item L<Duration> |
ec3f7a50 |
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 |
4664d531 |
155 | |
156 | =item L<DateTime::Locale> |
157 | |
ec3f7a50 |
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 |
4664d531 |
165 | L<DateTime::Locale/load>. |
166 | |
ec3f7a50 |
167 | =item from L<Locale::Maktext> |
168 | |
169 | The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>. |
170 | |
4664d531 |
171 | =item L<DateTime::TimeZone> |
172 | |
ec3f7a50 |
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. |
4664d531 |
183 | |
ec3f7a50 |
184 | =back |
4664d531 |
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 |