Debian Codegraph @

libhylafax/TimeOfDay.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "TimeOfDay.h"
27 #include <ctype.h>
28
29 class Sys {
30 public:
31 static struct tm* localtime(time_t& t) { return ::localtime(&t); }
32 };
33
34 #define BIT(x) (1<<(x))
35
36 TimeOfDay::TimeOfDay() : tod(0xff, 0, 24*60) // any day, any time
37 {
38 }
39 TimeOfDay::TimeOfDay(const TimeOfDay& other) : tod(other.tod)
40 {
41 for (_tod* t = other.tod.next; t; t = t->next)
42 add(t->days, t->start, t->end);
43 }
44 TimeOfDay::~TimeOfDay()
45 {
46 reset();
47 }
48
49 void
50 TimeOfDay::reset()
51 {
52 if (tod.days != 0xff) {
53 _tod* next;
54 for (_tod* t = tod.next; t; t = next) {
55 next = t->next;
56 delete t;
57 }
58 tod.days = 0xff;
59 tod.start = 0;
60 tod.end = 24*60;
61 tod.next = NULL;
62 }
63 }
64
65 void
66 TimeOfDay::add(int days, time_t start, time_t end)
67 {
68 if (tod.days == 0xff) { // NB: 0xff means initialized by constructure
69 tod.days = days;
70 tod.start = start;
71 tod.end = end;
72 } else {
73 _tod* t = new _tod(days, start, end);
74 _tod** tpp;
75 for (tpp = &tod.next; *tpp; tpp = &(*tpp)->next)
76 ;
77 *tpp = t;
78 }
79 }
80
81 /*
82 * Parse time-to-send specification string and generate
83 * an internal format that's easy to check against a
84 * struct tm time.
85 *
86 * Syntax = tod ["," tod]
87 * tod = <days><timerange>
88 * days = "Any" | "Wk" | <dayname>+ | nothing
89 * dayname = "Sun" | "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat"
90 * timerange = <start> "-" <end> | nothing
91 * start = <24hrtime>
92 * end = <24hrtime>
93 * 24hrtime = {0-9}{0-9}{0-9}{0-9}
94 *
95 * where start & end are 24-hour times, day names can be either
96 * 2- or 3-characters, and a null day or time specification means
97 * any time or day. For example,
98 *
99 * Any any time of any day
100 * MoWeFri0800-0900 Monday+Wednesday+Friday between 8am and 9am
101 * 2330-0130 any day, between 11:30pm and 1:30am the next day
102 * TueThu any time on Tuesday and Thursday
103 *
104 * White space and other syntactic sugar may be freely inserted between
105 * tokens:
106 *
107 * MoWeFri/0800-0900 Monday+Wednesday+Friday between 8am and 9am
108 * Tue+Thu any time on Tuesday and Thursday
109 *
110 * but may not be inserted between 24-hour times in the time range.
111 */
112 void
113 TimeOfDay::parse(const char* cp)
114 {
115 reset();
116 while (*cp != '\0') {
117 while (isspace(*cp))
118 cp++;
119 int days = 0;
120 if (strneq(cp, "Any", 3)) { // any day
121 days = 0x7f;
122 cp += 3;
123 } else if (strneq(cp, "Wk", 2)) { // any week day
124 days = 0x3e;
125 cp += 2;
126 } else if (isalpha(*cp)) { // list of days
127 do {
128 static const char* dayNames = "Sun Mon Tue Wed Thu Fri Sat ";
129 u_int i;
130 for (i = 0; dayNames[i] != '\0'; i += 4)
131 if (cp[0] == dayNames[i] && cp[1] == dayNames[i+1])
132 break;
133 if (dayNames[i] == '\0') {
134 // XXX unknown day
135 break;
136 }
137 days |= BIT(i>>2);
138 cp += (cp[2] == dayNames[i+2] ? 3 : 2);
139 for (; !isalnum(*cp) && *cp != ',' && *cp; cp++)
140 ;
141 } while (isalpha(*cp));
142 }
143 if (days == 0)
144 days = 0x7f; // default to any day
145 // skip to any time range
146 while (*cp && *cp != ',' && !isdigit(*cp))
147 cp++;
148 unsigned start, end;
149 if (sscanf(cp, "%u-%u", &start, &end) == 2) {
150 // convert from military time to seconds
151 start = (start/100)*60 + (start%100);
152 end = (end/100)*60 + (end%100);
153 } else {
154 // no time spec or invalid spec, use all day
155 start = 0;
156 end = 24*60;
157 }
158 add(days, start, end);
159 // skip to next time-of-day specification
160 while (*cp && *cp++ != ',')
161 ;
162 }
163 }
164
165 /*
166 * Return the next time of day that's OK.
167 */
168 time_t
169 TimeOfDay::nextTimeOfDay(time_t t) const
170 {
171 struct tm* tm = Sys::localtime(t);
172 time_t best = 7*24*60+1; // 1 week+1 minute
173 time_t hm = tm->tm_hour*60 + tm->tm_min;
174 for (const _tod* td = &tod; td; td = td->next) {
175 time_t diff = td->nextTime(tm->tm_wday, hm);
176 if (diff < best)
177 best = diff;
178 }
179 return (t + 60*best);
180 }
181
182 _tod::_tod(int d, time_t s, time_t e)
183 {
184 days = d;
185 start = s;
186 end = e;
187 next = NULL;
188 }
189 _tod::_tod(const _tod& other)
190 {
191 days = other.days;
192 start = other.start;
193 end = other.end;
194 next = NULL;
195 }
196
197 /*
198 * Starting at wday+d, look for an OK day.
199 */
200 int
201 _tod::nextDay(int d, int wday) const
202 {
203 for (int w = (wday+d)%7; (days & BIT(w)) == 0; w = (w+1)%7)
204 d++;
205 return d;
206 }
207
208 /*
209 * Calculate the time differential to the next
210 * time at which <wday,hm> is acceptable. Note
211 * that 0 means it's OK right now.
212 */
213 time_t
214 _tod::nextTime(int wday, time_t hm) const
215 {
216 int d = (days & BIT(wday)) ? 0 : nextDay(1, wday);
217 time_t mins;
218 if (start <= end) {
219 if (start <= hm) {
220 if (end < hm) { // outside time range
221 mins = (24*60-hm)+start;// time to next start of range
222 if (d == 0) // find next available day
223 d = nextDay(d+1, wday);
224 d--; // time includes day forward
225 } else { // within time range
226 if (d != 0) { // backup to start of range
227 mins = (24*60-hm)+start;
228 d--; // time includes day forward
229 } else // ok to go now
230 mins = 0;
231 }
232 } else { // before start of range
233 mins = start - hm; // push to start of range
234 }
235 } else {
236 if (start <= hm) { // within time range
237 if (d != 0) { // backup to start of range
238 mins = (24*60-hm)+start;
239 d--; // time includes day forward
240 } else // ok to go now
241 mins = 0;
242 } else {
243 if (end < hm) // outside time range, push to start
244 mins = start - hm;
245 else // ok to go now
246 mins = 0;
247 }
248 }
249 return (d*24*60 + mins);
250 }