3 package MooseX::Types::DateTime;
11 use DateTime::Locale ();
12 use DateTime::TimeZone ();
14 use MooseX::Types::Moose qw/Num HashRef Str/;
18 use MooseX::Types -declare => [qw( DateTime Duration TimeZone Locale Now )];
20 class_type "DateTime";
21 class_type "DateTime::Duration";
22 class_type "DateTime::TimeZone";
23 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
25 subtype DateTime, as 'DateTime';
26 subtype Duration, as 'DateTime::Duration';
27 subtype TimeZone, as 'DateTime::TimeZone';
28 subtype Locale, as 'DateTime::Locale';
32 where { $_ eq 'now' },
33 Moose::Util::TypeConstraints::optimize_as {
34 no warnings 'uninitialized';
35 !ref($_[0]) and $_[0] eq 'now';
41 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
42 from HashRef, via { 'DateTime'->new( %$_ ) },
43 from Now, via { 'DateTime'->now },
45 "DateTime::Duration" => [
46 from Num, via { DateTime::Duration->new( seconds => $_ ) },
47 from HashRef, via { DateTime::Duration->new( %$_ ) },
49 "DateTime::TimeZone" => [
50 from Str, via { DateTime::TimeZone->new( name => $_ ) },
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($_) },
59 for my $type ( "DateTime", DateTime ) {
60 coerce $type => @{ $coercions{DateTime} };
63 for my $type ( "DateTime::Duration", Duration ) {
64 coerce $type => @{ $coercions{"DateTime::Duration"} };
67 for my $type ( "DateTime::TimeZone", TimeZone ) {
68 coerce $type => @{ $coercions{"DateTime::TimeZone"} };
71 for my $type ( "DateTime::Locale", Locale ) {
72 coerce $type => @{ $coercions{"DateTime::Locale"} };
83 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
90 use MooseX::Types::DateTime qw(TimeZone);
98 Class->new( time_zone => "Africa/Timbuktu" );
102 use MooseX::Types::DateTime;
105 isa => 'DateTime::TimeZone',
110 Class->new( time_zone => "Africa/Timbuktu" );
114 This module packages several L<Moose::Util::TypeConstraints> with coercions,
115 designed to work with the L<DateTime> suite of objects.
123 A class type for L<DateTime>.
129 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
130 percision, see L<DateTime> for details.
132 =item from C<HashRef>
134 Calls L<DateTime/new> with the hash entries as arguments.
140 A class type for L<DateTime::Duration>
146 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
148 Note that due to leap seconds, DST changes etc this may not do what you expect.
149 For instance passing in C<86400> is not always equivalent to one day, although
150 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
153 =item from C<HashRef>
155 Calls L<DateTime::Duration/new> with the hash entries as arguments.
159 =item L<DateTime::Locale>
161 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
167 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
168 L<DateTime::Locale/load>.
170 =item from L<Locale::Maktext>
172 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
174 =item L<DateTime::TimeZone>
176 A class type for L<DateTime::TimeZone>.
182 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
183 details on the allowed values.
185 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
191 L<MooseX::Types::DateTimeX>
193 L<DateTime>, L<DateTimeX::Easy>
195 =head1 VERSION CONTROL
197 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
198 #moose for commit bits.
202 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
204 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
208 Copyright (c) 2008 Yuval Kogman. All rights reserved
209 This program is free software; you can redistribute
210 it and/or modify it under the same terms as Perl itself.