File Coverage

blib/lib/DateTimeX/Format.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package DateTimeX::Format;
2 1     1   38938 use Moose::Role;
  0            
  0            
3              
4             use strict;
5             use warnings;
6              
7             use DateTime;
8             use DateTime::Locale;
9             use DateTime::TimeZone;
10             use MooseX::Types::DateTime::ButMaintained qw/TimeZone Locale/;
11             use Carp;
12              
13             use namespace::clean -except => 'meta';
14              
15             requires 'parse_datetime';
16             requires 'format_datetime';
17              
18             our $VERSION = '1.04';
19              
20             has 'locale' => (
21             isa => Locale
22             , is => 'rw'
23             , coerce => 1
24             , predicate => 'has_locale'
25             );
26              
27             has 'time_zone' => (
28             isa => TimeZone
29             , is => 'rw'
30             , coerce => 1
31             , predicate => 'has_time_zone'
32             );
33             has 'defaults' => ( isa => 'Bool', is => 'ro', default => 1 );
34             has 'debug' => ( isa => 'Bool', is => 'ro', default => 0 );
35              
36             around 'parse_datetime' => sub {
37             my ( $sub, $self, $time, $override, @args ) = @_;
38              
39             ## Set Timezone: from args, then from object
40             my $time_zone;
41             if ( defined $override->{time_zone} ) {
42             $time_zone = to_TimeZone( $override->{time_zone} );
43             }
44             elsif ( $self->has_time_zone ) {
45             $time_zone = $self->time_zone;
46             }
47             elsif ( $self->defaults ) {
48             carp "No time_zone supplied to constructor or the call to parse_datetime -- defaulting to floating\n"
49             if $self->debug
50             ;
51             $time_zone = DateTime::TimeZone->new( name => 'floating' );
52             }
53             else {
54             carp "No time_zone supplied instructed to not use defaults"
55             }
56              
57              
58             ## Set Locale: from args, then from object, then guess en_US
59             my $locale;
60             if ( defined $override->{locale} ) {
61             $locale = to_Locale( $override->{locale} );
62             }
63             elsif ( $self->has_locale ) {
64             $locale = $self->locale
65             }
66             elsif ( $self->defaults ) {
67             carp "No locale supplied to constructor or the call to parse_datetime -- defaulting to en_US\n"
68             if $self->debug
69             ;
70             $locale = DateTime::Locale->load( 'en_US' );
71             }
72             else {
73             carp "No time_zone supplied instructed to not use defaults"
74             }
75              
76             my $env = {
77             time_zone => $time_zone
78             , locale => $locale
79             , override => $override ## A copy of the original hash
80             };
81            
82             ## Calls the sub ( time, env, addtl args )
83             my $dt = $self->$sub( $time , $env , @args );
84              
85             warn "Module did not return DateTime object"
86             if ! blessed $dt eq 'DateTime'
87             && $self->debug
88             ;
89              
90             $dt;
91            
92             };
93              
94             sub new_datetime {
95             my ( $self, $args ) = @_;
96              
97             if ( $self->debug ) {
98             carp "Year Month and Day should be specified if Year Month or Day is specified\n"
99             if ( defined $args->{day} || defined $args->{month} || defined $args->{year} )
100             && ( ! defined $args->{day} or ! defined $args->{month} or ! defined $args->{year} )
101             ;
102             carp "Marking Year Month and Day as a default\n"
103             if not (defined $args->{day} || defined $args->{months} || defined $args->{year})
104             ;
105             }
106              
107             DateTime->new(
108             time_zone => $args->{time_zone}
109             , locale => $args->{locale}
110              
111             , nanosecond => ( defined ( $args->{nanosecond} ) ? $args->{nanosecond} : 0 )
112             , second => ( defined ( $args->{second} ) ? $args->{second} : 0 )
113             , minute => ( defined ( $args->{minute} ) ? $args->{minute} : 0 )
114             , hour => ( defined ( $args->{hour} ) ? $args->{hour} : 0 )
115              
116             , day => ( defined( $args->{day} ) ? $args->{day} : 1 )
117             , month => ( defined( $args->{month} ) ? $args->{month} : 1 )
118             , year => ( defined( $args->{year} ) ? $args->{year} : 1 )
119             );
120              
121             }
122              
123             1;
124              
125             __END__
126              
127             =head1 NAME
128              
129             DateTimeX::Format - Moose Roles for building next generation DateTime formats
130              
131             =head1 SYNOPSIS
132              
133             package DateTimeX::Format::Bleh;
134             use Moose;
135             with 'DateTimeX::Format';
136              
137             sub parse_datetime {
138             my ( $self, $time, $env, @args ) = @_;
139             }
140              
141             sub format_datetime {
142             my ( $self, @args ) = @_;
143             }
144              
145             my $dtxf = DateTimeX::Format::Bleh->new({
146             locale => $locale
147             , time_zone => $time_zone
148             , debug => 0|1
149             , defaults => 0|1
150             });
151              
152             $dtxf->debug(0);
153             $dtxf->time_zone( $time_zone );
154             $dtxf->locale( $locale );
155             $dtxf->defaults(1);
156              
157             my $dt = $dtxf->parse_datetime( $time, {locale=>$locale_for_call} );
158              
159             my $env = {
160             time_zone => $time_zone_for_call
161             , locale => $locale_for_call
162             };
163             my $dt = $dtxf->parse_datetime( $time, $env, @additional_arguments );
164             my $dt = $dtxf->parse_datetime( $time, {time_zone=>$time_zone_for_call} )
165            
166             ## if your module requires a pattern, or has variable time-input formats
167             ## see the Moose::Role DateTimeX::Format::CustomPattern
168             package DateTimeX::Format::Strptime;
169             use Moose;
170             with 'DateTimeX::Format::CustomPattern';
171             with 'DateTimeX::Format';
172              
173              
174             =head1 DESCRIPTION
175              
176             This L<Moose::Role> provides an environment at instantation which can be overriden in the call to L<parse_data> by supplying a hash of the environment.
177              
178             All of the DateTime based methods, locale and time_zone, coerce in accordence to what the docs of L<MooseX::Types::DateTime::ButMaintained> say -- the coercions apply to both runtime calls and constructors.
179              
180             In addition this module provides two other accessors to assist in the development of modules in the L<DateTimeX::Format> namespace, these are C<debug>, and C<defaults>.
181              
182             =head1 OBJECT ENVIRONMENT
183              
184             All of these slots correspond to your object environment: they can be supplied in the constructor, or through accessors.
185              
186             =over 4
187              
188             =item * locale
189              
190             Can be overridden in the call to ->parse_datetime.
191              
192             See the docs at L<MooseX::Types::DateTime::ButMaintained> for informations about the coercions.
193              
194             =item * time_zone
195              
196             Can be overridden in the call to ->parse_datetime.
197              
198             See the docs at L<MooseX::Types::DateTime::ButMaintained> for informations about the coercions.
199              
200             =item * debug( 1 | 0* )
201              
202             Set to one to get debugging information
203              
204             =item * defaults( 1* | 0 )
205              
206             Set to 0 to force data to be sent to the module
207              
208             =back
209              
210             =head1 HELPER FUNCTIONS
211              
212             =over 4
213              
214             =item new_datetime( $hashRef )
215              
216             Takes a hashRef of the name value pairs to hand off to DateTime->new
217              
218             =back
219              
220             =head1 AUTHOR
221              
222             Evan Carroll, C<< <me at evancarroll.com> >>
223              
224             =head1 BUGS
225              
226             Please report any bugs or feature requests to C<bug-datetimex-format at rt.cpan.org>, or through
227             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DateTimeX-Format>. I will be notified, and then you'll
228             automatically be notified of progress on your bug as I make changes.
229              
230             =head1 SUPPORT
231              
232             You can find documentation for this module with the perldoc command.
233              
234             perldoc DateTimeX::Format
235              
236             You can also look for information at:
237              
238             =over 4
239              
240             =item * RT: CPAN's request tracker
241              
242             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DateTimeX-Format>
243              
244             =item * AnnoCPAN: Annotated CPAN documentation
245              
246             L<http://annocpan.org/dist/DateTimeX-Format>
247              
248             =item * CPAN Ratings
249              
250             L<http://cpanratings.perl.org/d/DateTimeX-Format>
251              
252             =item * Search CPAN
253              
254             L<http://search.cpan.org/dist/DateTimeX-Format/>
255              
256             =back
257              
258             =head1 ACKNOWLEDGEMENTS
259              
260             Dave Rolsky -- provided some assistance with how DateTime works.
261              
262             =head1 COPYRIGHT & LICENSE
263              
264             Copyright 2009 Evan Carroll, all rights reserved.
265              
266             This program is free software; you can redistribute it and/or modify it
267             under the same terms as Perl itself.