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