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