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