3 package MooseX::Types::DateTime;
11 use DateTime::Locale ();
12 use DateTime::TimeZone ();
14 use MooseX::Types::Moose qw/Num HashRef Str/;
16 -declare => [qw( DateTime Duration TimeZone )];
18 class_type "DateTime";
19 class_type "DateTime::Duration";
20 class_type "DateTime::TimeZone";
21 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
23 subtype DateTime, as 'DateTime';
24 subtype Duration, as 'DateTime::Duration';
25 subtype TimeZone, as 'DateTime::TimeZone';
28 for my $type ( "DateTime", DateTime ) {
31 via { 'DateTime'->from_epoch( epoch => $_ ) },
33 via { 'DateTime'->new( %$_ ) },
34 Moose::Meta::TypeConstraint->new(
36 parent => find_type_constraint("Str"),
37 constraint => sub { $_ eq 'now' },
38 optimise_as => sub { no warnings 'uninitialized'; !ref($_[0]) and $_[0] eq 'now' },
40 via { 'DateTime'->now },
44 for my $type ( "DateTime::Duration", Duration ) {
47 via { DateTime::Duration->new( seconds => $_ ) },
49 via { DateTime::Duration->new( %$_ ) },
53 for my $type ( "DateTime::TimeZone", TimeZone ) {
56 via { DateTime::TimeZone->new( name => $_ ) },
61 coerce "DateTime::Locale" => (
62 from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
63 via { DateTime::Locale->load($_->language_tag) },
65 via { DateTime::Locale->load($_) },
76 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
83 use MooseX::Types::DateTime qw(TimeZone);
91 Class->new( time_zone => "Africa/Timbuktu" );
95 use MooseX::Types::DateTime;
98 isa => 'DateTime::TimeZone',
103 Class->new( time_zone => "Africa/Timbuktu" );
107 This module packages several L<Moose::Util::TypeConstraints> with coercions,
108 designed to work with the L<DateTime> suite of objects.
116 A class type for L<DateTime>.
122 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
123 percision, see L<DateTime> for details.
125 =item from C<HashRef>
127 Calls L<DateTime/new> with the hash entries as arguments.
133 A class type for L<DateTime::Duration>
139 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
141 Note that due to leap seconds, DST changes etc this may not do what you expect.
142 For instance passing in C<86400> is not always equivalent to one day, although
143 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
146 =item from C<HashRef>
148 Calls L<DateTime::Duration/new> with the hash entries as arguments.
152 =item L<DateTime::Locale>
154 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
160 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
161 L<DateTime::Locale/load>.
163 =item from L<Locale::Maktext>
165 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
167 =item L<DateTime::TimeZone>
169 A class type for L<DateTime::TimeZone>.
175 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
176 details on the allowed values.
178 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
182 =head1 VERSION CONTROL
184 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
185 #moose for commit bits.
189 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
193 Copyright (c) 2008 Yuval Kogman. All rights reserved
194 This program is free software; you can redistribute
195 it and/or modify it under the same terms as Perl itself.