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';
36 for my $type ( "DateTime", DateTime ) {
39 via { 'DateTime'->from_epoch( epoch => $_ ) },
41 via { 'DateTime'->new( %$_ ) },
42 Moose::Meta::TypeConstraint->new(
44 parent => find_type_constraint("Str"),
45 constraint => sub { $_ eq 'now' },
46 optimise_as => sub { no warnings 'uninitialized'; !ref($_[0]) and $_[0] eq 'now' },
48 via { 'DateTime'->now },
52 for my $type ( "DateTime::Duration", Duration ) {
55 via { DateTime::Duration->new( seconds => $_ ) },
57 via { DateTime::Duration->new( %$_ ) },
61 for my $type ( "DateTime::TimeZone", TimeZone ) {
64 via { DateTime::TimeZone->new( name => $_ ) },
69 coerce "DateTime::Locale" => (
70 from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
71 via { DateTime::Locale->load($_->language_tag) },
73 via { DateTime::Locale->load($_) },
84 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
91 use MooseX::Types::DateTime qw(TimeZone);
99 Class->new( time_zone => "Africa/Timbuktu" );
103 use MooseX::Types::DateTime;
106 isa => 'DateTime::TimeZone',
111 Class->new( time_zone => "Africa/Timbuktu" );
115 This module packages several L<Moose::Util::TypeConstraints> with coercions,
116 designed to work with the L<DateTime> suite of objects.
124 A class type for L<DateTime>.
130 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
131 percision, see L<DateTime> for details.
133 =item from C<HashRef>
135 Calls L<DateTime/new> with the hash entries as arguments.
141 A class type for L<DateTime::Duration>
147 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
149 Note that due to leap seconds, DST changes etc this may not do what you expect.
150 For instance passing in C<86400> is not always equivalent to one day, although
151 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
154 =item from C<HashRef>
156 Calls L<DateTime::Duration/new> with the hash entries as arguments.
160 =item L<DateTime::Locale>
162 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
168 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
169 L<DateTime::Locale/load>.
171 =item from L<Locale::Maktext>
173 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
175 =item L<DateTime::TimeZone>
177 A class type for L<DateTime::TimeZone>.
183 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
184 details on the allowed values.
186 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
190 =head1 VERSION CONTROL
192 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
193 #moose for commit bits.
197 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
201 Copyright (c) 2008 Yuval Kogman. All rights reserved
202 This program is free software; you can redistribute
203 it and/or modify it under the same terms as Perl itself.