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;
47 int is_leap_year(year_t year)
49 return (year % 400 == 0) ? 1 :
50 (year % 100 == 0) ? 0 :
51 (year % 4 == 0) ? 1 : 0;
54 int days_in_year(year_t year)
56 return 365 + is_leap_year(year);
59 int days_in_month(year_t year, month_t month)
61 static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
62 int days = mdays[month];
64 days += is_leap_year(year);
68 int weeks_in_month(year_t year, month_t month)
70 int start = start_of_month(year, month);
71 int days = days_in_month(year, month);
72 return ((start + days)-1) / 7 + 1;
75 wday_t day_of_week(year_t year, month_t month, day_t day)
77 static int tmp[] = {0, 3, 2, 5, 0, 3,
81 int start = year + year / 4
85 return (start + day + 1) % 7;
88 wday_t start_of_month(year_t year, month_t month)
90 return day_of_week(year, month, 0);
93 void add_days(year_t *year, month_t *month, day_t *day, int days)
95 time_t time = mktime(&(struct tm){
96 .tm_year = *year-1900,
100 time += days*24*60*60;
101 struct tm *tm = localtime(&time);
102 *year = tm->tm_year+1900;
104 *day = tm->tm_mday-1;
107 void add_months(year_t *year, month_t *month, int months)
109 int total = *year*12 + *month + months;
114 stamp_t get_time(date_t *date)
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});
124 int get_mins(date_t *start, date_t *end)
126 return (get_time(end)-get_time(start))/60;
129 int before(date_t *start, int year, int month, int day, int hour, int min)
131 int rval = start->year < year ? 1 : start->year > year ? 0 :
132 start->month < month ? 1 : start->month > month? 0 :
133 start->day < day ? 1 : start->day > day ? 0 :
134 start->hour < hour ? 1 : start->hour > hour ? 0 :
135 start->min < min ? 1 : start->min > min ? 0 : 0;
139 /* Debug functions */
140 const char *month_to_str(month_t month)
142 static const char *map[] =
143 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
144 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
145 return map[month%12];
147 const char *month_to_string(month_t month)
149 static const char *map[] =
150 { "January", "February", "March", "April",
151 "May", "June", "July", "August",
152 "September", "October", "November", "December" };
153 return map[month%12];
156 const char *day_to_st(wday_t day)
158 static const char *map[] =
159 { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
162 const char *day_to_str(wday_t day)
164 static const char *map[] =
165 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
168 const char *day_to_string(wday_t day)
170 static const char *map[] =
171 { "Sunday", "Monday", "Tuesday", "Wednesday",
172 "Thursday", "Friday", "Saturday" };
180 printf(" Year Month Start Weeks Days\n");
181 for (int y = 2012; y <= 2012; y++)
182 for (int m = JAN; m <= DEC; m++) {
184 printf(" %-10s", month_to_string(m));
185 printf(" %-6s", day_to_str(start_of_month(y,m)));
186 printf(" %-6d", weeks_in_month(y,m));
187 printf(" %-2d", days_in_month(y,m));