]> Pileus Git - lackey/blob - src/util.h
e814dfdcd6e4b8d2a4fb2523791989b161300ad7
[lackey] / src / util.h
1 /*
2  * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /* Macros */
19 #define MIN(a,b) ((a) < (b) ? (a) : (b))
20 #define MAX(a,b) ((a) > (b) ? (a) : (b))
21 #define ROUND(x) ((int)((x)+0.5))
22 #define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0]))
23
24 /* Time types */
25 typedef int year_t;
26 typedef int day_t;
27
28 typedef enum {
29         JAN =  0,
30         FEB =  1,
31         MAR =  2,
32         APR =  3,
33         MAY =  4,
34         JUN =  5,
35         JUL =  6,
36         AUG =  7,
37         SEP =  8,
38         OCT =  9,
39         NOV = 10,
40         DEC = 11,
41 } month_t;
42
43 typedef enum {
44         SUN = 0,
45         MON = 1,
46         TUE = 2,
47         WED = 3,
48         THU = 4,
49         FRI = 5,
50         SAT = 6,
51 } wday_t;
52
53 /* Time functions */
54 int is_leap_year(year_t year);
55 int days_in_year(year_t year);
56 int days_in_month(year_t year, month_t month);
57 int weeks_in_month(year_t year, month_t month);
58 wday_t day_of_week(year_t year, month_t month, day_t day);
59 wday_t start_of_month(year_t year, month_t month);
60 day_t start_of_week(year_t year, month_t month, day_t day);
61 void add_days(year_t *year, month_t *month, day_t *day, int days);
62
63 /* Time to string functions */
64 const char *month_to_str(month_t month);
65 const char *month_to_string(month_t month);
66 const char *day_to_st(wday_t day);
67 const char *day_to_str(wday_t day);
68 const char *day_to_string(wday_t day);
69
70 /* Tests */
71 void test_time(void);