00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00051
00052
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
00071
00072
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
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
00105
00106
00107
00108
00109
00110
00111 static char *
00112 weekday(char *str, int *wday)
00113 {
00114
00115
00116
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
00140
00141
00142
00143
00144
00145 char *
00146 daytime(char *str, int *min)
00147 {
00148
00149
00150
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
00173
00174
00175
00176
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 }