ORM/Date.pm

filter: Statementstmt 30.3% 34 / 112 Branchbran 5.8% 2 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 25.0% 10 / 40 Podpod 84.0% 21 / 25 totaltotal 31.7% 67 / 211 CC 18 SCAR 16.2
CC
18
Cov
25%
_check_args
20.0
new_mysql
12.0
__ANON__
6.0
CRAP 5.1
« ORM/Filter/Interval.pm ↑ Summary ORM/Filter/Group.pm »
1 #
2 # DESCRIPTION
3 # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl
4 # library that implements object-relational mapping. Its features are
5 # much similar to those of Java's Hibernate library, but interface is
6 # much different and easier to use.
7 #
8 # AUTHOR
9 # Alexey V. Akimov <akimov_alexey@sourceforge.net>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2005-2006 Alexey V. Akimov
13 #
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
18 #
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
23 #
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #
28  
29 package ORM::Date;
30  
31 6 $VERSION=0.8;
32  
33 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶use ORM::Datetime;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 use overload
37 0 ▶ '>' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch > $_[1]->epoch; },
Subroutine BEGIN: called CC=1 SCAR=5.9
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
38 0 ▶ '<' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch < $_[1]->epoch; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
39 0 ▶ '>=' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch >= $_[1]->epoch; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
40 0 ▶ '<=' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch <= $_[1]->epoch; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
41 0 ▶ '==' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch == $_[1]->epoch; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
42 0 ▶ '!=' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch != $_[1]->epoch; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
43 0 ▶ '<=>' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch <=> $_[1]->epoch; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
44 0 ▶ 'cmp' => sub { ( my $err = _check_args( @_ ) ); $err && croak $err; $_[0]->epoch cmp $_[1]->epoch; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$err and croak($err) 0 0 0
45 6 'fallback' => 1;
46  
47 6 my $use_local_tz = 1;
48  
49 ##
50 ## CONSTRUCTORS
51 ##
52  
53 sub new_epoch
54 128 ▶{
Subroutine new_epoch: called CC=1 SCAR=0.0
Pod documented
55 128 my $class = shift;
56 128 my $epoch = shift;
57  
58 128 return bless { epoch=>$epoch }, $class;
59 }
60  
61 sub new
62 7 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
63 7 my $class = shift;
64 7 my $array = shift;
65  
66 7 my $time = POSIX::mktime
67 (
68 $array->[5],
69 $array->[4],
70 $array->[3],
71 $array->[2],
72 $array->[1]-1,
73 $array->[0]-1900,
74 0,0,-1
75 );
76  
77 7 ▶ unless( defined $time )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 0 7 7
78 {
79 0 croak "Specified time [".join( ',',@$array )."] cannot be represented";
80 }
81  
82 7 $class->new_epoch( $time );
83 }
84  
85 sub new_mysql
86 0 ▶{
Subroutine new_mysql: not called CC=3 SCAR=24.8
Pod documented
87 0 my $class = shift;
88 0 my $str = shift;
89 0 my $date;
90  
91 0 ▶ if( $str =~ /^(\d{4,4})\-0*(\d+)\-0*(\d+)$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /^(\d{4,4})\-0*(\d+)\-0*(\d+)$/) { } 0 0 0
elsif ($str =~ /^(\d{4,4})\-0*(\d+)\-0*(\d+)(\s+0*(\d+)\:0*(\d+)(\:0*(\d+))?)$/) { } 0 0 0
92 {
93 0 $date = $class->new( [ $1, $2, $3, 0, 0, 0 ] );
94 }
95 elsif( $str =~ /^(\d{4,4})\-0*(\d+)\-0*(\d+)(\s+0*(\d+)\:0*(\d+)(\:0*(\d+))?)$/ )
96 {
97 0 $date = $class->new( [ $1, $2, $3, $5, $6, $8 ] );
98 }
99  
100 0 return $date;
101 }
102  
103 sub copy
104 0 ▶{
Subroutine copy: not called CC=2 SCAR=17.9
Pod documented
105 0 my $class = shift;
106 0 my $self;
107  
108 0 ▶ if( ref $class )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
109 {
110 0 $self = $class;
111 0 $class = ref $class;
112 }
113 else
114 {
115 0 $self = shift;
116 }
117  
118 0 return $class->new_epoch( $self->{epoch} );
119 }
120  
121 sub diff
122 0 ▶{
Subroutine diff: not called CC=1 SCAR=6.9
Pod documented
123 0 my $self = shift;
124 0 my @diff = @{$_[0]};
125  
126 0 return (ref $self)->new
127 (
128 [
129 $self->year + $diff[0],
130 $self->month + $diff[1],
131 $self->mday + $diff[2],
132 $self->hour + $diff[3],
133 $self->min + $diff[4],
134 $self->sec + $diff[5],
135 ],
136 );
137 }
138  
139 sub current
140 0 ▶{
Subroutine current: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my $class = shift;
142 0 my $date = $class->new_epoch( time );
143 0 $class->new( [$date->year,$date->month,$date->mday,0,0,0] );
144 }
145 sub earlier24h
146 0 ▶{
Subroutine earlier24h: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my $class = shift;
148 0 my $date = $class->new_epoch( time-24*60*60 );
149 0 $class->new( [$date->year,$date->month,$date->mday,0,0,0] );
150 }
151  
152 0 ▶sub date { ORM::Date->new_epoch( $_[0]->epoch ); }
Subroutine date: not called CC=1 SCAR=6.9
Pod documented
153 0 ▶sub datetime { ORM::Datetime->new_epoch( $_[0]->epoch ); }
Subroutine datetime: not called CC=1 SCAR=6.9
Pod documented
154  
155 ##
156 ## OBJECT PROPERTIES
157 ##
158  
159 26 ▶sub epoch { $_[0]->{epoch}; }
Subroutine epoch: called CC=1 SCAR=0.0
Pod documented
160 0 ▶sub sec { $_[0]->_tz_time( $_[0]->epoch )->[0]; }
Subroutine sec: not called CC=1 SCAR=6.9
Pod documented
161 0 ▶sub min { $_[0]->_tz_time( $_[0]->epoch )->[1]; }
Subroutine min: not called CC=1 SCAR=6.9
Pod documented
162 0 ▶sub hour { $_[0]->_tz_time( $_[0]->epoch )->[2]; }
Subroutine hour: not called CC=1 SCAR=6.9
Pod documented
163 0 ▶sub mday { $_[0]->_tz_time( $_[0]->epoch )->[3]; }
Subroutine mday: not called CC=1 SCAR=6.9
Pod documented
164 0 ▶sub wday { $_[0]->_tz_time( $_[0]->epoch )->[6]; }
Subroutine wday: not called CC=1 SCAR=6.9
Pod documented
165 0 ▶sub yday { $_[0]->_tz_time( $_[0]->epoch )->[7]; }
Subroutine yday: not called CC=1 SCAR=6.9
Pod documented
166 0 ▶sub month { $_[0]->_tz_time( $_[0]->epoch )->[4]; }
Subroutine month: not called CC=1 SCAR=6.9
Pod documented
167 0 ▶sub year { $_[0]->_tz_time( $_[0]->epoch )->[5]; }
Subroutine year: not called CC=1 SCAR=6.9
Pod documented
168  
169 sub mysql_date
170 0 ▶{
Subroutine mysql_date: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172 0 my $time = $self->_tz_time( $self->epoch );
173  
174 0 sprintf '%04d-%02d-%02d', $time->[5], $time->[4], $time->[3];
175 }
176  
177 sub mysql_time
178 0 ▶{
Subroutine mysql_time: not called CC=1 SCAR=6.9
Pod documented
179 0 my $self = shift;
180 0 my $time = $self->_tz_time( $self->epoch );
181  
182 0 sprintf '%02d:%02d:%02d', $time->[2], $time->[1], $time->[0];
183 }
184  
185 sub mysql_datetime
186 26 ▶{
Subroutine mysql_datetime: called CC=1 SCAR=0.0
Pod documented
187 26 my $self = shift;
188 26 my $time = $self->_tz_time( $self->epoch );
189  
190 26 sprintf '%04d-%02d-%02d %02d:%02d:%02d'
191 , $time->[5], $time->[4], $time->[3]
192 , $time->[2], $time->[1], $time->[0];
193 }
194  
195 sub datetime_str
196 0 ▶{
Subroutine datetime_str: not called CC=1 SCAR=6.9
Pod documented
197 0 my $self = shift;
198  
199 0 scalar $self->_tz_time_str( $self->epoch );
200 }
201  
202 ##
203 ## OBJECT METHODS
204 ##
205  
206 0 ▶sub set_epoch { $_[0]->{epoch} = $_[1]; }
Subroutine set_epoch: not called CC=1 SCAR=6.9
Pod documented
207  
208 ##
209 ## CLASS PROPERTIES
210 ##
211  
212 1 ▶sub use_local_tz { $use_local_tz = 1; }
Subroutine use_local_tz: called CC=1 SCAR=0.0
Pod undocumented
213 0 ▶sub use_utc_tz { $use_local_tz = 0; }
Subroutine use_utc_tz: not called CC=1 SCAR=6.9
Pod undocumented
214  
215 ##
216 ## PROTECTED PROPERTIES
217 ##
218  
219 sub _tz_time
220 26 ▶{
Subroutine _tz_time: called CC=2 SCAR=7.0
221 26 my $class = shift;
222 26 my $time = shift;
223 26 ▶ my @time = $use_local_tz ? localtime $time : gmtime $time;
Branch: 1 branch
BranchTrueFalseTotal
$use_local_tz ? : 26 0 26
224  
225 26 $time[4] ++;
226 26 $time[5] += 1900;
227  
228 26 return \@time;
229 }
230  
231 sub _tz_time_str
232 0 ▶{
Subroutine _tz_time_str: not called CC=2 SCAR=17.9
233 0 my $class = shift;
234 0 my $time = shift;
235  
236 0 ▶ return $use_local_tz ? localtime $time : gmtime $time;
Branch: 1 branch
BranchTrueFalseTotal
$use_local_tz ? : 0 0 0
237 }
238  
239 sub _check_args
240 0 ▶{
Subroutine _check_args: not called CC=4 SCAR=30.0
241 0 ▶ my @arg = $_[2] ? ( $_[1], $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
242 0 my $err = undef;
243  
244 0 ▶ if( ! UNIVERSAL::isa( $arg[0], 'ORM::Date' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not UNIVERSAL::isa($arg[0], 'ORM::Date')) { } 0 0 0
elsif (not UNIVERSAL::isa($arg[1], 'ORM::Date')) { } 0 0 0
245 {
246 0 $err = "First arg must be an 'ORM::Date' instance.";
247 }
248 elsif( ! UNIVERSAL::isa( $arg[1], 'ORM::Date' ) )
249 {
250 0 $err = "Second arg must be an 'ORM::Date' instance.";
251 }
252  
253 0 $err;
254 }
255  
256 6 1;
« ORM/Filter/Interval.pm ↑ Summary ORM/Filter/Group.pm »