| 1 | package MYDan::Oncall::Period; | ||
| 2 | |||
| 3 | use base qw( MYDan::Oncall MYDan::Node::Integer ); | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | |||
| 10 | our $SEP = qr/[^:~\d\w]/; | ||
| 11 | our %RGX = %MYDan::Oncall::RGX; | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use MYDan::Oncall::Period; | ||
| 16 | |||
| 17 | my $period = MYDan::Oncall::Period->new( '07:13 ~ 13:20, 23:00 ~ 02:23' ); | ||
| 18 | my ( $range, $event ) = $period->dump( $dt1, $dt2, period => 7 ); | ||
| 19 | |||
| 20 | =cut | ||
| 21 | sub new | ||
| 22 | { | ||
| 23 | my ( $class, $expr ) = splice @_; | ||
| 24 | my $self = $class->SUPER::new(); | ||
| 25 | |||
| 26 | while ( $expr && $expr =~ s/^\s*($RGX{hour}):($RGX{minute})\s* | ||
| 27 | ~\s*($RGX{hour}):($RGX{minute})$SEP*//gx ) | ||
| 28 | { | ||
| 29 | my $day = MYDan::Oncall::DAY * ( $1 > $3 || $1 == $3 && $2 > $4 ); | ||
| 30 | my $duration = $self->new()->load | ||
| 31 | ( | ||
| 32 | $1 * MYDan::Oncall::HOUR + $2 * MYDan::Oncall::MINUTE, | ||
| 33 | $3 * MYDan::Oncall::HOUR + ( $4 + 1 ) * MYDan::Oncall::MINUTE - 1 + $day | ||
| 34 | ); | ||
| 35 | $self->add( $duration ); | ||
| 36 | } | ||
| 37 | return length $expr ? croak "invalid expression -> $expr" : $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =head3 dump( $begin, $end, %param ) | ||
| 43 | |||
| 44 | Returns a range of durations and a list of events | ||
| 45 | indicated by seconds since epoch. | ||
| 46 | |||
| 47 | period: interation | ||
| 48 | day: days to select by | ||
| 49 | |||
| 50 | =cut | ||
| 51 | sub dump | ||
| 52 | { | ||
| 53 | my ( $self, $begin, $end, %param ) = splice @_; | ||
| 54 | my $period = $param{period} || 1; | ||
| 55 | my %day = map { $_ % $period => 1 } @{ $param{day} || [ 1 .. $period ] }; | ||
| 56 | my ( @duration, %event ) = $self->list( skip => 1 ); | ||
| 57 | my $range = $self->SUPER::new(); | ||
| 58 | my ( $now, $then ) = map { $_->epoch } $begin, $end; | ||
| 59 | |||
| 60 | for ( my $i = 1; $now < $then; $i ++ ) | ||
| 61 | { | ||
| 62 | next unless $day{ $i % $period }; | ||
| 63 | |||
| 64 | my $dt = $begin->clone->add( days => $i - 1 ); | ||
| 65 | $event{ $now = $dt->epoch } = 1; ## rotation point | ||
| 66 | my $today = $dt->set( map { $_ => 0 } qw( hour minute second ) )->epoch; | ||
| 67 | |||
| 68 | for my $duration ( @duration ) | ||
| 69 | { | ||
| 70 | my @duration = map { $_ + $today } @$duration; | ||
| 71 | $range->add( $range->new()->load( @duration ) ); | ||
| 72 | $duration[1] ++; @event{@duration} = 1 .. 2; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | return $range, [ keys %event ]; | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |