src/lib/common/hours.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  hours.c: Game hours determination; is it legal to play now?
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1989
00032  *     Doug Hay, 1998
00033  *     Steve McClure, 1998
00034  *     Markus Armbruster, 2004
00035  */
00036 
00037 #include <config.h>
00038 
00039 #include <ctype.h>
00040 #include <time.h>
00041 #include "misc.h"
00042 #include "optlist.h"
00043 #include "prototypes.h"
00044 
00045 static char *weekday(char *str, int *wday);
00046 static char *daytime(char *str, int *min);
00047 static char *daytime_range(char *str, int *from_min, int *to_min);
00048 
00049 /*
00050  * Is week day WDAY (Sunday is 0) allowed by restriction DAYS?
00051  * If DAYS is not empty, it lists the allowed week day names.  See
00052  * weekday() for syntax.
00053  */
00054 int
00055 is_wday_allowed(int wday, char *days)
00056 {
00057     int wd;
00058 
00059     if (!days || !*days)
00060         return 1;
00061 
00062     while (NULL != (days = weekday(days, &wd)))
00063         if (wd == wday)
00064             return 1;
00065 
00066     return 0;
00067 }
00068 
00069 /*
00070  * Is day time DTIME (minutes since midnight) allowed by restriction TIMES?
00071  * If TIMES is not empty, it lists the allowed day time ranges.  See
00072  * daytime_range() for syntax.
00073  */
00074 int
00075 is_daytime_allowed(int dtime, char *times)
00076 {
00077     int from, to;
00078 
00079     if (!times || !*times)
00080         return 1;
00081 
00082     while (NULL != (times = daytime_range(times, &from, &to)))
00083         if (from <= dtime && dtime < to)
00084             return 1;
00085 
00086     return 0;
00087 }
00088 
00089 /*
00090  * Can the game played at time T?
00091  */
00092 int
00093 gamehours(time_t t)
00094 {
00095     struct tm *tm;
00096 
00097     tm = localtime(&t);
00098     if (!is_wday_allowed(tm->tm_wday, game_days))
00099         return 0;
00100     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
00101 }
00102 
00103 /*
00104  * Parse weekday name in STR.
00105  * On success assign day number (Sunday is 0) to *WDAY and return
00106  * pointer to first character not parsed.
00107  * Else return NULL.
00108  * Abbreviated names are recognized, but not single characters.
00109  * Initial whitespace is ignored.
00110  */
00111 static char *
00112 weekday(char *str, int *wday)
00113 {
00114     /*
00115      * strptime() format " %a" would do fine, but it's XPG and Windows
00116      * doesn't have it.  Besides, Empire accepts more abbreviations.
00117      */
00118     static char *day_name[7] = {
00119         "sunday", "monday", "tuesday", "wednesday",
00120         "thursday", "friday", "saturday" };
00121     int i, j;
00122 
00123     for (; isspace(*str); ++str) ;
00124 
00125     for (i = 0; i < 7; ++i) {
00126         j = 0;
00127         while (str[j] && tolower(str[j]) == day_name[i][j])
00128             ++j;
00129         if (j > 1) {
00130             *wday = i;
00131             return str + j;
00132         }
00133     }
00134 
00135     return NULL;
00136 }
00137 
00138 /*
00139  * Parse day time in STR.
00140  * On success store minutes since midnight in *MIN and return pointer
00141  * to first character not parsed.
00142  * Else return NULL.
00143  * Time format is HOUR:MINUTE.  Initial whitespace is ignored.
00144  */
00145 char *
00146 daytime(char *str, int *min)
00147 {
00148     /*
00149      * strptime() format " %H:%M" would do fine, but it's XPG and
00150      * Windows doesn't have it.
00151      */
00152     char *end;
00153     unsigned long h, m;
00154 
00155     h = strtoul(str, &end, 10);
00156     if (end == str || h > 23)
00157         return NULL;
00158 
00159     if (*end++ != ':')
00160         return NULL;
00161 
00162     str = end;
00163     m = strtoul(str, &end, 10);
00164     if (end == str || m > 59)
00165         return NULL;
00166 
00167     *min = 60 * h + m;
00168     return end;
00169 }
00170 
00171 /*
00172  * Parse a day time range in STR.
00173  * On success store minutes since midnight in *FROM and *TO, return
00174  * pointer to first character not parsed.
00175  * Else return NULL.
00176  * Format is HOUR:MINUTE-HOUR:MINUTE.  Initial whitespace is ignored.
00177  */
00178 char *
00179 daytime_range(char *str, int *from_min, int *to_min)
00180 {
00181     char *end;
00182 
00183     end = daytime(str, from_min);
00184     if (!end)
00185         return NULL;
00186     while (isspace(*end)) ++end;
00187     if (*end++ != '-')
00188         return NULL;
00189     return daytime(end, to_min);
00190 }

Generated on Fri Mar 28 11:01:14 2008 for empserver by  doxygen 1.5.2