2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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, see <http://www.gnu.org/licenses/>.
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.
27 * hours.c: Game hours determination; is it legal to play now?
29 * Known contributors to this file:
33 * Markus Armbruster, 2004-2010
42 #include "prototypes.h"
44 static char *weekday(char *str, int *wday);
45 static char *daytime_range(char *str, int *from_min, int *to_min);
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.
53 is_wday_allowed(int wday, char *days)
60 while (NULL != (days = weekday(days, &wd)))
68 * Is day time @dtime allowed by restriction @times?
69 * @dtime is in minutes since midnight.
70 * If @times is not empty, it lists the allowed day time ranges. See
71 * daytime_range() for syntax.
74 is_daytime_allowed(int dtime, char *times)
78 if (!times || !*times)
81 while (NULL != (times = daytime_range(times, &from, &to)))
82 if (from <= dtime && dtime < to)
89 * Can the game played at time T?
97 if (!is_wday_allowed(tm->tm_wday, game_days))
99 return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
103 * Parse weekday name in @str.
104 * On success assign day number (Sunday is 0) to *@wday and return
105 * pointer to first character not parsed.
107 * Abbreviated names are recognized, but not single characters.
108 * Initial whitespace is ignored.
111 weekday(char *str, int *wday)
114 * strptime() format " %a" would do fine, but it's XPG and Windows
115 * doesn't have it. Besides, Empire accepts more abbreviations.
117 static char *day_name[7] = {
118 "sunday", "monday", "tuesday", "wednesday",
119 "thursday", "friday", "saturday"
123 for (; isspace(*str); ++str) ;
125 for (i = 0; i < 7; ++i) {
127 while (str[j] && tolower(str[j]) == day_name[i][j])
139 * Parse day time in @str.
140 * On success store minutes since midnight in *@min and return pointer
141 * to first character not parsed.
143 * Time format is HOUR:MINUTE. Initial whitespace is ignored.
146 daytime(char *str, int *min)
149 * strptime() format " %H:%M" would do fine, but it's XPG and
150 * Windows doesn't have it.
155 h = strtoul(str, &end, 10);
156 if (end == str || h > 24)
163 m = strtoul(str, &end, 10);
164 if (end == str || m > 59)
166 else if (h == 24 && m != 0)
174 * Parse a day time range in @str.
175 * On success, store minutes since midnight in *@from_min, *@to_min,
176 * and return pointer to first character not parsed.
178 * Format is HOUR:MINUTE-HOUR:MINUTE. Initial whitespace is ignored.
181 daytime_range(char *str, int *from_min, int *to_min)
185 end = daytime(str, from_min);
188 while (isspace(*end)) ++end;
191 return daytime(end, to_min);