]> git.pond.sub.org Git - empserver/blob - src/lib/common/hours.c
New update scheduler:
[empserver] / src / lib / common / hours.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 <ctype.h>
40 #include <limits.h>
41 #include <time.h>
42 #include "misc.h"
43 #include "optlist.h"
44 #include "prototypes.h"
45
46 static char *weekday(char *str, int *wday);
47 static char *daytime(char *str, int *min);
48 static char *daytime_range(char *str, int *from_min, int *to_min);
49
50 /*
51  * Is week day WDAY (Sunday is 0) allowed by restriction DAYS?
52  * If DAYS is not empty, it lists the allowed week day names.  See
53  * weekday() for syntax.
54  */
55 int
56 is_wday_allowed(int wday, char *days)
57 {
58     int wd;
59
60     if (!days || !*days)
61         return 1;
62
63     while (NULL != (days = weekday(days, &wd)))
64         if (wd == wday)
65             return 1;
66
67     return 0;
68 }
69
70 /*
71  * Is day time DTIME (minutes since midnight) allowed by restriction TIMES?
72  * If TIMES is not empty, it lists the allowed day time ranges.  See
73  * daytime_range() for syntax.
74  */
75 int
76 is_daytime_allowed(int dtime, char *times)
77 {
78     int from, to;
79
80     if (!times || !*times)
81         return 1;
82
83     while (NULL != (times = daytime_range(times, &from, &to)))
84         if (from <= dtime && dtime < to)
85             return 1;
86
87     return 0;
88 }
89
90 /*
91  * Can the game played at time T?
92  */
93 int
94 gamehours(time_t t)
95 {
96     struct tm *tm;
97
98     tm = localtime(&t);
99     if (!is_wday_allowed(tm->tm_wday, game_days))
100         return 0;
101     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
102 }
103
104 /*
105  * Parse weekday name in STR.
106  * On success assign day number (Sunday is 0) to *WDAY and return
107  * pointer to first character not parsed.
108  * Else return NULL.
109  * Abbreviated names are recognized, but not single characters.
110  * Initial whitespace is ignored.
111  */
112 static char *
113 weekday(char *str, int *wday)
114 {
115     /*
116      * strptime() format " %a" would do fine, but it's XPG and Windows
117      * doesn't have it.  Besides, Empire accepts more abbreviations.
118      */
119     static char *day_name[7] = {
120         "sunday", "monday", "tuesday", "wednesday",
121         "thursday", "friday", "saturday" };
122     int i, j;
123
124     for (; isspace(*str); ++str) ;
125
126     for (i = 0; i < 7; ++i) {
127         j = 0;
128         while (str[j] && tolower(str[j]) == day_name[i][j])
129             ++j;
130         if (j > 1) {
131             *wday = i;
132             return str + j;
133         }
134     }
135
136     return NULL;
137 }
138
139 /*
140  * Parse day time in STR.
141  * On success store minutes since midnight in *MIN and return pointer
142  * to first character not parsed.
143  * Else return NULL.
144  * Time format is HOUR:MINUTE.  Initial whitespace is ignored.
145  */
146 char *
147 daytime(char *str, int *min)
148 {
149     /*
150      * strptime() format " %H:%M" would do fine, but it's XPG and
151      * Windows doesn't have it.
152      */
153     char *end;
154     unsigned long h, m;
155
156     h = strtoul(str, &end, 10);
157     if (end == str || h > 23)
158         return NULL;
159
160     if (*end++ != ':')
161         return NULL;
162
163     str = end;
164     m = strtoul(str, &end, 10);
165     if (end == str || m > 59)
166         return NULL;
167
168     *min = 60 * h + m;
169     return end;
170 }
171
172 /*
173  * Parse a day time range in STR.
174  * On success store minutes since midnight in *FROM and *TO, return
175  * pointer to first character not parsed.
176  * Else return NULL.
177  * Format is HOUR:MINUTE-HOUR:MINUTE.  Initial whitespace is ignored.
178  */
179 char *
180 daytime_range(char *str, int *from_min, int *to_min)
181 {
182     char *end;
183
184     end = daytime(str, from_min);
185     if (!end)
186         return NULL;
187     while (isspace(*end)) ++end;
188     if (*end++ != '-')
189         return NULL;
190     return daytime(end, to_min);
191 }