You are here

function _elysia_cron_next_run in Elysia Cron 6

Same name and namespace in other branches
  1. 5.2 elysia_cron_scheduler.inc \_elysia_cron_next_run()
  2. 5 elysia_cron_scheduler.inc \_elysia_cron_next_run()
  3. 6.2 elysia_cron_scheduler.inc \_elysia_cron_next_run()
  4. 7.2 elysia_cron_scheduler.inc \_elysia_cron_next_run()
  5. 7 elysia_cron_scheduler.inc \_elysia_cron_next_run()
1 call to _elysia_cron_next_run()
elysia_cron_should_run in ./elysia_cron_scheduler.inc

File

./elysia_cron_scheduler.inc, line 18

Code

function _elysia_cron_next_run($conf) {
  $ranges = array(
    array(
      0,
      59,
    ),
    array(
      0,
      23,
    ),
    array(
      1,
      31,
    ),
    // TODO
    array(
      1,
      12,
    ),
    array(
      0,
      3000,
    ),
    array(
      0,
      6,
    ),
  );
  if (!preg_match('/^([0-9*,\\/-]+)[ ]+([0-9*,\\/-]+)[ ]+([0-9*,\\/-]+)[ ]+([0-9*,\\/-]+)[ ]+([0-9*,\\/-]+)$/', $conf['rule'], $rules)) {
    _dco_watchdog('cron', 'Invalid rule found: %rule', array(
      '%rule' => $conf['rule'],
    ));
    return false;
  }
  $rule = array(
    $rules[1],
    $rules[2],
    array(
      $rules[3],
      $rules[5],
    ),
    $rules[4],
  );
  $ruledec = array();
  $date = __cronDecodeDate($conf['last_run'], 1);
  $expected_date = __cronDecodeDate(!empty($conf['last_run_expected']) ? $conf['last_run_expected'] : 0);
  for ($i = 0; $i < 4; $i++) {
    if ($i != 2) {

      // Standard scheme for mins, hours, month
      $ruledec[$i] = __cronDecodeRule($rule[$i], $ranges[$i][0], $ranges[$i][1]);
    }
    else {

      // For mday+week we follow another scheme
      $ruledec[$i] = __cronDecodeRuleMday($rule[2], $date[3], $date[4]);
    }
    $r = $ruledec[$i];
    $new = $date[$i];
    if ($r['d']) {
      if ($expected_date[$i] > $date[$i]) {
        $expected_date[$i] -= $ranges[$i][1] + 1;
      }
      $new = $expected_date[$i] + ceil(($date[$i] - $expected_date[$i]) / $r['d']) * $r['d'];
    }
    elseif ($r['n']) {
      $new = __cronNextOrEqual($date[$i], $r['n'], $ranges[$i][0], $ranges[$i][1]);
    }
    if ($new != $date[$i]) {
      $date[$i] = $new;
      if ($date[$i] > $ranges[$i][1]) {
        $date[$i + 1]++;
        $date[$i] = $ranges[$i][0] + $date[$i] - $ranges[$i][1] - 1;
      }
      for ($j = 0; $j < $i; $j++) {
        if ($j == 2) {

          // For mday+week decoded rule could be changed (by month+year)
          $ruledec[$j] = __cronDecodeRuleMday($rule[2], $date[3], $date[4]);
        }
        $date[$j] = $ruledec[$j]['d'] ? $ranges[$j][0] == 0 ? 0 : $ruledec[$j]['d'] : ($ruledec[$j]['n'] ? reset($ruledec[$j]['n']) : $ranges[$j][0]);
        $expected_date[$j] = 0;
      }
    }
  }
  return __cronEncodeDate($date);
}