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