version bump
[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.04";
9
10 use DateTime ();
11 use DateTime::Locale ();
12 use DateTime::TimeZone ();
13
14 use MooseX::Types::Moose qw/Num HashRef Str/;
15
16 use namespace::clean;
17
18 use MooseX::Types -declare => [qw( DateTime Duration TimeZone Locale Now )];
19
20 class_type "DateTime";
21 class_type "DateTime::Duration";
22 class_type "DateTime::TimeZone";
23 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
24
25 subtype DateTime, as 'DateTime';
26 subtype Duration, as 'DateTime::Duration';
27 subtype TimeZone, as 'DateTime::TimeZone';
28 subtype Locale,   as 'DateTime::Locale';
29
30 subtype( Now,
31     as Str,
32     where { $_ eq 'now' },
33     Moose::Util::TypeConstraints::optimize_as {
34         no warnings 'uninitialized';
35         !ref($_[0]) and $_[0] eq 'now';
36     },
37 );
38
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
59 for my $type ( "DateTime", DateTime ) {
60     coerce $type => @{ $coercions{DateTime} };
61 }
62
63 for my $type ( "DateTime::Duration", Duration ) {
64     coerce $type => @{ $coercions{"DateTime::Duration"} };
65 }
66
67 for my $type ( "DateTime::TimeZone", TimeZone ) {
68         coerce $type => @{ $coercions{"DateTime::TimeZone"} };
69 }
70
71 for my $type ( "DateTime::Locale", Locale ) {
72         coerce $type => @{ $coercions{"DateTime::Locale"} };
73 }
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
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
102         use MooseX::Types::DateTime;
103
104     has time_zone => (
105         isa => 'DateTime::TimeZone',
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
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
138 =item L<Duration>
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
158
159 =item L<DateTime::Locale>
160
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
168 L<DateTime::Locale/load>.
169
170 =item from L<Locale::Maktext>
171
172 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
173
174 =item L<DateTime::TimeZone>
175
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.
186
187 =back
188
189 =head1 SEE ALSO
190
191 L<MooseX::Types::DateTimeX>
192
193 L<DateTime>, L<DateTimeX::Easy>
194
195 =head1 VERSION CONTROL
196
197 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
198 #moose for commit bits.
199
200 =head1 AUTHOR
201
202 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
203
204 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
205
206 =head1 COPYRIGHT
207
208         Copyright (c) 2008 Yuval Kogman. All rights reserved
209         This program is free software; you can redistribute
210         it and/or modify it under the same terms as Perl itself.
211
212 =cut