您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

72 行
2.2 KiB

  1. <?php
  2. namespace Cron;
  3. use DateTime;
  4. use DateTimeZone;
  5. /**
  6. * Hours field. Allows: * , / -
  7. */
  8. class HoursField extends AbstractField
  9. {
  10. public function isSatisfiedBy(DateTime $date, $value)
  11. {
  12. return $this->isSatisfied($date->format('H'), $value);
  13. }
  14. public function increment(DateTime $date, $invert = false, $parts = null)
  15. {
  16. // Change timezone to UTC temporarily. This will
  17. // allow us to go back or forwards and hour even
  18. // if DST will be changed between the hours.
  19. if (is_null($parts) || $parts == '*') {
  20. $timezone = $date->getTimezone();
  21. $date->setTimezone(new DateTimeZone('UTC'));
  22. if ($invert) {
  23. $date->modify('-1 hour');
  24. } else {
  25. $date->modify('+1 hour');
  26. }
  27. $date->setTimezone($timezone);
  28. $date->setTime($date->format('H'), $invert ? 59 : 0);
  29. return $this;
  30. }
  31. $parts = strpos($parts, ',') !== false ? explode(',', $parts) : array($parts);
  32. $hours = array();
  33. foreach ($parts as $part) {
  34. $hours = array_merge($hours, $this->getRangeForExpression($part, 23));
  35. }
  36. $current_hour = $date->format('H');
  37. $position = $invert ? count($hours) - 1 : 0;
  38. if (count($hours) > 1) {
  39. for ($i = 0; $i < count($hours) - 1; $i++) {
  40. if ((!$invert && $current_hour >= $hours[$i] && $current_hour < $hours[$i + 1]) ||
  41. ($invert && $current_hour > $hours[$i] && $current_hour <= $hours[$i + 1])) {
  42. $position = $invert ? $i : $i + 1;
  43. break;
  44. }
  45. }
  46. }
  47. $hour = $hours[$position];
  48. if ((!$invert && $date->format('H') >= $hour) || ($invert && $date->format('H') <= $hour)) {
  49. $date->modify(($invert ? '-' : '+') . '1 day');
  50. $date->setTime($invert ? 23 : 0, $invert ? 59 : 0);
  51. }
  52. else {
  53. $date->setTime($hour, $invert ? 59 : 0);
  54. }
  55. return $this;
  56. }
  57. public function validate($value)
  58. {
  59. return (bool) preg_match('/^[\*,\/\-0-9]+$/', $value);
  60. }
  61. }