code_junkie

Calculate when a cron job will be executed then next time

Nov 12th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. 1 * * * * (every hour at xx:01)
  2. 2 5 * * * (every day at 05:02)
  3. 0 4 3 * * (every third day of the month at 04:00)
  4. * 2 * * 5 (every minute between 02:00 and 02:59 on fridays)
  5.  
  6. //Totaly made up language
  7. next = getTimeNow();
  8. next.addMinutes(1) //so that next is never now
  9. done = false;
  10. while (!done) {
  11. if (cron.minute != '*' && next.minute != cron.minute) {
  12. if (next.minute > cron.minute) {
  13. next.addHours(1);
  14. }
  15. next.minute = cron.minute;
  16. }
  17. if (cron.hour != '*' && next.hour != cron.hour) {
  18. if (next.hour > cron.hour) {
  19. next.hour = cron.hour;
  20. next.addDays(1);
  21. next.minute = 0;
  22. continue;
  23. }
  24. next.hour = cron.hour;
  25. next.minute = 0;
  26. continue;
  27. }
  28. if (cron.weekday != '*' && next.weekday != cron.weekday) {
  29. deltaDays = cron.weekday - next.weekday //assume weekday is 0=sun, 1 ... 6=sat
  30. if (deltaDays < 0) { deltaDays+=7; }
  31. next.addDays(deltaDays);
  32. next.hour = 0;
  33. next.minute = 0;
  34. continue;
  35. }
  36. if (cron.day != '*' && next.day != cron.day) {
  37. if (next.day > cron.day || !next.month.hasDay(cron.day)) {
  38. next.addMonths(1);
  39. next.day = 1; //assume days 1..31
  40. next.hour = 0;
  41. next.minute = 0;
  42. continue;
  43. }
  44. next.day = cron.day
  45. next.hour = 0;
  46. next.minute = 0;
  47. continue;
  48. }
  49. if (cron.month != '*' && next.month != cron.month) {
  50. if (next.month > cron.month) {
  51. next.addMonths(12-next.month+cron.month)
  52. next.day = 1; //assume days 1..31
  53. next.hour = 0;
  54. next.minute = 0;
  55. continue;
  56. }
  57. next.month = cron.month;
  58. next.day = 1;
  59. next.hour = 0;
  60. next.minute = 0;
  61. continue;
  62. }
  63. done = true;
  64. }
  65.  
  66. //Shorter more loopy version
  67. next = getTimeNow().addMinutes(1);
  68. while (true) {
  69. if (cron.month != '*' && next.month != cron.month) {
  70. next.addMonths(1);
  71. next.day = 1;
  72. next.hour = 0;
  73. next.minute = 0;
  74. continue;
  75. }
  76. if (cron.day != '*' && next.day != cron.day) {
  77. next.addDays(1);
  78. next.hour = 0;
  79. next.minute = 0;
  80. continue;
  81. }
  82. if (cron.weekday != '*' && next.weekday != cron.weekday) {
  83. next.addDays(1);
  84. next.hour = 0;
  85. next.minute = 0;
  86. continue;
  87. }
  88. if (cron.hour != '*' && next.hour != cron.hour) {
  89. next.addHours(1);
  90. next.minute = 0;
  91. continue;
  92. }
  93. if (cron.minute != '*' && next.minute != cron.minute) {
  94. next.addMinutes(1);
  95. continue;
  96. }
  97. break;
  98. }
  99.  
  100. <?php
  101.  
  102. // Works with predefined scheduling definitions
  103. $cron = CronCronExpression::factory('@daily');
  104. $cron->isDue();
  105. $cron->getNextRunDate();
  106. $cron->getPreviousRunDate();
  107.  
  108. // Works with complex expressions
  109. $cron = CronCronExpression::factory('15 2,6-12 */15 1 2-5');
  110. $cron->getNextRunDate();
  111.  
  112. class myMiniDate {
  113. var $myTimestamp;
  114. static private $dateComponent = array(
  115. 'second' => 's',
  116. 'minute' => 'i',
  117. 'hour' => 'G',
  118. 'day' => 'j',
  119. 'month' => 'n',
  120. 'year' => 'Y',
  121. 'dow' => 'w',
  122. 'timestamp' => 'U'
  123. );
  124. static private $weekday = array(
  125. 1 => 'monday',
  126. 2 => 'tuesday',
  127. 3 => 'wednesday',
  128. 4 => 'thursday',
  129. 5 => 'friday',
  130. 6 => 'saturday',
  131. 0 => 'sunday'
  132. );
  133.  
  134. function __construct($ts = NULL) { $this->myTimestamp = is_null($ts)?time():$ts; }
  135.  
  136. function __set($var, $value) {
  137. list($c['second'], $c['minute'], $c['hour'], $c['day'], $c['month'], $c['year'], $c['dow']) = explode(' ', date('s i G j n Y w', $this->myTimestamp));
  138. switch ($var) {
  139. case 'dow':
  140. $this->myTimestamp = strtotime(self::$weekday[$value], $this->myTimestamp);
  141. break;
  142.  
  143. case 'timestamp':
  144. $this->myTimestamp = $value;
  145. break;
  146.  
  147. default:
  148. $c[$var] = $value;
  149. $this->myTimestamp = mktime($c['hour'], $c['minute'], $c['second'], $c['month'], $c['day'], $c['year']);
  150. }
  151. }
  152.  
  153.  
  154. function __get($var) {
  155. return date(self::$dateComponent[$var], $this->myTimestamp);
  156. }
  157.  
  158. function modify($how) { return $this->myTimestamp = strtotime($how, $this->myTimestamp); }
  159. }
  160.  
  161.  
  162. $cron = new myMiniDate(time() + 60);
  163. $cron->second = 0;
  164. $done = 0;
  165.  
  166. echo date('Y-m-d H:i:s') . '<hr>' . date('Y-m-d H:i:s', $cron->timestamp) . '<hr>';
  167.  
  168. $Job = array(
  169. 'Minute' => 5,
  170. 'Hour' => 3,
  171. 'Day' => 13,
  172. 'Month' => null,
  173. 'DOW' => 5,
  174. );
  175.  
  176. while ($done < 100) {
  177. if (!is_null($Job['Minute']) && ($cron->minute != $Job['Minute'])) {
  178. if ($cron->minute > $Job['Minute']) {
  179. $cron->modify('+1 hour');
  180. }
  181. $cron->minute = $Job['Minute'];
  182. }
  183. if (!is_null($Job['Hour']) && ($cron->hour != $Job['Hour'])) {
  184. if ($cron->hour > $Job['Hour']) {
  185. $cron->modify('+1 day');
  186. }
  187. $cron->hour = $Job['Hour'];
  188. $cron->minute = 0;
  189. }
  190. if (!is_null($Job['DOW']) && ($cron->dow != $Job['DOW'])) {
  191. $cron->dow = $Job['DOW'];
  192. $cron->hour = 0;
  193. $cron->minute = 0;
  194. }
  195. if (!is_null($Job['Day']) && ($cron->day != $Job['Day'])) {
  196. if ($cron->day > $Job['Day']) {
  197. $cron->modify('+1 month');
  198. }
  199. $cron->day = $Job['Day'];
  200. $cron->hour = 0;
  201. $cron->minute = 0;
  202. }
  203. if (!is_null($Job['Month']) && ($cron->month != $Job['Month'])) {
  204. if ($cron->month > $Job['Month']) {
  205. $cron->modify('+1 year');
  206. }
  207. $cron->month = $Job['Month'];
  208. $cron->day = 1;
  209. $cron->hour = 0;
  210. $cron->minute = 0;
  211. }
  212.  
  213. $done = (is_null($Job['Minute']) || $Job['Minute'] == $cron->minute) &&
  214. (is_null($Job['Hour']) || $Job['Hour'] == $cron->hour) &&
  215. (is_null($Job['Day']) || $Job['Day'] == $cron->day) &&
  216. (is_null($Job['Month']) || $Job['Month'] == $cron->month) &&
  217. (is_null($Job['DOW']) || $Job['DOW'] == $cron->dow)?100:($done+1);
  218. }
  219.  
  220. echo date('Y-m-d H:i:s', $cron->timestamp) . '<hr>';
  221.  
  222. function parse_crontab($time, $crontab)
  223. {$time=explode(' ', date('i G j n w', strtotime($time)));
  224. $crontab=explode(' ', $crontab);
  225. foreach ($crontab as $k=>&$v)
  226. {$v=explode(',', $v);
  227. foreach ($v as &$v1)
  228. {$v1=preg_replace(array('/^*$/', '/^d+$/', '/^(d+)-(d+)$/', '/^*/(d+)$/'),
  229. array('true', $time[$k].'===', '(1<='.$time[$k].' and '.$time[$k].'<=2)', $time[$k].'%1===0'),
  230. $v1
  231. );
  232. }
  233. $v='('.implode(' or ', $v).')';
  234. }
  235. $crontab=implode(' and ', $crontab);
  236. return eval('return '.$crontab.';');
  237. }
  238. var_export(parse_crontab('2011-05-04 02:08:03', '*/2,3-5,9 2 3-5 */2 *'));
  239. var_export(parse_crontab('2011-05-04 02:08:03', '*/8 */2 */4 */5 *'));
Add Comment
Please, Sign In to add comment