1 package MooseX::Types::DateTimeX;
6 use DateTime::Duration;
8 use Time::Duration::Parse qw(parse_duration);
9 use MooseX::Types::DateTime ();
10 use MooseX::Types::Moose qw/Num HashRef Str/;
14 our $VERSION = '0.06';
16 use MooseX::Types -declare => [qw( DateTime Duration)];
18 subtype DateTime, as MooseX::Types::DateTime::DateTime;
20 @{ $MooseX::Types::DateTime::coercions{DateTime} },
21 from Str, via { DateTimeX::Easy->new($_) },
24 subtype Duration, as MooseX::Types::DateTime::Duration;
26 @{ $MooseX::Types::DateTime::coercions{"DateTime::Duration"} },
28 DateTime::Duration->new(
29 seconds => parse_duration($_)
40 MooseX::Types::DateTimeX - Extensions to L<MooseX::Types::DateTime>
44 package MyApp::MyClass;
46 use MooseX::Types::DateTimeX qw( DateTime );
54 my $instance = MyApp::MyClass->new(created=>'January 1, 1980');
55 print $instance->created->year; # is 1980
57 ## Coercions from the base type continue to work as normal.
58 my $instance = MyApp::MyClass->new(created=>{year=>2000,month=>1,day=>10});
60 Please see the test case for more example usage.
64 This module builds on L<MooseX::Types::DateTime> to add additional custom types and coercions. Since it builds on an existing type, all coercions and constraints are inherited.
66 The package name is left as is for legacy reasons: this module is really a Type with coercions for L<DateTimeX::Easy>. DateTimeX is just a namespace for non-core or less-official L<DateTime> modules.
70 This module defines the following additional subtypes.
74 Subtype of 'DateTime'. Adds an additional coercion from strings.
76 Uses L<DateTimeX::Easy> to try and convert strings, like "yesterday" into a valid L<DateTime> object. Please note that due to ambiguity with how different systems might localize their timezone, string parsing may not always return the most expected value. IN general we try to localize to UTC whenever possible. Feedback welcomed!
80 Subtype of 'DateTime::Duration' that coerces from a string. We use the module L<Time::Duration::Parse> to attempt this.
84 Firstly, this module uses L<DateTimeX::Easy> which is way to more DWIM than any sane person would desire. L<DateTimeX::Easy> works by falling back until something makes sense, this is variable. Furthermore, all the modules that L<DateTimeX::Easy> *can* use aren't required for "proper" function of L<DateTimeX::Easy>. What does this mean? Simple, your mileage may vary in your coercions because L<DateTimeX::Easy> is installation specific.
90 =item * L<MooseX::Types::DateTime> Replacement for this module -- coercions with less voodoo
92 =item * L<DateTimeX::Easy> Backend of this module
98 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
100 Broken into a seperate package from L<MooseX::Types::DateTime> by Evan Carroll.
104 Copyright (c) 2008 John Napiorkowski.
106 This program is free software; you can redistribute
107 it and/or modify it under the same terms as Perl itself.