File Coverage

blib/lib/DateTimeX/Factory.pm
Criterion Covered Total %
statement 42 43 97.6
branch 2 4 50.0
condition 4 8 50.0
subroutine 19 20 95.0
pod 13 13 100.0
total 80 88 90.9


line stmt bran cond sub pod time code
1             package DateTimeX::Factory;
2 4     4   143804 use 5.010_001;
  4         16  
  4         158  
3 4     4   25 use strict;
  4         6  
  4         137  
4 4     4   20 use warnings;
  4         22  
  4         212  
5              
6             our $VERSION = '1.00';
7              
8             use Class::Accessor::Lite (
9 4         34 new => 0,
10             rw => [qw/default_options/],
11 4     4   5367 );
  4         7945  
12              
13 4     4   7798 use DateTime;
  4         1468216  
  4         178  
14 4     4   6779 use DateTime::Format::Strptime;
  4         40465  
  4         2820  
15              
16             sub new {
17 7     7 1 6753 my ($class, %default_options) = @_;
18 7         49 bless {default_options => \%default_options}, $class;
19             }
20              
21 0     0 1 0 sub create {shift->_call_factory_method('new' => @_)}
22 6     6 1 49 sub now {shift->_call_factory_method('now' => @_)}
23 3     3 1 36538 sub from_epoch {shift->_call_factory_method('from_epoch' => @_)}
24 9     9 1 1126625 sub today {shift->_call_factory_method('today' => @_)}
25 3     3 1 12654 sub last_day_of_month {shift->_call_factory_method('last_day_of_month' => @_)}
26 3     3 1 3485 sub from_day_of_year {shift->_call_factory_method('from_day_of_year' => @_)}
27              
28             sub _call_factory_method {
29 24     24   72 my ($self, $method, @params) = @_;
30 24         38 DateTime->$method(%{$self->default_options}, @params);
  24         161  
31             }
32              
33             sub strptime {
34 12     12 1 17902 my ($self, $string, $pattern) = @_;
35 12         44 return DateTime::Format::Strptime->new(
36             pattern => $pattern,
37 12         18 %{$self->default_options},
38             )->parse_datetime($string);
39             }
40              
41             sub from_mysql_datetime {
42 3     3 1 6894 my ($self, $string) = @_;
43              
44 3 50 33     29 return if !defined $string || $string eq '0000-00-00 00:00:00';
45              
46 3         11 return $self->strptime($string, '%Y-%m-%d %H:%M:%S');
47             }
48              
49             sub from_ymd {
50 6     6 1 6090 my ($self, $string, $delimiter) = @_;
51              
52 6   100     20 $delimiter //= '-';
53 6         21 return $self->strptime($string, join($delimiter, '%Y','%m','%d'));
54             }
55              
56             sub from_mysql_date {
57 3     3 1 5618 my ($self, $string) = @_;
58              
59 3 50 33     27 return if !defined $string || $string eq '0000-00-00';
60 3         10 return $self->from_ymd($string);
61             }
62              
63 3     3 1 31849 sub yesterday {shift->today(@_)->subtract(days => 1)}
64 3     3 1 14495 sub tommorow {shift->today(@_)->add(days => 1)}
65              
66             1;
67             __END__