2 * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
18 /* Time Keeping Bugs Abound! */
33 time_t sec = time(NULL);
34 struct tm *tm = localtime(&sec);
36 YEAR = tm->tm_year+1900;
42 int is_leap_year(year_t year)
44 return (year % 400 == 0) ? 1 :
45 (year % 100 == 0) ? 0 :
46 (year % 4 == 0) ? 1 : 0;
49 int days_in_year(year_t year)
51 return 365 + is_leap_year(year);
54 int days_in_month(year_t year, month_t month)
56 static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
57 int days = mdays[month];
59 days += is_leap_year(year);
63 int weeks_in_month(year_t year, month_t month)
65 int start = start_of_month(year, month);
66 int days = days_in_month(year, month);
67 return ((start + days)-1) / 7 + 1;
70 wday_t day_of_week(year_t year, month_t month, day_t day)
72 static int tmp[] = {0, 3, 2, 5, 0, 3,
76 int start = year + year / 4
80 return (start + day + 1) % 7;
83 wday_t start_of_month(year_t year, month_t month)
85 return day_of_week(year, month, 0);
88 void add_days(year_t *year, month_t *month, day_t *day, int days)
90 time_t time = mktime(&(struct tm){
91 .tm_year = *year-1900,
95 time += days*24*60*60;
96 struct tm *tm = localtime(&time);
97 *year = tm->tm_year+1900;
102 void add_months(year_t *year, month_t *month, int months)
104 int total = *year*12 + *month + months;
109 stamp_t get_time(date_t *date)
111 return mktime(&(struct tm){
112 .tm_year = date->year-1900,
113 .tm_mon = date->month,
114 .tm_mday = date->day+1,
115 .tm_hour = date->hour,
116 .tm_min = date->min});
119 int get_mins(date_t *start, date_t *end)
121 return (get_time(end)-get_time(start))/60;
124 int compare(date_t *a, date_t *b)
126 int rval = a->year < b->year ? -1 : a->year > b->year ? 1 :
127 a->month < b->month ? -1 : a->month > b->month ? 1 :
128 a->day < b->day ? -1 : a->day > b->day ? 1 :
129 a->hour < b->hour ? -1 : a->hour > b->hour ? 1 :
130 a->min < b->min ? -1 : a->min > b->min ? 1 : 0;
134 int before(date_t *start, int year, int month, int day, int hour, int min)
136 return compare(start, &(date_t){year, month, day, hour, min}) < 0;
139 int all_day(date_t *start, date_t *end)
141 date_t test = *start;
142 add_days(&test.year, &test.month, &test.day, 1);
143 return compare(&test, end) <= 0;
146 int no_date(date_t *date)
148 return date->year == 0;
151 /* Debug functions */
152 const char *month_to_str(month_t month)
154 static const char *map[] =
155 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
156 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
157 return map[month%12];
159 const char *month_to_string(month_t month)
161 static const char *map[] =
162 { "January", "February", "March", "April",
163 "May", "June", "July", "August",
164 "September", "October", "November", "December" };
165 return map[month%12];
168 const char *day_to_st(wday_t day)
170 static const char *map[] =
171 { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
174 const char *day_to_str(wday_t day)
176 static const char *map[] =
177 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
180 const char *day_to_string(wday_t day)
182 static const char *map[] =
183 { "Sunday", "Monday", "Tuesday", "Wednesday",
184 "Thursday", "Friday", "Saturday" };
192 printf(" Year Month Start Weeks Days\n");
193 for (int y = 2012; y <= 2012; y++)
194 for (int m = JAN; m <= DEC; m++) {
196 printf(" %-10s", month_to_string(m));
197 printf(" %-6s", day_to_str(start_of_month(y,m)));
198 printf(" %-6d", weeks_in_month(y,m));
199 printf(" %-2d", days_in_month(y,m));