File Coverage

blib/lib/Data/OpeningHours/Calendar.pm
Criterion Covered Total %
statement 49 52 94.2
branch 9 12 75.0
condition n/a
subroutine 13 13 100.0
pod 0 9 0.0
total 71 86 82.5


line stmt bran cond sub pod time code
1             package Data::OpeningHours::Calendar;
2 2     2   1424 use strict;
  2         4  
  2         184  
3 2     2   12 use warnings;
  2         5  
  2         50  
4              
5 2     2   8469 use Data::OpeningHours::Hours;
  2         5  
  2         62  
6 2     2   3669 use DateTime::Format::Strptime;
  2         638100  
  2         1297  
7              
8             sub new {
9 2     2 0 33 my ($class) = @_;
10 2         6 my $self = {};
11 2         27 $self->{parser} = DateTime::Format::Strptime->new(
12             pattern => '%a',
13             locale => 'en_US',
14             on_error => 'croak',
15             );
16 2         903 return bless $self, $class;
17             }
18              
19             sub set_week_day {
20 14     14 0 1610 my ($self, $day, $hours) = @_;
21 14         43 $self->{week}[$day] = Data::OpeningHours::Hours->new($hours);
22 14         22 return;
23             }
24              
25             sub set_special_day {
26 6     6 0 19 my ($self, $date, $hours) = @_;
27 6         19 $self->{days}{$date} = Data::OpeningHours::Hours->new($hours);
28 6         15 return;
29             }
30             sub is_special_day {
31 9     9 0 143 my ($self, $date) = @_;
32 9         47 return exists $self->{days}{$date};
33             }
34              
35             sub is_open_on_week_day {
36 10     10 0 52 my ($self, $day_of_week, $hour) = @_;
37 10 50       27 return unless exists $self->{week}[$day_of_week];
38 10         34 return $self->{week}[$day_of_week]->is_open_between($hour);
39             }
40              
41             sub is_open_on_special_day {
42 5     5 0 44 my ($self, $date, $hour) = @_;
43 5         17 return $self->{days}{$date}->is_open_between($hour);
44             }
45              
46             sub is_open {
47 4     4 0 1244 my ($self, $date) = @_;
48 4 100       15 if ($self->is_special_day($date->ymd('-'))){
49 2         6 return $self->is_open_on_special_day(
50             $date->ymd('-'),
51             sprintf('%02d:%02d', $date->hour, $date->minute));
52             }
53              
54 2         10 return $self->is_open_on_week_day(
55             $date->wday,
56             sprintf('%02d:%02d', $date->hour, $date->minute));
57             }
58              
59             sub first_open_hour {
60 5     5 0 9 my ($self, $date) = @_;
61 5 100       13 if ($self->is_special_day($date->ymd('-'))) {
62 1         5 my $hours = $self->{days}{$date->ymd('-')};
63 1 50       23 return $hours->first_hour() if $hours;
64 0         0 return;
65             }
66 4         14 my $hours = $self->{week}[$date->wday];
67 4 50       40 return $hours->first_hour() if $hours;
68 0         0 return;
69             }
70              
71             sub next_open {
72 3     3 0 4058 my ($self, $date) = @_;
73 3         11 my $open = $date->clone;
74 3         31 while (1) {
75 5         16 $open->add(days => 1);
76 5         2817 my $hour = $self->first_open_hour($open);
77 5 100       13 if ($hour) {
78 3         13 my ($h,$m) = split /:/, $hour;
79 3         13 $open->set_hour($h);
80 3         790 $open->set_minute($m);
81 3         724 return $open;
82             }
83             }
84 0           return;
85             }
86              
87             1;
88