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