]> Pileus Git - lackey/blob - src/date.c
Add event selection to day and week views
[lackey] / src / date.c
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 /* Time Keeping Bugs Abound! */
19
20 #include <stdio.h>
21 #include <time.h>
22
23 #include "date.h"
24
25 /* Global data */
26 date_t  NOW;
27 date_t  SEL;
28
29 /* Initialize */
30 void date_init(void)
31 {
32         time_t     sec = time(NULL);
33         struct tm *tm  = localtime(&sec);
34
35         SEL.year  = tm->tm_year+1900;
36         SEL.month = tm->tm_mon;
37         SEL.day   = tm->tm_mday-1;
38 }
39
40 void date_sync(void)
41 {
42         time_t     sec = time(NULL);
43         struct tm *tm  = localtime(&sec);
44
45         NOW.year  = tm->tm_year+1900;
46         NOW.month = tm->tm_mon;
47         NOW.day   = tm->tm_mday-1;
48         NOW.hour  = tm->tm_hour;
49         NOW.min   = tm->tm_min;
50         NOW.sec   = tm->tm_sec;
51 }
52
53 /* Time functions */
54 int is_leap_year(year_t year)
55 {
56         return (year % 400 == 0) ? 1 :
57                (year % 100 == 0) ? 0 :
58                (year % 4   == 0) ? 1 : 0;
59 }
60
61 int days_in_year(year_t year)
62 {
63         return 365 + is_leap_year(year);
64 }
65
66 int days_in_month(year_t year, month_t month)
67 {
68         static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
69         int days = mdays[month];
70         if (month == FEB)
71                 days += is_leap_year(year);
72         return days;
73 }
74
75 int weeks_in_month(year_t year, month_t month)
76 {
77         int start = start_of_month(year, month);
78         int days  = days_in_month(year, month);
79         return ((start + days)-1) / 7 + 1;
80 }
81
82 wday_t day_of_week(year_t year, month_t month, day_t day)
83 {
84         static int tmp[] = {0, 3, 2, 5, 0, 3,
85                             5, 1, 4, 6, 2, 4};
86         if (month < MAR)
87                 year--;
88         int start = year + year / 4
89                          - year / 100
90                          + year / 400
91                          + tmp[month];
92         return (start + day + 1) % 7;
93 }
94
95 wday_t start_of_month(year_t year, month_t month)
96 {
97         return day_of_week(year, month, 0);
98 }
99
100 void add_days(year_t *year, month_t *month, day_t *day, int days)
101 {
102         time_t time = mktime(&(struct tm){
103                         .tm_year = *year-1900,
104                         .tm_mon  = *month,
105                         .tm_mday = *day+1,
106                         .tm_hour = 12});
107         time  += days*24*60*60;
108         struct tm *tm = localtime(&time);
109         *year  = tm->tm_year+1900;
110         *month = tm->tm_mon;
111         *day   = tm->tm_mday-1;
112 }
113
114 void add_months(year_t *year, month_t *month, int months)
115 {
116         int total = *year*12 + *month + months;
117         *year  = total / 12;
118         *month = total % 12;
119 }
120
121 stamp_t get_time(date_t *date)
122 {
123         return mktime(&(struct tm){
124                 .tm_year = date->year-1900,
125                 .tm_mon  = date->month,
126                 .tm_mday = date->day+1,
127                 .tm_hour = date->hour,
128                 .tm_min  = date->min});
129 }
130
131 int get_mins(date_t *start, date_t *end)
132 {
133         return (get_time(end)-get_time(start))/60;
134 }
135
136 int compare(date_t *a, date_t *b)
137 {
138         int rval = a->year  < b->year  ? -1 : a->year  > b->year  ? 1 :
139                    a->month < b->month ? -1 : a->month > b->month ? 1 :
140                    a->day   < b->day   ? -1 : a->day   > b->day   ? 1 :
141                    a->hour  < b->hour  ? -1 : a->hour  > b->hour  ? 1 :
142                    a->min   < b->min   ? -1 : a->min   > b->min   ? 1 : 0;
143         return rval;
144 }
145
146 int same_day(date_t *a, date_t *b)
147 {
148         return a->year  == b->year  &&
149                a->month == b->month &&
150                a->day   == b->day;
151 }
152
153 int before(date_t *start, int year, int month, int day, int hour, int min)
154 {
155         return compare(start, &(date_t){year, month, day, hour, min}) < 0;
156 }
157
158 int all_day(date_t *start, date_t *end)
159 {
160         date_t test = *start;
161         add_days(&test.year, &test.month, &test.day, 1);
162         return compare(&test, end) <= 0;
163 }
164
165 int no_date(date_t *date)
166 {
167         return date->year == 0;
168 }
169
170 /* Debug functions */
171 const char *month_to_str(month_t month)
172 {
173         static const char *map[] =
174                 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
175                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
176         return map[month%12];
177 }
178 const char *month_to_string(month_t month)
179 {
180         static const char *map[] =
181                 { "January",   "February", "March",    "April",
182                   "May",       "June",     "July",     "August",
183                   "September", "October",  "November", "December" };
184         return map[month%12];
185 }
186
187 const char *day_to_st(wday_t day)
188 {
189         static const char *map[] =
190                 { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
191         return map[day%7];
192 }
193 const char *day_to_str(wday_t day)
194 {
195         static const char *map[] =
196                 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
197         return map[day%7];
198 }
199 const char *day_to_string(wday_t day)
200 {
201         static const char *map[] =
202                 { "Sunday",   "Monday", "Tuesday", "Wednesday",
203                   "Thursday", "Friday", "Saturday" };
204         return map[day%7];
205 }
206
207 /* Test functions */
208 void date_test(void)
209 {
210         printf("Info\n");
211         printf("  Year Month     Start Weeks Days\n");
212         for (int y = 2012; y <= 2012; y++)
213         for (int m = JAN;  m <= DEC;  m++) {
214                 printf("  %-5d",  y);
215                 printf("  %-10s", month_to_string(m));
216                 printf("  %-6s",  day_to_str(start_of_month(y,m)));
217                 printf("  %-6d",  weeks_in_month(y,m));
218                 printf("  %-2d",  days_in_month(y,m));
219                 printf("\n");
220         }
221 }