Commit | Line | Data |
4664d531 |
1 | #!/usr/bin/perl |
2 | |
3 | package MooseX::Types::DateTime; |
4 | |
5 | use strict; |
6 | use warnings; |
7 | |
8 | our $VERSION = "0.01"; |
9 | |
10 | use DateTime (); |
11 | use DateTime::Locale (); |
12 | use DateTime::TimeZone (); |
13 | |
14 | use Moose::Util::TypeConstraints; |
15 | |
16 | class_type "DateTime"; |
ec3f7a50 |
17 | class_type "DateTime::Duration"; |
4664d531 |
18 | class_type "DateTime::TimeZone"; |
19 | class_type "DateTime::Locale::root" => { name => "DateTime::Locale" }; |
20 | |
21 | coerce "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 | |
35 | coerce "DateTime::Duration" => ( |
36 | from "Num", |
37 | via { DateTime::Duration->new( seconds => $_ ) }, |
38 | from "HashRef", |
39 | via { DateTime::Duration->new( %$_ ) }, |
4664d531 |
40 | ); |
41 | |
42 | coerce "DateTime::TimeZone" => ( |
43 | from "Str", |
44 | via { DateTime::TimeZone->new( name => $_ ) }, |
45 | ); |
46 | |
47 | coerce "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 | |
62 | MooseX::Types::DateTime - L<DateTime> related constraints and coercions for |
63 | Moose |
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 | |
79 | This module packages several L<Moose::Util::TypeConstraints> with coercions, |
80 | designed to work with the L<DateTime> suite of objects. |
81 | |
82 | =head1 CONSTRAINTS |
83 | |
84 | =over 4 |
85 | |
86 | =item L<DateTime> |
87 | |
ec3f7a50 |
88 | A class type for L<DateTime>. |
89 | |
90 | =over 4 |
91 | |
92 | =item from C<Num> |
93 | |
94 | Uses L<DateTime/from_epoch>. Floating values will be used for subsecond |
95 | percision, see L<DateTime> for details. |
96 | |
97 | =item from C<HashRef> |
98 | |
99 | Calls L<DateTime/new> with the hash entries as arguments. |
100 | |
101 | =back |
102 | |
103 | =item L<DateTime::Duration> |
104 | |
105 | A class type for L<DateTime::Duration> |
106 | |
107 | =over 4 |
108 | |
109 | =item from C<Num> |
110 | |
111 | Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument. |
112 | |
113 | Note that due to leap seconds, DST changes etc this may not do what you expect. |
114 | For instance passing in C<86400> is not always equivalent to one day, although |
115 | there are that many seconds in a day. See L<DateTime/"How Date Math is Done"> |
116 | for more details. |
117 | |
118 | =item from C<HashRef> |
119 | |
120 | Calls L<DateTime::Duration/new> with the hash entries as arguments. |
121 | |
122 | =back |
4664d531 |
123 | |
124 | =item L<DateTime::Locale> |
125 | |
ec3f7a50 |
126 | A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>. |
127 | |
128 | =over 4 |
129 | |
130 | =item from C<Str> |
131 | |
132 | The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to |
4664d531 |
133 | L<DateTime::Locale/load>. |
134 | |
ec3f7a50 |
135 | =item from L<Locale::Maktext> |
136 | |
137 | The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>. |
138 | |
4664d531 |
139 | =item L<DateTime::TimeZone> |
140 | |
ec3f7a50 |
141 | A class type for L<DateTime::TimeZone>. |
142 | |
143 | =over 4 |
144 | |
145 | =item from C<Str> |
146 | |
147 | Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more |
148 | details on the allowed values. |
149 | |
150 | Delegates 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 | |
156 | L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on |
157 | #moose for commit bits. |
158 | |
159 | =head1 AUTHOR |
160 | |
161 | Yuval 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 |