]> git.pond.sub.org Git - empserver/blob - src/lib/common/hours.c
Update copyright notice.
[empserver] / src / lib / common / hours.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  hours.c: Game hours determination; is it legal to play now?
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Doug Hay, 1998
33  *     Steve McClure, 1998
34  */
35
36 #include <limits.h>
37 #include <time.h>
38 #include "misc.h"
39 #include "optlist.h"
40 #include "prototypes.h"
41
42 static char *weekday(char *str, int *wday);
43 static char *daytime(char *str, int *min);
44 static char *daytime_range(char *str, int *from_min, int *to_min);
45
46 /*
47  * Is week day WDAY (Sunday is 0) allowed by restriction DAYS?
48  * If DAYS is not empty, it lists the allowed week day names.  See
49  * weekday() for syntax.
50  */
51 int
52 is_wday_allowed(int wday, char *days)
53 {
54     int wd;
55
56     if (!days || !*days)
57         return 1;
58
59     while (NULL != (days = weekday(days, &wd)))
60         if (wd == wday)
61             return 1;
62
63     return 0;
64 }
65
66 /*
67  * Is day time DTIME (minutes since midnight) allowed by restriction TIMES?
68  * If TIMES is not empty, it lists the allowed day time ranges.  See
69  * daytime_range() for syntax.
70  */
71 int
72 is_daytime_allowed(int dtime, char *times)
73 {
74     int from, to;
75
76     if (!times || !*times)
77         return 1;
78
79     while (NULL != (times = daytime_range(times, &from, &to)))
80         if (from <= dtime && dtime < to)
81             return 1;
82
83     return 0;
84 }
85
86 /*
87  * Can the game played at time T?
88  */
89 int
90 gamehours(time_t t)
91 {
92     struct tm *tm;
93
94     tm = localtime(&t);
95     if (!is_wday_allowed(tm->tm_wday, game_days))
96         return 0;
97     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
98 }
99
100 /*
101  * Is day time DTIME (minutes since midnight) near a time in TIMES?
102  * TIMES is a list of day times.  See daytime() for syntax.
103  * DTIME is near a listed time T if its within T and T + SLOP minutes.
104  */
105 int
106 is_daytime_near(int dtime, char *times, int slop)
107 {
108     int dt;
109
110     if (times)
111         while (NULL != (times = daytime(times, &dt)))
112             if (dt <= dtime && dtime < dt + slop)
113                 return 1;
114
115     return 0;
116 }
117
118 /*
119  * Return time in minutes between DTIME and next time in TIMES.
120  * If TIMES doesn't give a time, return -1.
121  * DTIME is day time in minutes since midnight.
122  * TIMES is a list of day times.  See daytime() for syntax.
123  */
124 int
125 min_to_next_daytime(int dtime, char *times)
126 {
127     int mindt = INT_MAX;
128     int dt;
129
130     if (times) {
131         while (NULL != (times = daytime(times, &dt))) {
132             if (dt <= dtime)
133                 dt += 24 * 60;  /* tomorrow */
134             if (dt < mindt)
135                 mindt = dt;
136         }
137     }
138
139     if (mindt == INT_MAX)
140         return -1;
141     return mindt - dtime;
142 }
143
144 /*
145  * Parse weekday name in STR.
146  * On success assign day number (Sunday is 0) to *WDAY and return
147  * pointer to first character not parsed.
148  * Else return NULL.
149  * Abbreviated names are recognized, but not single characters.
150  * Initial whitespace is ignored.
151  */
152 static char *
153 weekday(char *str, int *wday)
154 {
155     /*
156      * strptime() format " %a" would do fine, but it's XPG and Windows
157      * doesn't have it.  Besides, Empire accepts more abbreviations.
158      */
159     static char *day_name[7] = {
160         "sunday", "monday", "tuesday", "wednesday",
161         "thursday", "friday", "saturday" };
162     int i, j;
163
164     for (; isspace(*str); ++str) ;
165
166     for (i = 0; i < 7; ++i) {
167         j = 0;
168         while (str[j] && tolower(str[j]) == day_name[i][j])
169             ++j;
170         if (j > 1) {
171             *wday = i;
172             return str + j;
173         }
174     }
175
176     return NULL;
177 }
178
179 /*
180  * Parse day time in STR.
181  * On success store minutes since midnight in *MIN and return pointer
182  * to first character not parsed.
183  * Else return NULL.
184  * Time format is HOUR:MINUTE.  Initial whitespace is ignored.
185  */
186 char *
187 daytime(char *str, int *min)
188 {
189     /*
190      * strptime() format " %H:%M" would do fine, but it's XPG and
191      * Windows doesn't have it.
192      */
193     char *end;
194     unsigned long h, m;
195
196     h = strtoul(str, &end, 10);
197     if (end == str || h > 23)
198         return NULL;
199
200     if (*end++ != ':')
201         return NULL;
202
203     str = end;
204     m = strtoul(str, &end, 10);
205     if (end == str || m > 59)
206         return NULL;
207
208     *min = 60 * h + m;
209     return end;
210 }
211
212 /*
213  * Parse a day time range in STR.
214  * On success store minutes since midnight in *FROM and *TO, return
215  * pointer to first character not parsed.
216  * Else return NULL.
217  * Format is HOUR:MINUTE-HOUR:MINUTE.  Initial whitespace is ignored.
218  */
219 char *
220 daytime_range(char *str, int *from_min, int *to_min)
221 {
222     char *end;
223
224     end = daytime(str, from_min);
225     if (!end)
226         return NULL;
227     while (isspace(*end)) ++end;
228     if (*end++ != '-')
229         return NULL;
230     return daytime(end, to_min);
231 }