Version 0.07
[gitmo/MooseX-Types-DateTime-MoreCoercions.git] / lib / MooseX / Types / DateTime / MoreCoercions.pm
CommitLineData
00d7c0c9 1package MooseX::Types::DateTime::MoreCoercions;
de827165 2use strict;
3use warnings;
4
5use DateTime;
6use DateTime::Duration;
7use DateTimeX::Easy;
8use Time::Duration::Parse qw(parse_duration);
e18dcdf2 9use MooseX::Types::DateTime ();
de827165 10use MooseX::Types::Moose qw/Num HashRef Str/;
11
12use namespace::clean;
13
fdbc2421 14our $VERSION = '0.07';
de827165 15
16use MooseX::Types -declare => [qw( DateTime Duration)];
17
e18dcdf2 18subtype DateTime, as MooseX::Types::DateTime::DateTime;
de827165 19coerce( DateTime,
e18dcdf2 20 @{ $MooseX::Types::DateTime::coercions{DateTime} },
de827165 21 from Str, via { DateTimeX::Easy->new($_) },
22);
23
e18dcdf2 24subtype Duration, as MooseX::Types::DateTime::Duration;
de827165 25coerce( Duration,
e18dcdf2 26 @{ $MooseX::Types::DateTime::coercions{"DateTime::Duration"} },
de827165 27 from Str, via {
28 DateTime::Duration->new(
29 seconds => parse_duration($_)
30 );
31 },
32);
33
341;
35
36__END__
37
38=head1 NAME
39
00d7c0c9 40MooseX::Types::DateTime::MoreCoercions - Extensions to L<MooseX::Types::DateTime>
de827165 41
42=head1 SYNOPSIS
43
44 package MyApp::MyClass;
45
00d7c0c9 46 use MooseX::Types::DateTime::MoreCoercions qw( DateTime );
de827165 47
48 has created => (
49 isa => DateTime,
50 is => "rw",
51 coerce => 1,
52 );
53
54 my $instance = MyApp::MyClass->new(created=>'January 1, 1980');
55 print $instance->created->year; # is 1980
56
57 ## Coercions from the base type continue to work as normal.
58 my $instance = MyApp::MyClass->new(created=>{year=>2000,month=>1,day=>10});
59
60Please see the test case for more example usage.
61
62=head1 DESCRIPTION
63
64This 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.
65
de827165 66=head1 SUBTYPES
67
68This module defines the following additional subtypes.
69
70=head2 DateTime
71
00d7c0c9 72Subtype of L<MooseX::Types::DateTime/DateTime>. Adds an additional coercion from strings.
de827165 73
74Uses 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!
75
76=head2 Duration
77
00d7c0c9 78Subtype of L<MooseX::Types::DateTime/Duration> that coerces from a string. We use the module L<Time::Duration::Parse> to attempt this.
de827165 79
80=head1 CAVEATS
81
82Firstly, 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.
83
84=head1 SEE ALSO
85
86=over 4
87
e18dcdf2 88=item * L<MooseX::Types::DateTime> Replacement for this module -- coercions with less voodoo
de827165 89
90=item * L<DateTimeX::Easy> Backend of this module
91
00d7c0c9 92=item * L<Time::Duration::Parse> Duration parsing backend for this module
93
de827165 94=back
95
96=head1 AUTHOR
97
98John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
99
100Broken into a seperate package from L<MooseX::Types::DateTime> by Evan Carroll.
101
00d7c0c9 102Forked from L<MooseX::Types::DateTimeX> and ported back to use
103L<MooseX::Types::DateTime> by Dagfinn Ilmari MannsE<aring>ker
104E<lt>ilmari@ilmari.orgE<gt>.
105
de827165 106=head1 LICENSE
107
108 Copyright (c) 2008 John Napiorkowski.
109
110 This program is free software; you can redistribute
111 it and/or modify it under the same terms as Perl itself.