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