package MooseX::Types::DateTime; # git description: v0.12-2-g35c46dd # ABSTRACT: L related constraints and coercions for Moose use strict; use warnings; our $VERSION = '0.13'; use 5.008003; use Moose 0.41 (); use DateTime 0.4302 (); use DateTime::Duration 0.4302 (); use DateTime::Locale 0.4001 (); use DateTime::TimeZone 0.95 (); use MooseX::Types::Moose 0.30 qw/Num HashRef Object Str/; use namespace::clean 0.19; use MooseX::Types 0.30 -declare => [qw( DateTime Duration TimeZone Locale Now )]; use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean'; class_type "DateTime"; class_type "DateTime::Duration"; class_type "DateTime::TimeZone"; subtype DateTime, as 'DateTime'; subtype Duration, as 'DateTime::Duration'; subtype TimeZone, as 'DateTime::TimeZone'; subtype 'DateTime::Locale', as Object, where { $_->isa('DateTime::Locale::root') || $_->isa('DateTime::Locale::FromData') }; subtype Locale, as 'DateTime::Locale'; subtype( Now, as Str, where { $_ eq 'now' }, ($Moose::VERSION >= 2.0100 ? Moose::Util::TypeConstraints::inline_as { 'no warnings "uninitialized";'. '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"'; } : Moose::Util::TypeConstraints::optimize_as { no warnings 'uninitialized'; !ref($_[0]) and $_[0] eq 'now'; } ), ); our %coercions = ( DateTime => [ from Num, via { 'DateTime'->from_epoch( epoch => $_ ) }, from HashRef, via { 'DateTime'->new( %$_ ) }, from Now, via { 'DateTime'->now }, ], "DateTime::Duration" => [ from Num, via { DateTime::Duration->new( seconds => $_ ) }, from HashRef, via { DateTime::Duration->new( %$_ ) }, ], "DateTime::TimeZone" => [ from Str, via { DateTime::TimeZone->new( name => $_ ) }, ], "DateTime::Locale" => [ from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"), via { DateTime::Locale->load($_->language_tag) }, from Str, via { DateTime::Locale->load($_) }, ], ); for my $type ( "DateTime", DateTime ) { coerce $type => @{ $coercions{DateTime} }; } for my $type ( "DateTime::Duration", Duration ) { coerce $type => @{ $coercions{"DateTime::Duration"} }; } for my $type ( "DateTime::TimeZone", TimeZone ) { coerce $type => @{ $coercions{"DateTime::TimeZone"} }; } for my $type ( "DateTime::Locale", Locale ) { coerce $type => @{ $coercions{"DateTime::Locale"} }; } __PACKAGE__ __END__ =pod =encoding UTF-8 =head1 NAME MooseX::Types::DateTime - L related constraints and coercions for Moose =head1 VERSION version 0.13 =head1 SYNOPSIS Export Example: use MooseX::Types::DateTime qw(TimeZone); has time_zone => ( isa => TimeZone, is => "rw", coerce => 1, ); Class->new( time_zone => "Africa/Timbuktu" ); =head1 DESCRIPTION This module packages several L with coercions, designed to work with the L suite of objects. =for stopwords Namespaced Namespaced Example: use MooseX::Types::DateTime; has time_zone => ( isa => 'DateTime::TimeZone', is => "rw", coerce => 1, ); Class->new( time_zone => "Africa/Timbuktu" ); =head1 CONSTRAINTS =over 4 =item L A class type for L. =over 4 =item from C Uses L. Floating values will be used for sub-second precision, see L for details. =item from C Calls L with the hash entries as arguments. =back =item L A class type for L =over 4 =item from C Uses L and passes the number as the C argument. Note that due to leap seconds, DST changes etc this may not do what you expect. For instance passing in C<86400> is not always equivalent to one day, although there are that many seconds in a day. See L for more details. =item from C Calls L with the hash entries as arguments. =back =item L A class type for L with the name L. =over 4 =item from C The string is treated as a language tag (e.g. C or C) and given to L. =item from L The C attribute will be used with L. =item L A class type for L. =over 4 =item from C Treated as a time zone name or offset. See L for more details on the allowed values. Delegates to L with the string as the C argument. =back =back =back =head1 SEE ALSO L L, L =head1 SUPPORT Bugs may be submitted through L (or L). There is also a mailing list available for users of this distribution, at L. There is also an irc channel available for users of this distribution, at irc://irc.perl.org/#moose. =head1 AUTHOR יובל קוג'מן (Yuval Kogman) =head1 CONTRIBUTORS =for stopwords Karen Etheridge Dagfinn Ilmari Mannsåker Florian Ragwitz John Napiorkowski Shawn M Moore Dave Rolsky =over 4 =item * Karen Etheridge =item * Dagfinn Ilmari Mannsåker =item * Florian Ragwitz =item * John Napiorkowski =item * Shawn M Moore =item * Dave Rolsky =back =head1 COPYRIGHT AND LICENCE This software is copyright (c) 2008 by יובל קוג'מן (Yuval Kogman). This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut