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