3 package MooseX::Types::DateTime;
11 use DateTime::Locale ();
12 use DateTime::TimeZone ();
14 use MooseX::Types::Moose qw/Num HashRef Str/;
15 use MooseX::Types -declare => [qw( DateTime Duration TimeZone Locale Now )];
17 class_type "DateTime";
18 class_type "DateTime::Duration";
19 class_type "DateTime::TimeZone";
20 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
22 subtype DateTime, as 'DateTime';
23 subtype Duration, as 'DateTime::Duration';
24 subtype TimeZone, as 'DateTime::TimeZone';
25 subtype Locale, as 'DateTime::Locale';
29 where { $_ eq 'now' },
30 Moose::Util::TypeConstraints::optimize_as {
31 no warnings 'uninitialized';
32 !ref($_[0]) and $_[0] eq 'now';
38 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
39 from HashRef, via { 'DateTime'->new( %$_ ) },
40 from Now, via { 'DateTime'->now },
42 "DateTime::Duration" => [
43 from Num, via { DateTime::Duration->new( seconds => $_ ) },
44 from HashRef, via { DateTime::Duration->new( %$_ ) },
46 "DateTime::TimeZone" => [
47 from Str, via { DateTime::TimeZone->new( name => $_ ) },
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($_) },
56 for my $type ( "DateTime", DateTime ) {
57 coerce $type => @{ $coercions{DateTime} };
60 for my $type ( "DateTime::Duration", Duration ) {
61 coerce $type => @{ $coercions{"DateTime::Duration"} };
64 for my $type ( "DateTime::TimeZone", TimeZone ) {
65 coerce $type => @{ $coercions{"DateTime::TimeZone"} };
68 for my $type ( "DateTime::Locale", Locale ) {
69 coerce $type => @{ $coercions{"DateTime::Locale"} };
80 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
87 use MooseX::Types::DateTime qw(TimeZone);
95 Class->new( time_zone => "Africa/Timbuktu" );
99 use MooseX::Types::DateTime;
102 isa => 'DateTime::TimeZone',
107 Class->new( time_zone => "Africa/Timbuktu" );
111 This module packages several L<Moose::Util::TypeConstraints> with coercions,
112 designed to work with the L<DateTime> suite of objects.
120 A class type for L<DateTime>.
126 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
127 percision, see L<DateTime> for details.
129 =item from C<HashRef>
131 Calls L<DateTime/new> with the hash entries as arguments.
137 A class type for L<DateTime::Duration>
143 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
145 Note that due to leap seconds, DST changes etc this may not do what you expect.
146 For instance passing in C<86400> is not always equivalent to one day, although
147 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
150 =item from C<HashRef>
152 Calls L<DateTime::Duration/new> with the hash entries as arguments.
156 =item L<DateTime::Locale>
158 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
164 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
165 L<DateTime::Locale/load>.
167 =item from L<Locale::Maktext>
169 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
171 =item L<DateTime::TimeZone>
173 A class type for L<DateTime::TimeZone>.
179 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
180 details on the allowed values.
182 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
186 =head1 VERSION CONTROL
188 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
189 #moose for commit bits.
193 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
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.