3 package MooseX::Types::DateTime;
11 use DateTime 0.4302 ();
12 use DateTime::Duration 0.4302 ();
13 use DateTime::Locale 0.4001 ();
14 use DateTime::TimeZone 0.95 ();
16 use MooseX::Types::Moose 0.30 qw/Num HashRef Str/;
18 use namespace::clean 0.08;
20 use MooseX::Types 0.30 -declare => [qw( DateTime Duration TimeZone Locale Now )];
22 class_type "DateTime";
23 class_type "DateTime::Duration";
24 class_type "DateTime::TimeZone";
25 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
27 subtype DateTime, as 'DateTime';
28 subtype Duration, as 'DateTime::Duration';
29 subtype TimeZone, as 'DateTime::TimeZone';
30 subtype Locale, as 'DateTime::Locale';
34 where { $_ eq 'now' },
35 ($Moose::VERSION >= 2.0100
36 ? Moose::Util::TypeConstraints::inline_as {
37 'no warnings "uninitialized";'.
38 '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"';
40 : Moose::Util::TypeConstraints::optimize_as {
41 no warnings 'uninitialized';
42 !ref($_[0]) and $_[0] eq 'now';
49 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
50 from HashRef, via { 'DateTime'->new( %$_ ) },
51 from Now, via { 'DateTime'->now },
53 "DateTime::Duration" => [
54 from Num, via { DateTime::Duration->new( seconds => $_ ) },
55 from HashRef, via { DateTime::Duration->new( %$_ ) },
57 "DateTime::TimeZone" => [
58 from Str, via { DateTime::TimeZone->new( name => $_ ) },
60 "DateTime::Locale" => [
61 from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
62 via { DateTime::Locale->load($_->language_tag) },
63 from Str, via { DateTime::Locale->load($_) },
67 for my $type ( "DateTime", DateTime ) {
68 coerce $type => @{ $coercions{DateTime} };
71 for my $type ( "DateTime::Duration", Duration ) {
72 coerce $type => @{ $coercions{"DateTime::Duration"} };
75 for my $type ( "DateTime::TimeZone", TimeZone ) {
76 coerce $type => @{ $coercions{"DateTime::TimeZone"} };
79 for my $type ( "DateTime::Locale", Locale ) {
80 coerce $type => @{ $coercions{"DateTime::Locale"} };
91 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
98 use MooseX::Types::DateTime qw(TimeZone);
106 Class->new( time_zone => "Africa/Timbuktu" );
110 use MooseX::Types::DateTime;
113 isa => 'DateTime::TimeZone',
118 Class->new( time_zone => "Africa/Timbuktu" );
122 This module packages several L<Moose::Util::TypeConstraints> with coercions,
123 designed to work with the L<DateTime> suite of objects.
131 A class type for L<DateTime>.
137 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
138 percision, see L<DateTime> for details.
140 =item from C<HashRef>
142 Calls L<DateTime/new> with the hash entries as arguments.
148 A class type for L<DateTime::Duration>
154 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
156 Note that due to leap seconds, DST changes etc this may not do what you expect.
157 For instance passing in C<86400> is not always equivalent to one day, although
158 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
161 =item from C<HashRef>
163 Calls L<DateTime::Duration/new> with the hash entries as arguments.
167 =item L<DateTime::Locale>
169 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
175 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
176 L<DateTime::Locale/load>.
178 =item from L<Locale::Maktext>
180 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
182 =item L<DateTime::TimeZone>
184 A class type for L<DateTime::TimeZone>.
190 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
191 details on the allowed values.
193 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
203 L<MooseX::Types::DateTime::MoreCoercions>
205 L<DateTime>, L<DateTimeX::Easy>
207 =head1 VERSION CONTROL
209 This module is maintained using git. You can get the latest version from
210 L<git://git.moose.perl.org/MooseX-Types-DateTime.git>.
214 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
216 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
220 Copyright (c) 2008 Yuval Kogman. All rights reserved
221 This program is free software; you can redistribute
222 it and/or modify it under the same terms as Perl itself.