]> git.pond.sub.org Git - empserver/blob - src/lib/common/hours.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / common / hours.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
37
38 #include <limits.h>
39 #include <time.h>
40 #include "misc.h"
41 #include "optlist.h"
42 #include "prototypes.h"
43
44 static char *weekday(char *str, int *wday);
45 static char *daytime(char *str, int *min);
46 static char *daytime_range(char *str, int *from_min, int *to_min);
47
48 /*
49  * Is week day WDAY (Sunday is 0) allowed by restriction DAYS?
50  * If DAYS is not empty, it lists the allowed week day names.  See
51  * weekday() for syntax.
52  */
53 int
54 is_wday_allowed(int wday, char *days)
55 {
56     int wd;
57
58     if (!days || !*days)
59         return 1;
60
61     while (NULL != (days = weekday(days, &wd)))
62         if (wd == wday)
63             return 1;
64
65     return 0;
66 }
67
68 /*
69  * Is day time DTIME (minutes since midnight) allowed by restriction TIMES?
70  * If TIMES is not empty, it lists the allowed day time ranges.  See
71  * daytime_range() for syntax.
72  */
73 int
74 is_daytime_allowed(int dtime, char *times)
75 {
76     int from, to;
77
78     if (!times || !*times)
79         return 1;
80
81     while (NULL != (times = daytime_range(times, &from, &to)))
82         if (from <= dtime && dtime < to)
83             return 1;
84
85     return 0;
86 }
87
88 /*
89  * Can the game played at time T?
90  */
91 int
92 gamehours(time_t t)
93 {
94     struct tm *tm;
95
96     tm = localtime(&t);
97     if (!is_wday_allowed(tm->tm_wday, game_days))
98         return 0;
99     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
100 }
101
102 /*
103  * Is day time DTIME (minutes since midnight) near a time in TIMES?
104  * TIMES is a list of day times.  See daytime() for syntax.
105  * DTIME is near a listed time T if its within T and T + SLOP minutes.
106  */
107 int
108 is_daytime_near(int dtime, char *times, int slop)
109 {
110     int dt;
111
112     if (times)
113         while (NULL != (times = daytime(times, &dt)))
114             if (dt <= dtime && dtime < dt + slop)
115                 return 1;
116
117     return 0;
118 }
119
120 /*
121  * Return time in minutes between DTIME and next time in TIMES.
122  * If TIMES doesn't give a time, return -1.
123  * DTIME is day time in minutes since midnight.
124  * TIMES is a list of day times.  See daytime() for syntax.
125  */
126 int
127 min_to_next_daytime(int dtime, char *times)
128 {
129     int mindt = INT_MAX;
130     int dt;
131
132     if (times) {
133         while (NULL != (times = daytime(times, &dt))) {
134             if (dt <= dtime)
135                 dt += 24 * 60;  /* tomorrow */
136             if (dt < mindt)
137                 mindt = dt;
138         }
139     }
140
141     if (mindt == INT_MAX)
142         return -1;
143     return mindt - dtime;
144 }
145
146 /*
147  * Parse weekday name in STR.
148  * On success assign day number (Sunday is 0) to *WDAY and return
149  * pointer to first character not parsed.
150  * Else return NULL.
151  * Abbreviated names are recognized, but not single characters.
152  * Initial whitespace is ignored.
153  */
154 static char *
155 weekday(char *str, int *wday)
156 {
157     /*
158      * strptime() format " %a" would do fine, but it's XPG and Windows
159      * doesn't have it.  Besides, Empire accepts more abbreviations.
160      */
161     static char *day_name[7] = {
162         "sunday", "monday", "tuesday", "wednesday",
163         "thursday", "friday", "saturday" };
164     int i, j;
165
166     for (; isspace(*str); ++str) ;
167
168     for (i = 0; i < 7; ++i) {
169         j = 0;
170         while (str[j] && tolower(str[j]) == day_name[i][j])
171             ++j;
172         if (j > 1) {
173             *wday = i;
174             return str + j;
175         }
176     }
177
178     return NULL;
179 }
180
181 /*
182  * Parse day time in STR.
183  * On success store minutes since midnight in *MIN and return pointer
184  * to first character not parsed.
185  * Else return NULL.
186  * Time format is HOUR:MINUTE.  Initial whitespace is ignored.
187  */
188 char *
189 daytime(char *str, int *min)
190 {
191     /*
192      * strptime() format " %H:%M" would do fine, but it's XPG and
193      * Windows doesn't have it.
194      */
195     char *end;
196     unsigned long h, m;
197
198     h = strtoul(str, &end, 10);
199     if (end == str || h > 23)
200         return NULL;
201
202     if (*end++ != ':')
203         return NULL;
204
205     str = end;
206     m = strtoul(str, &end, 10);
207     if (end == str || m > 59)
208         return NULL;
209
210     *min = 60 * h + m;
211     return end;
212 }
213
214 /*
215  * Parse a day time range in STR.
216  * On success store minutes since midnight in *FROM and *TO, return
217  * pointer to first character not parsed.
218  * Else return NULL.
219  * Format is HOUR:MINUTE-HOUR:MINUTE.  Initial whitespace is ignored.
220  */
221 char *
222 daytime_range(char *str, int *from_min, int *to_min)
223 {
224     char *end;
225
226     end = daytime(str, from_min);
227     if (!end)
228         return NULL;
229     while (isspace(*end)) ++end;
230     if (*end++ != '-')
231         return NULL;
232     return daytime(end, to_min);
233 }