File Coverage

blib/lib/MooseX/Types/DateTime/ButMaintained.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package MooseX::Types::DateTime::ButMaintained;
2 1     1   32600 use strict;
  1         3  
  1         36  
3 1     1   6 use warnings;
  1         2  
  1         51  
4              
5             our $VERSION = "0.16";
6              
7 1     1   818 use Moose 0.41 ();
  0            
  0            
8             use DateTime ();
9             use DateTime::Locale ();
10             use DateTime::TimeZone ();
11             use Olson::Abbreviations 0.03 qw();
12              
13             use MooseX::Types::Moose 0.30 qw/Num HashRef Str/;
14              
15             use MooseX::Types 0.30 -declare => [qw( DateTime Duration TimeZone Locale Now )];
16              
17             use namespace::autoclean;
18              
19             class_type "DateTime";
20             class_type "DateTime::Duration";
21             class_type "DateTime::TimeZone";
22             class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
23              
24             subtype DateTime, as 'DateTime';
25             subtype Duration, as 'DateTime::Duration';
26             subtype TimeZone, as 'DateTime::TimeZone';
27             subtype Locale, as 'DateTime::Locale';
28              
29             subtype( Now, as Str, where { $_ eq 'now' },
30             ( $Moose::VERSION >= 2.0100
31             ? Moose::Util::TypeConstraints::inline_as {
32             'no warnings "uninitialized";'.
33             '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"';
34             }
35             : Moose::Util::TypeConstraints::optimize_as {
36             no warnings 'uninitialized';
37             !ref($_[0]) and $_[0] eq 'now';
38             }
39             )
40             );
41              
42             our %coercions = (
43             DateTime => [
44             from Num, via { 'DateTime'->from_epoch( epoch => $_ ) }
45             , from HashRef, via { 'DateTime'->new( %$_ ) }
46             , from Now, via { 'DateTime'->now }
47             ]
48              
49             , "DateTime::Duration" => [
50             from Num, via { DateTime::Duration->new( seconds => $_ ) }
51             , from HashRef, via { DateTime::Duration->new( %$_ ) }
52             ]
53              
54             , "DateTime::TimeZone" => [
55             from Str, via {
56             # No abbreviation - assumed if we don't have a '/'
57             if ( m,/|floating|local, ) {
58             return DateTime::TimeZone->new( name => $_ );
59             }
60             # Abbreviation - assumed if we do have a '/'
61             # returns a DateTime::TimeZone::OffsetOnly
62             else {
63             my $offset = Olson::Abbreviations->new({ tz_abbreviation => $_ })->get_offset;
64             return DateTime::TimeZone->new( name => $offset );
65             }
66             }
67             ]
68              
69             , "DateTime::Locale" => [
70             from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext")
71             , via { DateTime::Locale->load($_->language_tag) }
72             , from Str, via { DateTime::Locale->load($_) }
73             ]
74             );
75              
76             for my $type ( "DateTime", DateTime ) {
77             coerce $type => @{ $coercions{DateTime} };
78             }
79              
80             for my $type ( "DateTime::Duration", Duration ) {
81             coerce $type => @{ $coercions{"DateTime::Duration"} };
82             }
83              
84             for my $type ( "DateTime::TimeZone", TimeZone ) {
85             coerce $type => @{ $coercions{"DateTime::TimeZone"} };
86             }
87              
88             for my $type ( "DateTime::Locale", Locale ) {
89             coerce $type => @{ $coercions{"DateTime::Locale"} };
90             }
91              
92             1;
93              
94             __END__
95              
96             =head1 NAME
97              
98             MooseX::Types::DateTime::ButMaintained - L<DateTime> related constraints and coercions for Moose
99              
100             =head1 SYNOPSIS
101              
102             Export Example:
103              
104             use MooseX::Types::DateTime::ButMaintained qw(TimeZone);
105             has time_zone => (
106             isa => TimeZone
107             , is => "rw"
108             , coerce => 1
109             );
110             Class->new( time_zone => "Africa/Timbuktu" );
111             Class->new( time_zone => "CEST" );
112              
113             Namespaced Example:
114              
115             use MooseX::Types::DateTime::ButMaintained;
116             has time_zone => (
117             isa => 'DateTime::TimeZone'
118             , is => "rw"
119             , coerce => 1
120             );
121             Class->new( time_zone => "Africa/Timbuktu" );
122              
123             =head1 CONSTRAINTS
124              
125             =over 4
126              
127             =item L<DateTime>
128              
129             A class type for L<DateTime>.
130              
131             =over 4
132              
133             =item from C<Num>
134              
135             Uses L<DateTime/from_epoch>. Floating values will be used for subsecond percision, see L<DateTime> for details.
136              
137             =item from C<HashRef>
138              
139             Calls L<DateTime/new> with the hash entries as arguments.
140              
141             =back
142              
143             =item L<Duration>
144              
145             A class type for L<DateTime::Duration>
146              
147             =over 4
148              
149             =item from C<Num>
150              
151             Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
152              
153             Note that due to leap seconds, DST changes etc this may not do what you expect. For instance passing in C<86400> is not always equivalent to one day, although there are that many seconds in a day. See L<DateTime/"How Date Math is Done"> for more details.
154              
155             =item from C<HashRef>
156              
157             Calls L<DateTime::Duration/new> with the hash entries as arguments.
158              
159             =back
160              
161             =item L<DateTime::Locale>
162              
163             A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
164              
165             =over 4
166              
167             =item from C<Str>
168              
169             The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to L<DateTime::Locale/load>.
170              
171             =item from L<Locale::Maktext>
172              
173             The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
174              
175             =back
176              
177             =item L<DateTime::TimeZone>
178              
179             A class type for L<DateTime::TimeZone>, this now as of 0.05 coerces from non-globally ambigious Olson abbreviations, using L<Olson::Abbreviations>. This won't work for abbreviations like "EST" which are only unambigious if you know the locale. It will coerce from abbreviations like "CEST" though.
180              
181             =over 4
182              
183             =item from C<Str>
184              
185             Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more details on the allowed values.
186              
187             Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
188              
189             =back
190              
191             =back
192              
193             =head1 SEE ALSO
194              
195             L<MooseX::Types::DateTimeX>
196              
197             L<DateTime>
198              
199             =head1 AUTHOR
200              
201             =head2 Modern
202              
203             Evan Carroll E<lt>me+cpan@evancarroll.comE<gt>
204              
205             =head2 Yesteryear
206              
207             Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
208              
209             John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
210              
211             =head1 DESCRIPTION
212              
213             This module packages several L<Moose::Util::TypeConstraints> with coercions, designed to work with the L<DateTime> suite of objects.
214              
215             This module started as a fork of L<MooseX::Types::DateTime>. This history and explaination is as follows:
216             In Janurary 2009, I began a project to bring DateTime::Format::* stuff up to date with Moose. I created a framework that would greatly eliminate redundant code named L<DateTimeX::Format>. This project's adoption was slowed by then (and still currently) bundeled B<package> MooseX::Types::DateTime. MooseX::Types::DateTime was a badly packaged extention of two modules the self-titled MooseX::Types::DateTime, and another random module MooseX::Types::DateTimeX. In Februrary of the same year, I repackaged the module L<MooseX::Types::DateTimeX> with the authors blessing into a new package, for the purpose of removing its dependenices, namely L<Date::Manip>, from MooseX::Types::DateTime.
217              
218             Unfortunately, this just added confusion. Now, as of the time of writing L<MooseX::Types::DateTimeX> is available as a package, and it is available as a module which will be installed by L<MooseX::Types::DateTime>. The benefit of removing the dependency on L<MooseX::Types::DateTime> was never realized and the patch that updates the dependencies, and the build system remains in rt still as of writing.
219              
220             This module is just the L<MooseX::Types::DateTime> without the requirement on L<DateTimeX::Easy> (which requires L<DateTime::Manip>). As of 0.05 this module supports globally unique Olson abbreviations, and dies when they are not globally unique.
221              
222             =head1 COPYRIGHT
223              
224             Copyright (c) 2008 Yuval Kogman. All rights reserved
225             This program is free software; you can redistribute
226             it and/or modify it under the same terms as Perl itself.
227              
228             Modifications (c) 2009 Evan Carroll. All rights reserved
229             This program is free software; you can redistribute
230             it and/or modify it under the same terms as Perl itself.
231              
232             =cut