DBR/Config/Trans/UnixTime.pm

filter: Statementstmt 51.9% 68 / 131 Branchbran 26.0% 13 / 50 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 65.5% 19 / 29 Podpod 0.0% 0 / 4 totaltotal 45.0% 101 / 224 CC 28 SCAR 19.3
CC
28
Cov
44%
_manip
19.1
format
12.0
backward
8.3
CRAP 6.9
« DBR/Config/Scope.pm ↑ Summary DBR/Query/Insert.pm »
1 # the contents of this file are Copyright (c) 2009-2011 Daniel Norman
2 # This program is free software; you can redistribute it and/or
3 # modify it under the terms of the GNU General Public License as
4 # published by the Free Software Foundation.
5  
6 package DBR::Config::Trans::UnixTime;
7  
8 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use base 'DBR::Config::Trans';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 #use Date::Parse ();
12 18 ▶use Time::ParseDate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use POSIX qw(strftime tzset);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub new { die "Should not get here" }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
16  
17 22 ▶sub init {
Subroutine init: called CC=2 SCAR=7.1
Pod undocumented
18 22 my $self = shift;
19 22 ▶ $self->{tzref} = $self->{session}->timezone_ref or return $self->_error('failed to get timezone ref');
Branch: 1 branch
BranchTrueFalseTotal
$self->{"tzref"} = $self->{"session"}->timezone_ref or return $self->_error("failed to get timezone ref") 22 0 22
20 22 return 1;
21 }
22  
23 14 ▶sub forward{
Subroutine forward: called CC=1 SCAR=0.0
Pod undocumented
24 14 my $self = shift;
25 14 my $unixtime = shift;
26 14 return bless( [$unixtime,$self->{tzref}] , 'DBR::_UXTIME');
27 }
28  
29 13 ▶sub backward{
Subroutine backward: called CC=6 SCAR=21.2
Pod undocumented
30 13 my $self = shift;
31 13 my $value = shift;
32  
33 13 ▶ return undef unless defined($value) && length($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 13 13
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
34  
35 13 ▶ if(ref($value) eq 'DBR::_UXTIME'){ #ahh... I know what this is
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'DBR::_UXTIME') { } 0 13 13
elsif ($value =~ /^\d+$/) { } 0 13 13
36 0 return $value->unixtime;
37  
38 }elsif($value =~ /^\d+$/){ # smells like a unixtime
39 0 return $value;
40  
41 }else{
42 13 local($ENV{TZ}) = ${$self->{tzref}}; tzset(); # Date::Parse doesn't accept timezone in the way we want to specify it. Lame.
43  
44 # Ok... so Date::Parse is kinda cool and all, except for the fact that it breaks horribly on
45 # Non DST-specific timezone prefixes, like PT, MT, CT, ET. Treats them all like GMT.
46 # Even strptime freaks out on it. What gives Graham?
47 # P.S. glass house here throwing stones, but try adding a comment or two.
48  
49 #my $uxtime = Date::Parse::str2time($value);
50 13 my $uxtime = Time::ParseDate::parsedate($value);
51  
52 13 ▶ unless($uxtime){
Branch: 1 branch
BranchTrueFalseTotal
unless ($uxtime) 0 13 13
53 0 $self->_error("Invalid time '$value'");
54 0 return ();
55 }
56  
57 13 return $uxtime;
58 }
59  
60 }
61  
62 package DBR::_UXTIME;
63  
64 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 18 ▶use POSIX qw(strftime tzset);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 use overload
68 #values
69 14 ▶'""' => sub { $_[0]->datetime },
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine __ANON__: called CC=1 SCAR=0.0
70 48 ▶'0+' => sub { $_[0]->unixtime },
Subroutine __ANON__: called CC=1 SCAR=0.0
71  
72 #operators
73 6 ▶ '+' => sub { $_[0]->_manip( $_[1], 'add' ) || croak "Invalid date manipulation '$_[1]'" },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_manip($_[1], "add") or croak("Invalid date manipulation '$_[1]'") 6 0 6
74 0 ▶ '-' => sub { $_[0]->_manip( $_[1], 'subtract' ) || croak "Invalid date manipulation '$_[1]'" },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_manip($_[1], "subtract") or croak("Invalid date manipulation '$_[1]'") 0 0 0
75  
76 # Some ideas:
77 #
78  
79 18 'fallback' => 1,
80 #'nomethod' => sub {croak "UnixTime object: Invalid operation '$_[3]' The ways in which you can use UnixTime objects is restricted"}
81 18 ;
82  
83 18 *TO_JSON = \&datetime;
84  
85 60 ▶sub unixtime { $_[0][0] || '' };
Subroutine unixtime: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0][0] or "" 60 0 60
86  
87 # Using $ENV{TZ} and the posix functions is ugly... and about 60x faster than the alternative in benchmarks
88  
89 0 ▶sub date {
Subroutine date: not called CC=2 SCAR=17.9
90 0 ▶ return '' unless defined($_[0][0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] 0 0 0
91 0 local($ENV{TZ}) = ${$_[0][1]}; tzset();
92 0 return strftime ("%D", localtime($_[0][0]));
93 }
94  
95 0 ▶sub time {
Subroutine time: not called CC=2 SCAR=17.9
96 0 ▶ return '' unless defined($_[0][0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] 0 0 0
97 0 local($ENV{TZ}) = ${$_[0][1]}; tzset();
98 0 return strftime ("%H:%M:%S %Z", localtime($_[0][0]));
99 }
100  
101 14 ▶sub datetime {
Subroutine datetime: called CC=2 SCAR=7.0
102 14 ▶ return '' unless defined($_[0][0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] 0 14 14
103 14 local($ENV{TZ}) = ${$_[0][1]}; tzset();
104 14 return strftime ("%D %H:%M:%S %Z", localtime($_[0][0]));
105 }
106  
107 0 ▶sub fancytime {
Subroutine fancytime: not called CC=2 SCAR=17.9
108 0 ▶ return '' unless defined($_[0][0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] 0 0 0
109 0 local($ENV{TZ}) = ${$_[0][1]}; tzset();
110 0 return strftime ("%I:%M:%S %p %Z", localtime($_[0][0]));
111 }
112  
113 0 ▶sub fancydatetime {
Subroutine fancydatetime: not called CC=2 SCAR=17.9
114 0 ▶ return '' unless defined($_[0][0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] 0 0 0
115 0 local($ENV{TZ}) = ${$_[0][1]}; tzset();
116 0 my $v = strftime ("%A %B %e %l:%M%p %Y", localtime($_[0][0]));
117 0 $v =~ s/\s+/ /g;
118 0 $v =~ s/(AM|PM)/lc($1)/e;
119 0 return $v;
120 }
121  
122 0 ▶sub fancydate {
Subroutine fancydate: not called CC=2 SCAR=17.9
123 0 ▶ return '' unless defined($_[0][0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] 0 0 0
124 0 local($ENV{TZ}) = ${$_[0][1]}; tzset();
125 0 return strftime ("%A %B %e, %Y", localtime($_[0][0]));
126 }
127  
128 #format takes a strftime format string as an argument
129 0 ▶sub format {
Subroutine format: not called CC=3 SCAR=24.8
130 0 ▶ return '' unless defined($_[0][0]) && length($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] and length $_[1] 0 0 0
Condition0%0/3
defined $_[0][0] and length $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0][0] and length $_[1]
defined $_[0][0] length $_[1]
Truth table0%0/3
A and B
A= defined $_[0][0]B= length $_[1]
ABresult
0X0
100
111
131 0 local($ENV{TZ}) = ${$_[0][1]}; tzset();
132 0 return strftime ($_[1], localtime($_[0][0]));
133 }
134  
135 0 ▶sub midnight{
Subroutine midnight: not called CC=2 SCAR=17.9
136 0 my $self = shift;
137  
138 0 ▶ return '' unless defined($self->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[0] 0 0 0
139 0 local($ENV{TZ}) = ${$self->[1]}; tzset();
140 0 my ($sec,$min,$hour) = localtime($self->[0]);
141  
142 0 my $midnight = $self->[0] - ($sec + ($min * 60) + ($hour * 3600) ); # rewind!
143 0 return $self->new($midnight);
144  
145 }
146  
147 0 ▶sub endofday{
Subroutine endofday: not called CC=2 SCAR=17.9
148 0 my $self = shift;
149  
150 0 ▶ return '' unless defined($self->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[0] 0 0 0
151  
152 0 local($ENV{TZ}) = ${$self->[1]}; tzset();
153 0 my ($sec,$min,$hour) = localtime($self->[0]);
154  
155 0 my $endofday = $self->[0] + 86399 - ($sec + ($min * 60) + ($hour * 3600) ) ; # rewind!
156 0 return $self->new($endofday);
157 }
158  
159 6 ▶sub _manip{
Subroutine _manip: called CC=9 SCAR=29.5
160 6 my $self = shift;
161 6 my $manip = shift;
162 6 my $mode = shift;
163  
164 6 $manip =~ s/^\s+|\s+$//g;
165 6 ▶ return undef unless $manip;
Branch: 1 branch
BranchTrueFalseTotal
unless $manip 0 6 6
166  
167 6 my ($number, $unit) = $manip =~ /^(\d+)\s+([A-Za-z]+?)s?$/;
168 6 $unit = lc($unit);
169  
170 6 my $unixtime = $self->unixtime;
171  
172 # This isn't actually the correct way to do this, on account of DST nd leap year and so on,
173 # just a proof of concept. Should probably just farm it out to Date::Manip
174  
175 6 my $diff;
176 6 ▶ if($unit eq 'second'){
Branch: 5 branches
BranchTrueFalseTotal
if ($unit eq 'second') { } 0 6 6
elsif ($unit eq 'minute') { } 0 6 6
elsif ($unit eq 'hour') { } 6 0 6
elsif ($unit eq 'day') { } 0 0 0
elsif ($unit eq 'year') { } 0 0 0
177 0 $diff = $number
178 }elsif($unit eq 'minute'){
179 0 $diff = $number * 60;
180 }elsif($unit eq 'hour'){
181 6 $diff = $number * 3600;
182 }elsif($unit eq 'day'){
183 0 $diff = $number * 86400;
184 }elsif($unit eq 'year'){
185 0 $diff = $number * 31536000;
186 }else{
187 0 return undef;
188 }
189  
190 6 ▶ if ($mode eq 'add'){
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'add') { } 6 0 6
elsif ($mode eq 'subtract') { } 0 0 0
191 6 return $self->new( $unixtime + $diff );
192 }elsif($mode eq 'subtract'){
193 0 return $self->new( $unixtime - $diff );
194 }
195  
196 0 return undef;
197  
198 }
199  
200 # uxtime , tzref
201 6 ▶sub new{ bless([ $_[1], $_[0][1] ],'DBR::_UXTIME') }
Subroutine new: called CC=1 SCAR=0.0
202  
203 18 1;
« DBR/Config/Scope.pm ↑ Summary DBR/Query/Insert.pm »