-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcron_spec.cpp
137 lines (126 loc) · 3.72 KB
/
cron_spec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "cron_spec.h"
#include "cron_err.h"
namespace Cron{
bool SpecSchedule::DayMatch(bool dom , bool dow)const {
if ( (m_dom & AnyMark) || ( m_dow & AnyMark ) ) {
return dom && dow ;
} else {
return dom || dow ;
}
}
bool SpecSchedule::operator == (const SpecSchedule & another )const {
if( this == &another ) {
return true;
} else {
return m_dow == another.m_dow && m_dom == another.m_dom && m_mon == another.m_mon
&& m_hour == another.m_hour && m_min == another.m_min && m_sec == another.m_sec ;
}
}
TimePoint SpecSchedule::Next(const TimePoint &now) const{
TimePoint next = now + CRON_SECONDS(1);
int year_limit = CRON_TO_TM(next).tm_year + 5 ;
bool added = false ;
WRAP:
if (CRON_TO_TM(next).tm_year > year_limit ) {
return INVALID_TIMEPOINT;
}
// Month
for( ; ; ){
auto ctime = CRON_TO_TM(next);
// month start from 0
if( Match(ctime.tm_mon +1 , m_mon) ) {
break ;
}
if(!added) {
ctime.tm_mday = 1;
ctime.tm_hour = 0 ;
ctime.tm_min = 0;
ctime.tm_sec = 0 ;
next = CRON_FROM_TM(ctime);
added = true ;
}
int prev_month = ctime.tm_mon;
next += CRON_DAY( 28 ) ;
for( ; ; ) {
if( CRON_TO_TM(next).tm_mon != prev_month ) {
break;
}
next += CRON_DAY(1) ;
}
ctime = CRON_TO_TM(next);
if( ctime.tm_mon == 0 ) {
goto WRAP;
}
}
//Day
for( ; ; ){
auto ctime = CRON_TO_TM(next);
if( DayMatch(Match(ctime.tm_mday , m_dom) , Match(ctime.tm_wday , m_dow)) ) {
break ;
}
if(!added) {
ctime.tm_hour = 0 ;
ctime.tm_min = 0;
ctime.tm_sec = 0 ;
next = CRON_FROM_TM(ctime);
added = true ;
}
next += CRON_DAY(1);
ctime = CRON_TO_TM(next);
if( ctime.tm_mday == 1 ) {
goto WRAP;
}
}
// Hours
for( ; ; ){
auto ctime = CRON_TO_TM(next);
if( Match(ctime.tm_hour , m_hour) ) {
break ;
}
if(!added) {
ctime.tm_min = 0;
ctime.tm_sec = 0 ;
next = CRON_FROM_TM(ctime);
added = true ;
}
next += CRON_HOURS(1) ;
ctime = CRON_TO_TM(next);
if( ctime.tm_hour == 0 ) {
goto WRAP;
}
}
// Minutes
for( ; ; ){
auto ctime = CRON_TO_TM(next);
if( Match(ctime.tm_min , m_min) ) {
break ;
}
if(!added) {
ctime.tm_sec = 0 ;
next = CRON_FROM_TM(ctime);
added = true ;
}
next += CRON_MINUTES(1) ;
ctime = CRON_TO_TM(next);
if( ctime.tm_min == 0 ) {
goto WRAP;
}
}
// Seconds
for( ; ; ){
auto ctime = CRON_TO_TM(next);
if( Match(ctime.tm_sec , m_sec) ) {
break ;
}
if(!added) {
added = true ;
}
next += CRON_SECONDS(1) ;
ctime = CRON_TO_TM(next);
if( ctime.tm_sec == 0 ) {
goto WRAP;
}
}
return next;
}
}