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