99namespace Jenner \Zebra \Crontab ;
1010
1111
12- /**
13- * @author: Jan Konieczny <jkonieczny@gmail.com>
14- * @copyright: Copyright (C) 2009, Jan Konieczny
15- *
16- * This is a simple script to parse crontab syntax to get the execution time
17- *
18- * Eg.: $timestamp = Crontab::parse('12 * * * 1-5');
19- *
20- *
21- * This program is free software: you can redistribute it and/or modify
22- * it under the terms of the GNU General Public License as published by
23- * the Free Software Foundation, either version 3 of the License, or
24- * (at your option) any later version.
25- *
26- * This program is distributed in the hope that it will be useful,
27- * but WITHOUT ANY WARRANTY; without even the implied warranty of
28- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29- * GNU General Public License for more details.
30- *
31- * You should have received a copy of the GNU General Public License
32- * along with this program. If not, see <http://www.gnu.org/licenses/>.
33- */
12+ /**
13+ * @author: Jan Konieczny <jkonieczny@gmail.com>
14+ * @copyright: Copyright (C) 2009, Jan Konieczny
15+ *
16+ * This is a simple script to parse crontab syntax to get the execution time
17+ *
18+ * Eg.: $timestamp = Crontab::parse('12 * * * 1-5');
19+ *
20+ *
21+ * This program is free software: you can redistribute it and/or modify
22+ * it under the terms of the GNU General Public License as published by
23+ * the Free Software Foundation, either version 3 of the License, or
24+ * (at your option) any later version.
25+ *
26+ * This program is distributed in the hope that it will be useful,
27+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29+ * GNU General Public License for more details.
30+ *
31+ * You should have received a copy of the GNU General Public License
32+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33+ */
3434/**
3535 * Provides basic cron syntax parsing functionality
3636 *
3737 * @author: Jan Konieczny <jkonieczny@gmail.com>
3838 * @copyright: Copyright (C) 2009, Jan Konieczny
3939 */
40- class CrontabParse {
40+ class CrontabParse
41+ {
4142 /**
4243 * Finds next execution time(stamp) parsin crontab syntax,
4344 * after given starting timestamp (or current time if ommited)
4445 *
45- * @param string $_cron_string:
46+ * @param string $_cron_string :
4647 *
4748 * 0 1 2 3 4
4849 * * * * * *
@@ -53,40 +54,41 @@ class CrontabParse {
5354 * | | +--------- day of month (1 - 31)
5455 * | +----------- hour (0 - 23)
5556 * +------------- min (0 - 59)
56- * @param int $_after_timestamp timestamp [default=current timestamp]
57- * @return int unix timestamp - next execution time will be greater
57+ * @param int $_after_timestamp timestamp [default=current timestamp]
58+ * @return int unix timestamp - next execution time will be greater
5859 * than given timestamp (defaults to the current timestamp)
59- * @throws \InvalidArgumentException
60+ * @throws \InvalidArgumentException
6061 */
61- public static function parse ($ _cron_string ,$ _after_timestamp= null )
62+ public static function parse ($ _cron_string , $ _after_timestamp = null )
6263 {
63- if (!preg_match ('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i ' ,trim ($ _cron_string ))){
64- throw new \InvalidArgumentException ("Invalid cron string: " . $ _cron_string );
64+ if (!preg_match ('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i ' , trim ($ _cron_string ))) {
65+ throw new \InvalidArgumentException ("Invalid cron string: " . $ _cron_string );
6566 }
66- if ($ _after_timestamp && !is_numeric ($ _after_timestamp )){
67+ if ($ _after_timestamp && !is_numeric ($ _after_timestamp )) {
6768 throw new \InvalidArgumentException ("\$_after_timestamp must be a valid unix timestamp ( $ _after_timestamp given) " );
6869 }
69- $ cron = preg_split ("/[\s]+/i " ,trim ($ _cron_string ));
70- $ start = empty ($ _after_timestamp )? time (): $ _after_timestamp ;
71- $ date = array ( 'minutes ' => self ::_parseCronNumbers ($ cron [0 ],0 , 59 ),
72- 'hours ' => self ::_parseCronNumbers ($ cron [1 ],0 , 23 ),
73- 'dom ' => self ::_parseCronNumbers ($ cron [2 ],1 , 31 ),
74- 'month ' => self ::_parseCronNumbers ($ cron [3 ],1 , 12 ),
75- 'dow ' => self ::_parseCronNumbers ($ cron [4 ],0 , 6 ),
70+ $ cron = preg_split ("/[\s]+/i " , trim ($ _cron_string ));
71+ $ start = empty ($ _after_timestamp ) ? time () : $ _after_timestamp ;
72+ $ date = array ('minutes ' => self ::_parseCronNumbers ($ cron [0 ], 0 , 59 ),
73+ 'hours ' => self ::_parseCronNumbers ($ cron [1 ], 0 , 23 ),
74+ 'dom ' => self ::_parseCronNumbers ($ cron [2 ], 1 , 31 ),
75+ 'month ' => self ::_parseCronNumbers ($ cron [3 ], 1 , 12 ),
76+ 'dow ' => self ::_parseCronNumbers ($ cron [4 ], 0 , 6 ),
7677 );
7778 // limited to time()+366 - no need to check more than 1year ahead
78- for ($ i= 0 ; $ i <= 60 * 60 * 24 * 366 ;$ i += 60 ){
79- if ( in_array (intval (date ('j ' ,$ start+ $ i )),$ date ['dom ' ]) &&
80- in_array (intval (date ('n ' ,$ start+ $ i )),$ date ['month ' ]) &&
81- in_array (intval (date ('w ' ,$ start+ $ i )),$ date ['dow ' ]) &&
82- in_array (intval (date ('G ' ,$ start+ $ i )),$ date ['hours ' ]) &&
83- in_array (intval (date ('i ' ,$ start+ $ i )),$ date ['minutes ' ])
84- ){
85- return $ start+ $ i ;
79+ for ($ i = 0 ; $ i <= 60 * 60 * 24 * 366 ; $ i += 60 ) {
80+ if ( in_array (intval (date ('j ' , $ start + $ i )), $ date ['dom ' ]) &&
81+ in_array (intval (date ('n ' , $ start + $ i )), $ date ['month ' ]) &&
82+ in_array (intval (date ('w ' , $ start + $ i )), $ date ['dow ' ]) &&
83+ in_array (intval (date ('G ' , $ start + $ i )), $ date ['hours ' ]) &&
84+ in_array (intval (date ('i ' , $ start + $ i )), $ date ['minutes ' ])
85+ ) {
86+ return $ start + $ i ;
8687 }
8788 }
8889 return null ;
8990 }
91+
9092 /**
9193 * get a single cron style notation and parse it into numeric value
9294 *
@@ -95,18 +97,18 @@ public static function parse($_cron_string,$_after_timestamp=null)
9597 * @param int $max maximum possible value
9698 * @return int parsed number
9799 */
98- protected static function _parseCronNumbers ($ s ,$ min ,$ max )
100+ protected static function _parseCronNumbers ($ s , $ min , $ max )
99101 {
100102 $ result = array ();
101- $ v = explode (', ' ,$ s );
102- foreach ($ v as $ vv ){
103- $ vvv = explode ('/ ' ,$ vv );
104- $ step = empty ($ vvv [1 ])? 1 : $ vvv [1 ];
105- $ vvvv = explode ('- ' ,$ vvv [0 ]);
106- $ _min = count ($ vvvv )== 2 ? $ vvvv [0 ]: ($ vvv [0 ]== '* ' ? $ min: $ vvv [0 ]);
107- $ _max = count ($ vvvv )== 2 ? $ vvvv [1 ]: ($ vvv [0 ]== '* ' ? $ max: $ vvv [0 ]);
108- for ($ i= $ _min ;$ i <= $ _max ;$ i += $ step ){
109- $ result [$ i ]= intval ($ i );
103+ $ v = explode (', ' , $ s );
104+ foreach ($ v as $ vv ) {
105+ $ vvv = explode ('/ ' , $ vv );
106+ $ step = empty ($ vvv [1 ]) ? 1 : $ vvv [1 ];
107+ $ vvvv = explode ('- ' , $ vvv [0 ]);
108+ $ _min = count ($ vvvv ) == 2 ? $ vvvv [0 ] : ($ vvv [0 ] == '* ' ? $ min : $ vvv [0 ]);
109+ $ _max = count ($ vvvv ) == 2 ? $ vvvv [1 ] : ($ vvv [0 ] == '* ' ? $ max : $ vvv [0 ]);
110+ for ($ i = $ _min ; $ i <= $ _max ; $ i += $ step ) {
111+ $ result [$ i ] = intval ($ i );
110112 }
111113 }
112114 ksort ($ result );
0 commit comments