]> git.pond.sub.org Git - empserver/blob - src/lib/common/hours.c
Clean up redundant forward declarations
[empserver] / src / lib / common / hours.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  hours.c: Game hours determination; is it legal to play now?
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Doug Hay, 1998
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2004-2010
34  */
35
36 #include <config.h>
37
38 #include <ctype.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_range(char *str, int *from_min, int *to_min);
46
47 /*
48  * Is week day WDAY (Sunday is 0) allowed by restriction DAYS?
49  * If DAYS is not empty, it lists the allowed week day names.  See
50  * weekday() for syntax.
51  */
52 int
53 is_wday_allowed(int wday, char *days)
54 {
55     int wd;
56
57     if (!days || !*days)
58         return 1;
59
60     while (NULL != (days = weekday(days, &wd)))
61         if (wd == wday)
62             return 1;
63
64     return 0;
65 }
66
67 /*
68  * Is day time DTIME (minutes since midnight) allowed by restriction TIMES?
69  * If TIMES is not empty, it lists the allowed day time ranges.  See
70  * daytime_range() for syntax.
71  */
72 int
73 is_daytime_allowed(int dtime, char *times)
74 {
75     int from, to;
76
77     if (!times || !*times)
78         return 1;
79
80     while (NULL != (times = daytime_range(times, &from, &to)))
81         if (from <= dtime && dtime < to)
82             return 1;
83
84     return 0;
85 }
86
87 /*
88  * Can the game played at time T?
89  */
90 int
91 gamehours(time_t t)
92 {
93     struct tm *tm;
94
95     tm = localtime(&t);
96     if (!is_wday_allowed(tm->tm_wday, game_days))
97         return 0;
98     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
99 }
100
101 /*
102  * Parse weekday name in STR.
103  * On success assign day number (Sunday is 0) to *WDAY and return
104  * pointer to first character not parsed.
105  * Else return NULL.
106  * Abbreviated names are recognized, but not single characters.
107  * Initial whitespace is ignored.
108  */
109 static char *
110 weekday(char *str, int *wday)
111 {
112     /*
113      * strptime() format " %a" would do fine, but it's XPG and Windows
114      * doesn't have it.  Besides, Empire accepts more abbreviations.
115      */
116     static char *day_name[7] = {
117         "sunday", "monday", "tuesday", "wednesday",
118         "thursday", "friday", "saturday"
119     };
120     int i, j;
121
122     for (; isspace(*str); ++str) ;
123
124     for (i = 0; i < 7; ++i) {
125         j = 0;
126         while (str[j] && tolower(str[j]) == day_name[i][j])
127             ++j;
128         if (j > 1) {
129             *wday = i;
130             return str + j;
131         }
132     }
133
134     return NULL;
135 }
136
137 /*
138  * Parse day time in STR.
139  * On success store minutes since midnight in *MIN and return pointer
140  * to first character not parsed.
141  * Else return NULL.
142  * Time format is HOUR:MINUTE.  Initial whitespace is ignored.
143  */
144 static char *
145 daytime(char *str, int *min)
146 {
147     /*
148      * strptime() format " %H:%M" would do fine, but it's XPG and
149      * Windows doesn't have it.
150      */
151     char *end;
152     unsigned long h, m;
153
154     h = strtoul(str, &end, 10);
155     if (end == str || h > 24)
156         return NULL;
157
158     if (*end++ != ':')
159         return NULL;
160
161     str = end;
162     m = strtoul(str, &end, 10);
163     if (end == str || m > 59)
164         return NULL;
165     else if (h == 24 && m != 0)
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 static 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 }