avoid indexing headaches with DateTimeX
[gitmo/MooseX-Types-DateTime.git] / lib / MooseX / Types / DateTimeX.pm
CommitLineData
a3f4ab71 1package MooseX::Types::DateTimeX;
2
3use strict;
4use warnings;
5
a3f4ab71 6use DateTime;
6903978c 7use DateTime::Duration;
a3f4ab71 8use DateTimeX::Easy;
6903978c 9use Time::Duration::Parse ();
a3f4ab71 10use MooseX::Types::DateTime;
11use MooseX::Types::Moose qw/Num HashRef Str/;
6903978c 12use MooseX::Types -declare => [qw( DateTime Duration)];
13
a3f4ab71 14
15=head1 NAME
16
17MooseX::Types::DateTimeX - Extensions to L<MooseX::Types::DateTime>
18
19=head1 SYNOPSIS
20
21 package MyApp::MyClass;
22
23 use MooseX::Types::DateTimeX qw( DateTime );
24
25 has created => (
26 isa => DateTime,
27 is => "rw",
28 coerce => 1,
29 );
30
31 my $instance = MyApp::MyClass->new(created=>'January 1, 1980');
32 print $instance->created->year; # is 1980
33
34 ## Coercions from the base type continue to work as normal.
35 my $instance = MyApp::MyClass->new(created=>{year=>2000,month=>1,day=>10});
36
37Please see the test case for more example usage.
38
39=head1 DESCRIPTION
40
41This module builds on L<MooseX::Types::DateTime> to add additional custom
42types and coercions. Since it builds on an existing type, all coercions and
43constraints are inherited.
44
45=head1 SUBTYPES
46
47This module defines the following additional subtypes.
48
49=head2 DateTime
50
51Subtype of 'DateTime'. Adds an additional coercion from strings.
52
53Uses L<DateTimeX::Easy> to try and convert strings, like "yesterday" into a
54valid L<DateTime> object. Please note that due to ambiguity with how different
55systems might localize their timezone, string parsing may not always return
56the most expected value. IN general we try to localize to UTC whenever
57possible. Feedback welcomed!
58
59=cut
60
61subtype DateTime,
62 as 'DateTime'; ## From MooseX::Types::DateTime
63
64coerce DateTime,
65e64b35 65 @{find_type_constraint('DateTime')->coercion->type_coercion_map},
a3f4ab71 66 from Str,
67 via { DateTimeX::Easy->new($_, default_time_zone=>'UTC') };
68
69
6903978c 70=head2 Duration
71
72Subtype of 'DateTime::Duration' that coerces from a string. We use the module
73L<Time::Duration::Parse> to attempt this.
74
75=cut
76
77subtype Duration,
78 as 'DateTime::Duration'; ## From MooseX::Types::Duration
79
80coerce Duration,
65e64b35 81 @{find_type_constraint('DateTime::Duration')->coercion->type_coercion_map},
6903978c 82 from Str,
83 via {
84 DateTime::Duration->new(
85 seconds => Time::Duration::Parse::parse_duration($_)
86 )};
87
88
a3f4ab71 89=head1 AUTHOR
90
91John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
92
6903978c 93=head1 LICENSE
a3f4ab71 94
6903978c 95 Copyright (c) 2008 John Napiorkowski.
96
a3f4ab71 97 This program is free software; you can redistribute
98 it and/or modify it under the same terms as Perl itself.
99
100=cut
101
1021;