Commit | Line | Data |
a3f4ab71 |
1 | package MooseX::Types::DateTimeX; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a3f4ab71 |
6 | use DateTime; |
6903978c |
7 | use DateTime::Duration; |
a3f4ab71 |
8 | use DateTimeX::Easy; |
d3c47673 |
9 | use Time::Duration::Parse qw(parse_duration); |
cf90d88a |
10 | use MooseX::Types::DateTime (); |
a3f4ab71 |
11 | use MooseX::Types::Moose qw/Num HashRef Str/; |
d3c47673 |
12 | |
13 | use namespace::clean; |
14 | |
6903978c |
15 | use MooseX::Types -declare => [qw( DateTime Duration)]; |
16 | |
a3f4ab71 |
17 | =head1 NAME |
18 | |
19 | MooseX::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 | |
39 | Please see the test case for more example usage. |
40 | |
41 | =head1 DESCRIPTION |
42 | |
43 | This module builds on L<MooseX::Types::DateTime> to add additional custom |
44 | types and coercions. Since it builds on an existing type, all coercions and |
45 | constraints are inherited. |
46 | |
47 | =head1 SUBTYPES |
48 | |
49 | This module defines the following additional subtypes. |
50 | |
51 | =head2 DateTime |
52 | |
53 | Subtype of 'DateTime'. Adds an additional coercion from strings. |
54 | |
55 | Uses L<DateTimeX::Easy> to try and convert strings, like "yesterday" into a |
56 | valid L<DateTime> object. Please note that due to ambiguity with how different |
57 | systems might localize their timezone, string parsing may not always return |
58 | the most expected value. IN general we try to localize to UTC whenever |
59 | possible. Feedback welcomed! |
60 | |
61 | =cut |
62 | |
0ad29cc1 |
63 | subtype DateTime, as MooseX::Types::DateTime::DateTime; |
a3f4ab71 |
64 | |
bb952b88 |
65 | coerce( 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 | |
73 | Subtype of 'DateTime::Duration' that coerces from a string. We use the module |
74 | L<Time::Duration::Parse> to attempt this. |
75 | |
76 | =cut |
77 | |
0ad29cc1 |
78 | subtype Duration, as MooseX::Types::DateTime::Duration; |
6903978c |
79 | |
bb952b88 |
80 | coerce( 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 | |
91 | John 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 | |
102 | 1; |