blib/lib/Time/Fuzzy.pm

filter: Statementstmt 100% 62 / 62 Branchbran 100% 4 / 4 Conditioncond 90.4% 19 / 21 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 94.6% 107 / 113 CC 10 SCAR 14.7
CC
10
Cov
98%
_fuzzy_low
9.0
fuzzy
2.0
BEGIN
1.0
CRAP 4.4
↑ Summary
1 #
2 # This file is part of Time::Fuzzy
3 # Copyright (c) 2007 Jerome Quelin, all rights reserved.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
7 #
8  
9 package Time::Fuzzy;
10  
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Class::Accessor::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use base qw[ Exporter Class::Accessor::Fast ];
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 our @EXPORT = qw[ fuzzy ];
20 4 __PACKAGE__->mk_accessors( qw[ dt fuzziness ] );
21  
22 4 our $VERSION = '0.36';
23 4 our $FUZZINESS = 'medium';
24  
25 #--
26 # private vars
27  
28 # - for high fuzziness
29 4 my %weektime = ( # define the periods of the week
30 'start of week' => [ 1 ],
31 'middle of week' => [ 2..4 ],
32 'end of week' => [ 5 ],
33 'week-end!' => [ 6,7 ],
34 );
35 4 my @weektime; # a 7-slots array, one for each days
36 { # init @weektime by walking %weektime
37 4 foreach my $wt ( keys %weektime ) {
38 16 my $days = $weektime{$wt};
39 16 $weektime[$_] = $wt for @$days;
40 }
41 }
42  
43 # - for medium fuzziness
44 4 my %daytime = ( # define the periods of the day
45 'night' => [ 0, 1, 2, 3, 4 ],
46 'early morning' => [ 5, 6, 7 ],
47 'morning' => [ 8, 9, 10 ],
48 'noon' => [ 11, 12, 13 ],
49 'afternoon' => [ 14, 15, 16, 17, 18 ],
50 'evening' => [ 19, 20, 21 ],
51 'late evening' => [ 22, 23 ],
52 );
53 4 my @daytime; # a 24-slots array, one for each hour
54 { # init @daytime by walking %daytime
55 4 foreach my $dt ( keys %daytime ) {
56 28 my $hours = $daytime{$dt};
57 28 $daytime[$_] = $dt for @$hours;
58 }
59 }
60  
61 # - for low fuzziness
62 4 my @hourtime = ( # defining the periods of the hour
63 "%s o'clock", 'five past %s', 'ten past %s',
64 'quarter past %s', 'twenty past %s', 'twenty five past %s',
65 'half past %s', 'twenty five to %2$s', 'twenty to %2$s',
66 'quarter to %2$s', 'ten to %2$s', 'five to %2$s',
67 q{%2$s o'clock}, # needed for 58-59
68 );
69 4 my @hours = (
70 'midnight',
71 qw[ one two three four five six seven eight nine ten eleven noon ],
72 qw[ one two three four five six seven eight nine ten eleven midnight ],
73 );
74  
75  
76 #--
77 # public subs
78  
79 39 ▶sub fuzzy {
Subroutine fuzzy: called CC=2 SCAR=7.0
Pod documented
80 39 ▶ my $dt = $_[0] || DateTime->now( time_zone=>'local' );
Condition66%2/3
$_[0] || 'DateTime'->now('time_zone', 'local')
l!l&&r!l&&!r
3720
MC/DC0%0/2
$_[0] || 'DateTime'->now('time_zone', 'local')
$_[0] 'DateTime'->now('time_zone', 'local')
Truth table66%2/3
A || B
A= $_[0]B= 'DateTime'->now('time_zone', 'local')
ABresult
1X1
011
000
81 39 my %fuzzysub = (
82 low => \&_fuzzy_low,
83 medium => \&_fuzzy_medium,
84 high => \&_fuzzy_high,
85 );
86 39 return $fuzzysub{$FUZZINESS}->($dt);
87 }
88  
89  
90 #--
91 # public methods
92  
93 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
94 1 my $pkg = shift;
95 1 my %params = (
96 dt => DateTime->now( time_zone=>'local' ),
97 fuzziness => $FUZZINESS,
98 @_,
99 );
100 1 return bless \%params, $pkg;
101 }
102  
103 4 ▶use overload '""' => \&as_str;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 3 ▶sub as_str {
Subroutine as_str: called CC=1 SCAR=0.0
Pod documented
105 3 my ($self) = @_;
106 3 my %fuzzysub = (
107 low => \&_fuzzy_low,
108 medium => \&_fuzzy_medium,
109 high => \&_fuzzy_high,
110 );
111 3 return $fuzzysub{$self->fuzziness}->($self->dt);
112 }
113  
114  
115 #--
116 # private subs
117  
118 #
119 # my $fuz = _fuzzy_low($dt)
120 #
121 # Return a fuzzy time defined by $dt. The fuzziness is a bit low, that
122 # is, 5 minutes in this case.
123 #
124 7 ▶sub _fuzzy_low {
Subroutine _fuzzy_low: called CC=9 SCAR=22.0
125 7 my ($dt1) = @_;
126  
127 7 my $sector = int( ($dt1->minute + 2) / 5 );
128 7 my $hour1 = $hours[$dt1->hour];
129
130 # compute next hour, for 2nd half of the hour.
131 7 my $dt2 = $dt1 + DateTime::Duration->new(hours=>1);
132 7 my $hour2 = $hours[$dt2->hour];
133  
134 # midnight or noon don't need o'clock appended.
135 7 ▶ return $hour1
Branch: 1 branch
BranchTrueFalseTotal
if $sector == 0 and $dt1->hour == 0 or $sector == 0 and $dt1->hour == 12 2 5 7
Condition100%9/9
$sector == 0 and $dt1->hour == 0 or $sector == 0 and $dt1->hour == 12
l!l&&r!l&&!r
115
$sector == 0 and $dt1->hour == 0
!ll&&!rl&&r
331
$sector == 0 and $dt1->hour == 12
!ll&&!rl&&r
321
MC/DC100%4/4
$sector == 0 and $dt1->hour == 0 or $sector == 0 and $dt1->hour == 12
$sector == 0 $dt1->hour == 0 $sector == 0 $dt1->hour == 12
Truth table57%4/7
A and B or C and D
A= $sector == 0B= $dt1->hour == 0C= $sector == 0D= $dt1->hour == 12
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
136 if ($sector==0 && $dt1->hour==0) # 0:01
137 || ($sector==0 && $dt1->hour==12); # 12:02
138 5 ▶ return $hour2
Branch: 1 branch
BranchTrueFalseTotal
if $sector == 12 and $dt1->hour == 23 or $sector == 12 and $dt1->hour == 11 2 3 5
Condition88%8/9
$sector == 12 and $dt1->hour == 23 or $sector == 12 and $dt1->hour == 11
l!l&&r!l&&!r
113
$sector == 12 and $dt1->hour == 23
!ll&&!rl&&r
311
$sector == 12 and $dt1->hour == 11
!ll&&!rl&&r
301
MC/DC50%2/4
$sector == 12 and $dt1->hour == 23 or $sector == 12 and $dt1->hour == 11
$sector == 12 $dt1->hour == 23 $sector == 12 $dt1->hour == 11
Truth table42%3/7
A and B or C and D
A= $sector == 12B= $dt1->hour == 23C= $sector == 12D= $dt1->hour == 11
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
139 if ($sector==12 && $dt1->hour==23) # 23:58
140 || ($sector==12 && $dt1->hour==11); # 11:59
141  
142 # compute fuzzy.
143 3 my $fuzzy = sprintf $hourtime[$sector], $hour1, $hour2;
144 3 return $fuzzy;
145 }
146  
147  
148 #
149 # my $fuz = _fuzzy_medium($dt)
150 #
151 # Return a fuzzy time defined by $dt. The fuzziness is medium, that
152 # is, around 3 hours in this case.
153 #
154 26 ▶sub _fuzzy_medium {
Subroutine _fuzzy_medium: called CC=1 SCAR=0.0
155 26 my ($dt) = @_;
156 26 return $daytime[$dt->hour];
157 }
158  
159  
160 #
161 # my $fuz = _fuzzy_high($dt)
162 #
163 # Return a fuzzy time defined by $dt. The fuzziness is high, that
164 # is, around the day in this case.
165 #
166 9 ▶sub _fuzzy_high {
Subroutine _fuzzy_high: called CC=1 SCAR=0.0
167 9 my ($dt) = @_;
168 9 return $weektime[$dt->dow];
169 }
170  
171  
172 4 1;
173 __END__
↑ Summary