1 package MooseX::Types::DateTimeX;
7 use DateTime::Duration;
9 use Time::Duration::Parse ();
10 use MooseX::Types::DateTime;
11 use MooseX::Types::Moose qw/Num HashRef Str/;
12 use MooseX::Types -declare => [qw( DateTime Duration)];
17 MooseX::Types::DateTimeX - Extensions to L<MooseX::Types::DateTime>
21 package MyApp::MyClass;
23 use MooseX::Types::DateTimeX qw( DateTime );
31 my $instance = MyApp::MyClass->new(created=>'January 1, 1980');
32 print $instance->created->year; # is 1980
34 ## Coercions from the base type continue to work as normal.
35 my $instance = MyApp::MyClass->new(created=>{year=>2000,month=>1,day=>10});
37 Please see the test case for more example usage.
41 This module builds on L<MooseX::Types::DateTime> to add additional custom
42 types and coercions. Since it builds on an existing type, all coercions and
43 constraints are inherited.
47 This module defines the following additional subtypes.
51 Subtype of 'DateTime'. Adds an additional coercion from strings.
53 Uses L<DateTimeX::Easy> to try and convert strings, like "yesterday" into a
54 valid L<DateTime> object. Please note that due to ambiguity with how different
55 systems might localize their timezone, string parsing may not always return
56 the most expected value. IN general we try to localize to UTC whenever
57 possible. Feedback welcomed!
62 as 'DateTime'; ## From MooseX::Types::DateTime
65 @{find_type_constraint('DateTime')->coercion->type_coercion_map},
67 via { DateTimeX::Easy->new($_, default_time_zone=>'UTC') };
72 Subtype of 'DateTime::Duration' that coerces from a string. We use the module
73 L<Time::Duration::Parse> to attempt this.
78 as 'DateTime::Duration'; ## From MooseX::Types::Duration
81 @{find_type_constraint('DateTime::Duration')->coercion->type_coercion_map},
84 DateTime::Duration->new(
85 seconds => Time::Duration::Parse::parse_duration($_)
91 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
95 Copyright (c) 2008 John Napiorkowski.
97 This program is free software; you can redistribute
98 it and/or modify it under the same terms as Perl itself.