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