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