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
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.
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.
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
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.
28 * hours.c: Game hours determination; is it legal to play now?
30 * Known contributors to this file:
34 * Markus Armbruster, 2004
44 #include "prototypes.h"
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);
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.
56 is_wday_allowed(int wday, char *days)
63 while (NULL != (days = weekday(days, &wd)))
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.
76 is_daytime_allowed(int dtime, char *times)
80 if (!times || !*times)
83 while (NULL != (times = daytime_range(times, &from, &to)))
84 if (from <= dtime && dtime < to)
91 * Can the game played at time T?
99 if (!is_wday_allowed(tm->tm_wday, game_days))
101 return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
105 * Is day time DTIME (minutes since midnight) near a time in TIMES?
106 * TIMES is a list of day times. See daytime() for syntax.
107 * DTIME is near a listed time T if its within T and T + SLOP minutes.
110 is_daytime_near(int dtime, char *times, int slop)
115 while (NULL != (times = daytime(times, &dt)))
116 if (dt <= dtime && dtime < dt + slop)
123 * Return time in minutes between DTIME and next time in TIMES.
124 * If TIMES doesn't give a time, return -1.
125 * DTIME is day time in minutes since midnight.
126 * TIMES is a list of day times. See daytime() for syntax.
129 min_to_next_daytime(int dtime, char *times)
135 while (NULL != (times = daytime(times, &dt))) {
137 dt += 24 * 60; /* tomorrow */
143 if (mindt == INT_MAX)
145 return mindt - dtime;
149 * Parse weekday name in STR.
150 * On success assign day number (Sunday is 0) to *WDAY and return
151 * pointer to first character not parsed.
153 * Abbreviated names are recognized, but not single characters.
154 * Initial whitespace is ignored.
157 weekday(char *str, int *wday)
160 * strptime() format " %a" would do fine, but it's XPG and Windows
161 * doesn't have it. Besides, Empire accepts more abbreviations.
163 static char *day_name[7] = {
164 "sunday", "monday", "tuesday", "wednesday",
165 "thursday", "friday", "saturday" };
168 for (; isspace(*str); ++str) ;
170 for (i = 0; i < 7; ++i) {
172 while (str[j] && tolower(str[j]) == day_name[i][j])
184 * Parse day time in STR.
185 * On success store minutes since midnight in *MIN and return pointer
186 * to first character not parsed.
188 * Time format is HOUR:MINUTE. Initial whitespace is ignored.
191 daytime(char *str, int *min)
194 * strptime() format " %H:%M" would do fine, but it's XPG and
195 * Windows doesn't have it.
200 h = strtoul(str, &end, 10);
201 if (end == str || h > 23)
208 m = strtoul(str, &end, 10);
209 if (end == str || m > 59)
217 * Parse a day time range in STR.
218 * On success store minutes since midnight in *FROM and *TO, return
219 * pointer to first character not parsed.
221 * Format is HOUR:MINUTE-HOUR:MINUTE. Initial whitespace is ignored.
224 daytime_range(char *str, int *from_min, int *to_min)
228 end = daytime(str, from_min);
231 while (isspace(*end)) ++end;
234 return daytime(end, to_min);