]> git.pond.sub.org Git - empserver/blob - src/lib/common/hours.c
deity.h is redundant, remove it.
[empserver] / src / lib / common / hours.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  hours.c: Game hours determination; is it legal to play now?
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Doug Hay, 1998
33  *     Steve McClure, 1998
34  */
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include "misc.h"
39 #include "nat.h"
40 #include "tel.h"
41 #include "proto.h"
42 #include "com.h"
43 #include "keyword.h"
44 #include "file.h"
45 #include "common.h"
46 #include "optlist.h"
47
48 #include <time.h>
49
50 /*
51  * returns true if game can be played now.
52  * Sets the number of minutes until the hours
53  * function must be re-called.
54  */
55 int
56 gamehours(time_t now, int *hour)
57 {
58     register s_char *bp;
59     register struct tm *tm;
60     int day;
61     int curtime;
62     int okday[7];
63     int tomorrow;
64
65     tm = localtime(&now);
66     curtime = tm->tm_min + tm->tm_hour * 60;
67     bp = game_days;
68     if (*bp != 0) {
69         for (day = 0; day < 7; day++)
70             okday[day] = 0;
71         while (NULL != (bp = kw_parse(CF_WEEKDAY, bp, &day)))
72             okday[day] = 1;
73     } else {
74         for (day = 0; day < 7; day++)
75             okday[day] = 1;
76     }
77     if (!okday[tm->tm_wday])
78         return 0;
79     bp = game_hours;
80     if (*bp != 0) {
81         while (NULL != (bp = kw_parse(CF_TIMERANGE, bp, hour)))
82             if (curtime >= hour[0] && curtime < hour[1])
83                 break;
84         if (bp == 0)
85             return 0;
86     } else {
87         hour[0] = 0;
88         hour[1] = 24 * 60;
89     }
90     tomorrow = tm->tm_wday + 1;
91     if (tomorrow >= 7)
92         tomorrow = 0;
93     return 1;
94 }