DateTime-Event-Cron-Quartz

 view release on metacpan or  search on metacpan

lib/DateTime/Event/Cron/Quartz.pm  view on Meta::CPAN

    my $this = shift;

    my ( $s, $i ) = @_;

    my $end_of_val = $this->find_next_white_space( $i, $s );
    my $val = substr( $s, $i, $end_of_val );

    if ( !( $val =~ /^\d+$/ ) ) {
        ParseException->throw(
            error => "value is not numeric: " . $val,
            line  => $i
        );
    }

    return int($val);
}

sub get_month_number {
    my $this = shift;

    my $s = shift;

    my $integer = $MONTH_MAP->{$s};

    if ( !defined $integer ) {
        return -1;
    }

    return $integer;
}

sub get_day_of_week_number {
    my $this = shift;

    my $s = shift;

    my $integer = $DAY_MAP->{$s};

    if ( !defined $integer ) {
        return -1;
    }

    return $integer;
}

#//////////////////////////////////////////////////////////////////////////
#
# Computation Functions
#
#//////////////////////////////////////////////////////////////////////////

sub get_time_after {
    my $this = shift;

    my $after_time = shift->clone;

    # move ahead one second, since we're computing the time *after* the
    # given time
    $after_time->add( seconds => 1 );

    # operable calendar
    my $cl = $after_time->clone;

    my $got_one = 0;

    # loop until we've computed the next time, or we've past the endTime
    ITER: while ( !$got_one ) {

        #if (endTime != null && cl.getTime().after(endTime)) return null;
        if ( ( $cl->year ) > 2999 ) {    # prevent endless loop...
            return undef;
        }


        # get second.................................................
        {
            # sorted set
            # SortedSet
            my $st = undef;
            my $t  = 0;
    
            my $sec = $cl->second;
            my $min = $cl->minute;
    
            $st = $this->seconds->tail_set($sec);
    
            if ( defined $st && $st->size() != 0 ) {
                $sec = int( $st->first_item() );
            }
            else {
                $sec = int( $this->seconds->first_item() );
                $cl->add(minutes => 1);
            }
            $cl->set( second => $sec );
        }

        # get minute.................................................
        {
            my $min = $cl->minute;
            my $hr = $cl->hour;
            my $t = -1;

            my $st = $this->minutes->tail_set($min);
            if ( defined $st && $st->size() != 0 ) {
                $t   = $min;
                $min = int( $st->first_item );
            }
            else {
                # next hour
                $min = int( $this->minutes->first_item() );
                $hr++;
            }

            if ( $min != $t ) {
                $cl->set( second => 0, minute => $min );
                $this->set_calendar_hour( $cl, $hr );
                next ITER;
            }

            $cl->set( minute => $min );
        }

        # get hour...................................................
        {
            my $hr = $cl->hour;
            my $day = $cl->day;
            my $t = -1;
    
            my $st = $this->hours->tail_set( int($hr) );
            if ( defined $st && $st->size() != 0 ) {
                $t  = $hr;
                $hr = int( $st->first_item() );
            }
            else {
                $hr = int( $this->hours->first_item() );
                $day++;
            }

            if ( $hr != $t ) {

                $cl->add( days => $day - $cl->day );
                $cl->set( second => 0, minute => 0 );
    
                $this->set_calendar_hour( $cl, $hr );
                next ITER;
            }
    
            $cl->set( hour => $hr );
        }

        # get day...................................................
        {
            my $day = $cl->day;
            my $mon = $cl->month;
            my $t = -1;
            my $tmon = $mon;

            my $day_of_m_spec = !$this->days_of_month->contains($NO_SPEC);
            my $day_of_w_spec = !$this->days_of_week->contains($NO_SPEC);

            my $min = $cl->min;
            my $sec = $cl->sec;
            my $hr = $cl->hour;

            if ( $day_of_m_spec && !$day_of_w_spec )
            {
                # get day by day of month rule
                my $st = $this->days_of_month->tail_set( int($day) );
                if ( $this->lastday_of_month ) {
                    if ( !$this->nearest_weekday ) {
                        $t = $day;
                        $day = $this->getlastday_of_month( $mon, $cl->year );
                    }
                    else {
                        $t = $day;
                        $day = $this->getlastday_of_month( $mon, $cl->year );
    
                        my $tcal = DateTime->new(
                            second => 0,
                            minute => 0,
                            hour   => 0,
                            day    => $day,
                            month  => $mon,
                            year   => $cl->year
                        );
    
                        my $ldom = $this->getlastday_of_month( $mon, $cl->year );
                        my $dow = $tcal->day_of_week_0;
    
                        if ( $dow == $SATURDAY && $day == 1 ) {
                            $day += 2;
                        }
                        elsif ( $dow == $SATURDAY ) {
                            $day -= 1;
                        }
                        elsif ( $dow == $SUNDAY && $day == $ldom ) {
                            $day -= 2;
                        }
                        elsif ( $dow == $SUNDAY ) {
                            $day += 1;
                        }
    
                        $tcal->set(
                            second => $sec,

lib/DateTime/Event/Cron/Quartz.pm  view on Meta::CPAN

    
            my $st = $this->months->tail_set( int($mon) );
            if ( defined $st && $st->size() != 0 ) {
                $t   = $mon;
                $mon = ( int $st->first_item() );
            }
            else {
                $mon = ( int $this->months->first_item() );
                $year++;
            }
            if ( $mon != $t ) {
                $cl->set(
                    second => 0,
                    minute => 0,
                    hour   => 0,
                    day    => 1,
                    month  => $mon
                );
    
                $cl->set( year => $year );
                next ITER;
            }
            $cl->set( month => $mon );
        }

        # get year...................................................
        {
            my $year = $cl->year;
            my $t = -1;

            my $st = $this->years->tail_set( int($year) );
            if ( defined $st && $st->size() != 0 ) {
                $t    = $year;
                $year = int( $st->first_item() );
            }
            else {
                return undef;    # ran out of years...
            }
    
            if ( $year != $t ) {
                $cl->set(
                    second => 0,
                    minute => 0,
                    hour   => 0,
                    day    => 1,
                    month  => 1
                );
                $cl->set( year => $year );
                next ITER;
            }
    
            $cl->set( year => $year );
        }

        $got_one = 1;
    }    # while( !done )

    return $cl;
}

#* Advance the calendar to the particular hour paying particular attention
#* to daylight saving problems.
#*
#* @param cal
#* @param hour

sub set_calendar_hour {
    my $this = shift;

    my ( $cal, $hour ) = @_;

    my $delta = 0;

    if ( $hour == 24 ) {
        $delta = 1;
        $hour--;
    }

    $cal->set( hour => $hour );

    if ( $delta > 0 ) {
        $cal->add( hours => $delta );
    }
}

#=pod
# * NOT YET IMPLEMENTED: Returns the time before the given time
# * that the <code>CronExpression</code> matches.
#=cut

sub get_time_before {
    my $this = shift;

    my $end_time = shift;

    # TODO: implement QUARTZ-423
    return;
}

#=pod
# * NOT YET IMPLEMENTED: Returns the final time that the
# * <code>CronExpression</code> will match.
#=cut

sub get_final_fire_time {

    # TODO: implement QUARTZ-423
    return;
}

sub is_leap_year {
    my $this = shift;

    return DateTime->new( year => shift )->is_leap_year;
}

sub getlastday_of_month {
    my $this = shift;

    my ( $month_num, $year ) = @_;

    return DateTime->last_day_of_month( month => $month_num, year => $year )
      ->day;
}

1;



( run in 1.954 second using v1.01-cache-2.11-cpan-39bf76dae61 )