Created MX:T:DateTimeX as extensions to the MX:T:DateTime, created test for new modul...
[gitmo/MooseX-Types-DateTime.git] / lib / MooseX / Types / DateTimeX.pm
CommitLineData
a3f4ab71 1package MooseX::Types::DateTimeX;
2
3use strict;
4use warnings;
5
6our $VERSION = "0.02";
7our $AUTHORITY = 'cpan:JJNAPIORK';
8
9use DateTime;
10use DateTimeX::Easy;
11use MooseX::Types::DateTime;
12use MooseX::Types::Moose qw/Num HashRef Str/;
13use MooseX::Types
14 -declare => [qw( DateTime )];
15
16=head1 NAME
17
18MooseX::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
38Please see the test case for more example usage.
39
40=head1 DESCRIPTION
41
42This module builds on L<MooseX::Types::DateTime> to add additional custom
43types and coercions. Since it builds on an existing type, all coercions and
44constraints are inherited.
45
46=head1 SUBTYPES
47
48This module defines the following additional subtypes.
49
50=head2 DateTime
51
52Subtype of 'DateTime'. Adds an additional coercion from strings.
53
54Uses L<DateTimeX::Easy> to try and convert strings, like "yesterday" into a
55valid L<DateTime> object. Please note that due to ambiguity with how different
56systems might localize their timezone, string parsing may not always return
57the most expected value. IN general we try to localize to UTC whenever
58possible. Feedback welcomed!
59
60=cut
61
62subtype DateTime,
63 as 'DateTime'; ## From MooseX::Types::DateTime
64
65coerce 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
76John 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
861;