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