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)];
16 MooseX::Types::DateTimeX - Extensions to L<MooseX::Types::DateTime>
20 package MyApp::MyClass;
22 use MooseX::Types::DateTimeX qw( DateTime );
30 my $instance = MyApp::MyClass->new(created=>'January 1, 1980');
31 print $instance->created->year; # is 1980
33 ## Coercions from the base type continue to work as normal.
34 my $instance = MyApp::MyClass->new(created=>{year=>2000,month=>1,day=>10});
36 Please see the test case for more example usage.
40 This module builds on L<MooseX::Types::DateTime> to add additional custom
41 types and coercions. Since it builds on an existing type, all coercions and
42 constraints are inherited.
46 This module defines the following additional subtypes.
50 Subtype of 'DateTime'. Adds an additional coercion from strings.
52 Uses L<DateTimeX::Easy> to try and convert strings, like "yesterday" into a
53 valid L<DateTime> object. Please note that due to ambiguity with how different
54 systems might localize their timezone, string parsing may not always return
55 the most expected value. IN general we try to localize to UTC whenever
56 possible. Feedback welcomed!
60 subtype DateTime, as MooseX::Types::DateTime::DateTime;
63 @{find_type_constraint('DateTime')->coercion->type_coercion_map},
65 via { DateTimeX::Easy->new($_, default_time_zone=>'UTC') };
70 Subtype of 'DateTime::Duration' that coerces from a string. We use the module
71 L<Time::Duration::Parse> to attempt this.
75 subtype Duration, as MooseX::Types::DateTime::Duration;
78 @{find_type_constraint('DateTime::Duration')->coercion->type_coercion_map},
81 DateTime::Duration->new(
82 seconds => Time::Duration::Parse::parse_duration($_)
88 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
92 Copyright (c) 2008 John Napiorkowski.
94 This program is free software; you can redistribute
95 it and/or modify it under the same terms as Perl itself.