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