This file is indexed.

/usr/share/perl5/Business/Hours.pm is in libbusiness-hours-perl 0.12-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use strict;
use warnings;

package Business::Hours;

require 5.006;
use Set::IntSpan;
use Time::Local qw/timelocal_nocheck/;

our $VERSION = '0.12';

=head1 NAME

Business::Hours - Calculate business hours in a time period

=head1 SYNOPSIS

  use Business::Hours;
  my $hours = Business::Hours->new();    

  # Get a Set::IntSpan of all the business hours in the next week.
  # use the default business hours of 9am to 6pm localtime.
  $hours->for_timespan( Start => time(), End => time()+(86400*7) );

=head1 DESCRIPTION

This module is a simple tool for calculating business hours in a time period. 
Over time, additional functionality will be added to make it easy to
calculate the number of business hours between arbitrary dates. 

=head1 USAGE

=cut

# Default business hours are weekdays from 9am to 6pm
our $BUSINESS_HOURS = (
    {   0 => {
            Name  => 'Sunday',
            Start => undef,
            End   => undef,
        },
        1 => {
            Name  => 'Monday',
            Start => '9:00',
            End   => '18:00',
        },
        2 => {
            Name  => 'Tuesday',
            Start => '9:00',
            End   => '18:00',
        },
        3 => {
            Name  => 'Wednesday',
            Start => '9:00',
            End   => '18:00',
        },
        4 => {
            Name  => 'Thursday',
            Start => '9:00',
            End   => '18:00',
        },
        5 => {
            Name  => 'Friday',
            Start => '9:00',
            End   => '18:00',
        },
        6 => {
            Name  => 'Saturday',
            Start => undef,
            End   => undef,
        }
    }
);
__PACKAGE__->preprocess_business_hours( $BUSINESS_HOURS );

=head2 new

Creates a new L<Business::Hours> object.  Takes no arguments.

=cut

sub new {
    my $class = shift;

    my $self = bless( {}, ref($class) || $class );

    return ($self);
}

=head2 business_hours HASH

Gets / sets the business hours for this object.
Takes a hash (NOT a hash reference) of the form:

    my %hours = (
        0 => { Name     => 'Sunday',
               Start    => 'HH:MM',
               End      => 'HH:MM' },

        1 => { Name     => 'Monday',
               Start    => 'HH:MM',
               End      => 'HH:MM' },
        ....

        6 => { Name     => 'Saturday',
               Start    => 'HH:MM',
               End      => 'HH:MM' },
    );

Start and End times are of the form HH:MM.  Valid times are
from 00:00 to 23:59.  If your hours are from 9am to 6pm, use
Start => '9:00', End => '18:00'.  A given day MUST have a start
and end time OR may declare both Start and End to be undef, if
there are no valid hours on that day.

You can use the array Breaks to mark interruptions between Start/End (for instance lunch hour). It's an array of periods, each with a Start and End time:

    my %hours = (
        0 => { Name     => 'Sunday',
               Start    => 'HH:MM',
               End      => 'HH:MM',
               Breaks  => [ 
                             { Start    => 'HH:MM',
                             End      => 'HH:MM' },
                             { Start    => 'HH:MM',
                             End      => 'HH:MM' },
                           ],

        1 => { Name     => 'Monday',
               Start    => 'HH:MM',
               End      => 'HH:MM' },
        ....

        6 => { Name     => 'Saturday',
               Start    => 'HH:MM',
               End      => 'HH:MM' },
    );

Note that the ending time is really "what is the first minute we're closed.
If you specify an "End" of 18:00, that means that at 6pm, you are closed.
The last business second was 17:59:59.

As well, you can pass information about holidays using key 'holidays' and
an array reference value, for example:

    $hours->business_hours(
        0 => { Name     => 'Sunday',
               Start    => 'HH:MM',
               End      => 'HH:MM' },
        ....
        6 => { Name     => 'Saturday',
               Start    => 'HH:MM',
               End      => 'HH:MM' },

        holidays => [qw(01-01 12-25 2009-05-08)],
    );

Read more about holidays specification below in L<holidays|/"holidays ARRAY">.

=cut

sub business_hours {
    my $self = shift;
    if ( @_ ) {
        %{ $self->{'business_hours'} } = (@_);
        $self->{'holidays'} = delete $self->{'business_hours'}{'holidays'};
        $self->preprocess_business_hours( $self->{'business_hours'} );
    }
    return %{ $self->{'business_hours'} };
}

=head2 preprocess_business_hours

Checks and transforms business hours data. No need to call it.

=cut

sub preprocess_business_hours {
    my $self = shift;
    my $bizdays = shift;

    my $process_start_end = sub {
        my $span = shift;
        foreach my $which (qw(Start End)) {
            return 0 unless $span->{ $which } && $span->{ $which } =~ /^(\d+)\D(\d+)$/;

            $span->{ $which . 'Hour' }   = $1;
            $span->{ $which . 'Minute' } = $2;
        }
        $span->{'EndHour'} += 24
            if $span->{'EndHour'}*60+$span->{'EndMinute'}
            <= $span->{'StartHour'}*60+$span->{'StartMinute'};
        return 1;
    };

    # Split the Start and End times into hour/minute specifications
    foreach my $dow ( keys %$bizdays ) {
        unless (
            $bizdays->{ $dow } && ref($bizdays->{ $dow }) eq 'HASH'
            && $process_start_end->( $bizdays->{ $dow } )
        ) {
            delete $bizdays->{ $dow };
            next;
        }

        foreach my $break ( splice @{ $bizdays->{ $dow }{'Breaks'} || [] } ) {
            next unless $break && ref($break) eq 'HASH';
            push @{ $bizdays->{ $dow }{'Breaks'} }, $break
                if $process_start_end->( $break );
        }
    }
}

=head2 holidays ARRAY

Gets / sets holidays for this object. Takes an array
where each element is ether 'MM-DD' or 'YYYY-MM-DD'.

Specification with year defined may be required when a holiday
matches Sunday or Saturday. In many countries days are shifted
in such case.

Holidays can be set via L<business_hours|/"business_hours HASH"> method
as well, so you can use this feature without changing your code.

=cut

sub holidays {
    my $self = shift;
    if ( @_ ) {
        @{ $self->{'holidays'} } = (@_);
    }
    return @{ $self->{'holidays'} || [] };
}

=head2 for_timespan HASH

Takes a hash with the following parameters:

=over

=item Start

The start of the period in question in seconds since the epoch

=item End

The end of the period in question in seconds since the epoch

=back

Returns a L<Set::IntSpan> of business hours for this period of time.

=cut

sub for_timespan {
    my $self = shift;
    my %args = (
        Start => undef,
        End   => undef,
        @_
    );
    my $bizdays = $self->{'business_hours'} || $BUSINESS_HOURS;

    # now that we know what the business hours are for each day in a week,
    # we need to find all the business hours in the period in question.

    # Create an intspan of the period in total.
    my $business_period
        = Set::IntSpan->new( $args{'Start'} . "-" . $args{'End'} );

    # jump back to the first day (Sunday) of the last week before the period
    # began.
    my @start        = localtime( $args{'Start'} );
    my $month        = $start[4];
    my $year         = $start[5];
    my $first_sunday = $start[3] - $start[6];

    # period_start is time_t at midnight local time on the first sunday
    my $period_start
        = timelocal_nocheck( 0, 0, 0, $first_sunday, $month, $year );

    # for each week until the end of the week in seconds since the epoch
    # is outside the business period in question
    my $week_start = $period_start;

    # @run_list is a run list of the period's business hours
    # its form is (<int>-<int2>,<int3>-<int4>)
    # For documentation about its format, have a look at Set::IntSpan.
    # (This is fed into Set::IntSpan to use to compute our actual run.
    my @run_list;

    # @break_list is a run list of the period's breaks between business hours
    # its form is (<int>-<int2>,<int3>-<int4>)
    # For documentation about its format, have a look at Set::IntSpan.
    # (This is fed into Set::IntSpan to use to compute our actual run.
    my @break_list;

    my $convert_start_end = sub {
        my ($hours, @today) = @_;

        # add the business seconds in that week to the runlist we'll use to
        # figure out business hours
        # (Be careful to use timelocal to convert times in the week into actual
        # seconds, so we don't lose at DST transition)
        my $start = timelocal_nocheck(
            0, $hours->{'StartMinute'}, $hours->{'StartHour'}, @today
        );

        # We subtract 1 from the ending time, because the ending time
        # really specifies what hour we end up closed at
        my $end = timelocal_nocheck(
            0, $hours->{'EndMinute'}, $hours->{'EndHour'}, @today
        ) - 1;

        return "$start-$end";
    };

    while ( $week_start <= $args{'End'} ) {

        my @today = (localtime($week_start))[3, 4, 5];
        $today[0]--; # compensate next increment

        # foreach day in the week, find that day's business hours in
        # seconds since the epoch.
        for ( my $dow = 0; $dow <= 6; $dow++ ) {
            $today[0]++; # next day comes
            next unless my $day_hours = $bizdays->{$dow};

            push @run_list, $convert_start_end->( $day_hours, @today );

            foreach my $break ( @{ $bizdays->{$dow}{'Breaks'} || [] } ) {
                push @break_list, $convert_start_end->( $break, @today );
            }
        }

    # now that we're done with this week, calculate the start of the next week
    # the next week starts at midnight on the sunday following the previous
    # sunday
        $week_start = timelocal_nocheck( 0, 0, 0, $today[0]+1, $today[1], $today[2] );

    }

    my $business_hours = Set::IntSpan->new( join( ',', @run_list ) ) - Set::IntSpan->new( join( ',', @break_list ) );
    my $business_hours_in_period
        = $business_hours->intersect($business_period);

    # find the intersection of the business period intspan and the  business
    # hours intspan. (Because we want to trim any business hours that fall
    # outside the business period)

    if ( my @holidays = $self->holidays ) {
        my $start_year = $year;
        my $end_year = (localtime $args{'End'})[5];
        foreach my $holiday (@holidays) {
            my ($year, $month, $date) = ($holiday =~ /^(?:(\d\d\d\d)\D)?(\d\d)\D(\d\d)$/);
            $month--;
            my @range;
            if ( $year ) {
                push @range, [
                    timelocal_nocheck( 0, 0, 0, $date, $month, $year ),
                ];
            }
            else {
                push @range, [
                    timelocal_nocheck( 0, 0, 0, $date, $month, $start_year ),
                ];
                push @range, [
                    timelocal_nocheck( 0, 0, 0, $date, $month, $end_year ),
                ] if $start_year != $end_year;
            }
            $_->[1] = $_->[0] + 24*60*60 foreach @range;
            $business_hours_in_period -= \@range;
        }
    }

    # TODO: Add any special times to the business hours

    # cache the calculated business hours in the object
    $self->{'calculated'} = $business_hours_in_period;
    $self->{'start'}      = $args{'Start'};
    $self->{'end'}        = $args{'End'};

    # Return the intspan of business hours.

    return ($business_hours_in_period);

}

=head2 between START, END

Returns the number of business seconds between START and END
Both START and END should be specified in seconds since the epoch.

Returns -1 if START or END are outside the calculated business hours.

=cut

sub between {
    my $self  = shift;
    my $start = shift;
    my $end   = shift;

    if ( not defined $self->{'start'} or not defined $self->{'end'} ) {
        # We haven't calculated our sets yet, so let's do that for the
        # user now, assuming they want to use the same start and end
        # times
        $self->for_timespan( Start => $start, End => $end );
    }

    if ( $start < $self->{'start'} ) {
        return (-1);
    }
    if ( $end > $self->{'end'} ) {
        return (-1);
    }

    my $period       = Set::IntSpan->new( $start . "-" . $end );
    my $intersection = intersect $period $self->{'calculated'};

    return cardinality $intersection;
}

=head2 first_after START

Returns START if START is within business hours.
Otherwise, returns the next business second after START.
START should be specified in seconds since the epoch.

Returns -1 if it can't find any business hours within thirty days.

=cut

sub first_after {
    my $self  = shift;
    my $start = shift;

    # the maximum time after which we stop searching for business hours
    my $MAXTIME = $start + ( 30 * 24 * 60 * 60 );    # 30 days

    my $period = ( 24 * 60 * 60 );
    my $end    = $start + $period;
    my $hours  = new Set::IntSpan;

    while ( $hours->empty ) {
        if ( $end >= $MAXTIME ) {
            return -1;
        }
        $hours = $self->for_timespan( Start => $start, End => $end );
        $start = $end;
        $end   = $start + $period;
    }

    return $hours->first;
}

=head2 add_seconds START, SECONDS

Returns a time SECONDS business seconds after START.
START should be specified in seconds since the epoch.

Returns -1 if it can't find any business hours within thirty days.

=cut

sub add_seconds {
    my $self    = shift;
    my $start   = shift;
    my $seconds = shift;

    # the maximum time after which we stop searching for business hours
    my $MAXTIME = ( 30 * 24 * 60 * 60 );    # 30 days

    my $last;

    my $period = ( 24 * 60 * 60 );
    my $end    = $start + $period;

    my $hours = new Set::IntSpan;
    while ($hours->empty
        or $self->between( $start, $hours->last ) <= $seconds )
    {
        if ( $end >= $start + $MAXTIME ) {
            return -1;
        }
        $hours = $self->for_timespan( Start => $start, End => $end );

        $end += $period;
    }

    my @elements = elements $hours;
    $last = $elements[$seconds];

    return $last;
}

=head1 BUGS

Yes, most likely.  Please report them to L<bug-business-hours@rt.cpan.org>.

=head1 AUTHOR

Jesse Vincent, L<jesse@cpan.org>

=head1 COPYRIGHT

Copyright 2003-2008 Best Practical Solutions, LLC.

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE
file included with this module.

=cut

1;