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