1 package MooseX::Types::DateTime;
8 use DateTime 0.4302 ();
9 use DateTime::Duration 0.4302 ();
10 use DateTime::Locale 0.4001 ();
11 use DateTime::TimeZone 0.95 ();
13 use MooseX::Types::Moose 0.30 qw/Num HashRef Str/;
15 use namespace::clean 0.08;
17 use MooseX::Types 0.30 -declare => [qw( DateTime Duration TimeZone Locale Now )];
19 class_type "DateTime";
20 class_type "DateTime::Duration";
21 class_type "DateTime::TimeZone";
22 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
24 subtype DateTime, as 'DateTime';
25 subtype Duration, as 'DateTime::Duration';
26 subtype TimeZone, as 'DateTime::TimeZone';
27 subtype Locale, as 'DateTime::Locale';
31 where { $_ eq 'now' },
32 ($Moose::VERSION >= 2.0100
33 ? Moose::Util::TypeConstraints::inline_as {
34 'no warnings "uninitialized";'.
35 '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"';
37 : Moose::Util::TypeConstraints::optimize_as {
38 no warnings 'uninitialized';
39 !ref($_[0]) and $_[0] eq 'now';
46 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
47 from HashRef, via { 'DateTime'->new( %$_ ) },
48 from Now, via { 'DateTime'->now },
50 "DateTime::Duration" => [
51 from Num, via { DateTime::Duration->new( seconds => $_ ) },
52 from HashRef, via { DateTime::Duration->new( %$_ ) },
54 "DateTime::TimeZone" => [
55 from Str, via { DateTime::TimeZone->new( name => $_ ) },
57 "DateTime::Locale" => [
58 from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
59 via { DateTime::Locale->load($_->language_tag) },
60 from Str, via { DateTime::Locale->load($_) },
64 for my $type ( "DateTime", DateTime ) {
65 coerce $type => @{ $coercions{DateTime} };
68 for my $type ( "DateTime::Duration", Duration ) {
69 coerce $type => @{ $coercions{"DateTime::Duration"} };
72 for my $type ( "DateTime::TimeZone", TimeZone ) {
73 coerce $type => @{ $coercions{"DateTime::TimeZone"} };
76 for my $type ( "DateTime::Locale", Locale ) {
77 coerce $type => @{ $coercions{"DateTime::Locale"} };
88 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
95 use MooseX::Types::DateTime qw(TimeZone);
103 Class->new( time_zone => "Africa/Timbuktu" );
105 =for stopwords Namespaced
109 use MooseX::Types::DateTime;
112 isa => 'DateTime::TimeZone',
117 Class->new( time_zone => "Africa/Timbuktu" );
121 This module packages several L<Moose::Util::TypeConstraints> with coercions,
122 designed to work with the L<DateTime> suite of objects.
130 A class type for L<DateTime>.
136 Uses L<DateTime/from_epoch>. Floating values will be used for sub-second
137 precision, see L<DateTime> for details.
139 =item from C<HashRef>
141 Calls L<DateTime/new> with the hash entries as arguments.
147 A class type for L<DateTime::Duration>
153 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
155 Note that due to leap seconds, DST changes etc this may not do what you expect.
156 For instance passing in C<86400> is not always equivalent to one day, although
157 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
160 =item from C<HashRef>
162 Calls L<DateTime::Duration/new> with the hash entries as arguments.
166 =item L<DateTime::Locale>
168 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
174 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
175 L<DateTime::Locale/load>.
177 =item from L<Locale::Maktext>
179 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
181 =item L<DateTime::TimeZone>
183 A class type for L<DateTime::TimeZone>.
189 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
190 details on the allowed values.
192 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
202 L<MooseX::Types::DateTime::MoreCoercions>
204 L<DateTime>, L<DateTimeX::Easy>
208 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
210 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
214 Copyright (c) 2008 Yuval Kogman. All rights reserved
215 This program is free software; you can redistribute
216 it and/or modify it under the same terms as Perl itself.