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