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::VERSION >= 2.0100
34 ? Moose::Util::TypeConstraints::inline_as {
35 'no warnings "uninitialized";'.
36 '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"';
38 : Moose::Util::TypeConstraints::optimize_as {
39 no warnings 'uninitialized';
40 !ref($_[0]) and $_[0] eq 'now';
47 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
48 from HashRef, via { 'DateTime'->new( %$_ ) },
49 from Now, via { 'DateTime'->now },
51 "DateTime::Duration" => [
52 from Num, via { DateTime::Duration->new( seconds => $_ ) },
53 from HashRef, via { DateTime::Duration->new( %$_ ) },
55 "DateTime::TimeZone" => [
56 from Str, via { DateTime::TimeZone->new( name => $_ ) },
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($_) },
65 for my $type ( "DateTime", DateTime ) {
66 coerce $type => @{ $coercions{DateTime} };
69 for my $type ( "DateTime::Duration", Duration ) {
70 coerce $type => @{ $coercions{"DateTime::Duration"} };
73 for my $type ( "DateTime::TimeZone", TimeZone ) {
74 coerce $type => @{ $coercions{"DateTime::TimeZone"} };
77 for my $type ( "DateTime::Locale", Locale ) {
78 coerce $type => @{ $coercions{"DateTime::Locale"} };
89 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
96 use MooseX::Types::DateTime qw(TimeZone);
104 Class->new( time_zone => "Africa/Timbuktu" );
108 use MooseX::Types::DateTime;
111 isa => 'DateTime::TimeZone',
116 Class->new( time_zone => "Africa/Timbuktu" );
120 This module packages several L<Moose::Util::TypeConstraints> with coercions,
121 designed to work with the L<DateTime> suite of objects.
129 A class type for L<DateTime>.
135 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
136 percision, see L<DateTime> for details.
138 =item from C<HashRef>
140 Calls L<DateTime/new> with the hash entries as arguments.
146 A class type for L<DateTime::Duration>
152 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
154 Note that due to leap seconds, DST changes etc this may not do what you expect.
155 For instance passing in C<86400> is not always equivalent to one day, although
156 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
159 =item from C<HashRef>
161 Calls L<DateTime::Duration/new> with the hash entries as arguments.
165 =item L<DateTime::Locale>
167 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
173 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
174 L<DateTime::Locale/load>.
176 =item from L<Locale::Maktext>
178 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
180 =item L<DateTime::TimeZone>
182 A class type for L<DateTime::TimeZone>.
188 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
189 details on the allowed values.
191 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
201 L<MooseX::Types::DateTimeX>
203 L<DateTime>, L<DateTimeX::Easy>
205 =head1 VERSION CONTROL
207 This module is maintained using git. You can get the latest version from
208 L<git://github.com/nothingmuch/moosex-types-datetime.git>.
212 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
214 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
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.