Created MX:T:DateTimeX as extensions to the MX:T:DateTime, created test for new modul...
[gitmo/MooseX-Types-DateTime.git] / lib / MooseX / Types / DateTime.pm
CommitLineData
4664d531 1#!/usr/bin/perl
2
3package MooseX::Types::DateTime;
4
5use strict;
6use warnings;
7
8our $VERSION = "0.01";
9
10use DateTime ();
11use DateTime::Locale ();
12use DateTime::TimeZone ();
13
14use Moose::Util::TypeConstraints;
15
16class_type "DateTime";
ec3f7a50 17class_type "DateTime::Duration";
4664d531 18class_type "DateTime::TimeZone";
19class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
20
21coerce "DateTime" => (
ec3f7a50 22 from "Num",
4664d531 23 via { DateTime->from_epoch( epoch => $_ ) },
ec3f7a50 24 from "HashRef",
25 via { DateTime->new( %$_ ) },
26 Moose::Meta::TypeConstraint->new(
27 name => "__ANON__",
28 parent => find_type_constraint("Str"),
29 constraint => sub { $_ eq 'now' },
30 optimise_as => sub { no warnings 'uninitialized'; !ref($_[0]) and $_[0] eq 'now' },
31 ),
32 via { DateTime->now },
33);
34
35coerce "DateTime::Duration" => (
36 from "Num",
37 via { DateTime::Duration->new( seconds => $_ ) },
38 from "HashRef",
39 via { DateTime::Duration->new( %$_ ) },
4664d531 40);
41
42coerce "DateTime::TimeZone" => (
43 from "Str",
44 via { DateTime::TimeZone->new( name => $_ ) },
45);
46
47coerce "DateTime::Locale" => (
48 from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
49 via { DateTime::Locale->load($_->language_tag) },
50 from "Str",
51 via { DateTime::Locale->load($_) },
52);
53
54__PACKAGE__
55
56__END__
57
58=pod
59
60=head1 NAME
61
62MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
63Moose
64
65=head1 SYNOPSIS
66
67 use MooseX::Types::DateTime;
68
69 has time_zone => (
70 isa => "DateTime::TimeZone",
71 is => "rw",
72 coerce => 1,
73 );
74
75 Class->new( time_zone => "Africa/Timbuktu" );
76
77=head1 DESCRIPTION
78
79This module packages several L<Moose::Util::TypeConstraints> with coercions,
80designed to work with the L<DateTime> suite of objects.
81
82=head1 CONSTRAINTS
83
84=over 4
85
86=item L<DateTime>
87
ec3f7a50 88A class type for L<DateTime>.
89
90=over 4
91
92=item from C<Num>
93
94Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
95percision, see L<DateTime> for details.
96
97=item from C<HashRef>
98
99Calls L<DateTime/new> with the hash entries as arguments.
100
101=back
102
103=item L<DateTime::Duration>
104
105A class type for L<DateTime::Duration>
106
107=over 4
108
109=item from C<Num>
110
111Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
112
113Note that due to leap seconds, DST changes etc this may not do what you expect.
114For instance passing in C<86400> is not always equivalent to one day, although
115there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
116for more details.
117
118=item from C<HashRef>
119
120Calls L<DateTime::Duration/new> with the hash entries as arguments.
121
122=back
4664d531 123
124=item L<DateTime::Locale>
125
ec3f7a50 126A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
127
128=over 4
129
130=item from C<Str>
131
132The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
4664d531 133L<DateTime::Locale/load>.
134
ec3f7a50 135=item from L<Locale::Maktext>
136
137The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
138
4664d531 139=item L<DateTime::TimeZone>
140
ec3f7a50 141A class type for L<DateTime::TimeZone>.
142
143=over 4
144
145=item from C<Str>
146
147Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
148details on the allowed values.
149
150Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
4664d531 151
ec3f7a50 152=back
4664d531 153
154=head1 VERSION CONTROL
155
156L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
157#moose for commit bits.
158
159=head1 AUTHOR
160
161Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
162
163=head1 COPYRIGHT
164
165 Copyright (c) 2008 Yuval Kogman. All rights reserved
166 This program is free software; you can redistribute
167 it and/or modify it under the same terms as Perl itself.
168
169=cut