File Coverage

blib/lib/DateTimeX/Format.pm
Criterion Covered Total %
statement 32 37 86.4
branch 0 6 0.0
condition 0 32 0.0
subroutine 11 12 91.6
pod 1 1 100.0
total 44 88 50.0


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