3 package MooseX::Types::DateTime;
11 use DateTime::Locale ();
12 use DateTime::TimeZone ();
14 use Moose::Util::TypeConstraints;
16 class_type "DateTime";
17 class_type "DateTime::Duration";
18 class_type "DateTime::TimeZone";
19 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
21 coerce "DateTime" => (
23 via { DateTime->from_epoch( epoch => $_ ) },
25 via { DateTime->new( %$_ ) },
26 Moose::Meta::TypeConstraint->new(
28 parent => find_type_constraint("Str"),
29 constraint => sub { $_ eq 'now' },
30 optimise_as => sub { no warnings 'uninitialized'; !ref($_[0]) and $_[0] eq 'now' },
32 via { DateTime->now },
35 coerce "DateTime::Duration" => (
37 via { DateTime::Duration->new( seconds => $_ ) },
39 via { DateTime::Duration->new( %$_ ) },
42 coerce "DateTime::TimeZone" => (
44 via { DateTime::TimeZone->new( name => $_ ) },
47 coerce "DateTime::Locale" => (
48 from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
49 via { DateTime::Locale->load($_->language_tag) },
51 via { DateTime::Locale->load($_) },
62 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
67 use MooseX::Types::DateTime;
70 isa => "DateTime::TimeZone",
75 Class->new( time_zone => "Africa/Timbuktu" );
79 This module packages several L<Moose::Util::TypeConstraints> with coercions,
80 designed to work with the L<DateTime> suite of objects.
88 A class type for L<DateTime>.
94 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
95 percision, see L<DateTime> for details.
99 Calls L<DateTime/new> with the hash entries as arguments.
103 =item L<DateTime::Duration>
105 A class type for L<DateTime::Duration>
111 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
113 Note that due to leap seconds, DST changes etc this may not do what you expect.
114 For instance passing in C<86400> is not always equivalent to one day, although
115 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
118 =item from C<HashRef>
120 Calls L<DateTime::Duration/new> with the hash entries as arguments.
124 =item L<DateTime::Locale>
126 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
132 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
133 L<DateTime::Locale/load>.
135 =item from L<Locale::Maktext>
137 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
139 =item L<DateTime::TimeZone>
141 A class type for L<DateTime::TimeZone>.
147 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
148 details on the allowed values.
150 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
154 =head1 VERSION CONTROL
156 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
157 #moose for commit bits.
161 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
165 Copyright (c) 2008 Yuval Kogman. All rights reserved
166 This program is free software; you can redistribute
167 it and/or modify it under the same terms as Perl itself.