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! */
20 #define _POSIX_C_SOURCE 200112L
38 /* Sync current time */
43 SEL.month = NOW.month;
47 void date_config(const char *group, const char *name, const char *key, const char *value)
49 if (match(group, "date") && match(key, "timezone") && value)
50 setenv("TZ", get_string(value), 1);
59 int is_leap_year(year_t year)
61 return (year % 400 == 0) ? 1 :
62 (year % 100 == 0) ? 0 :
63 (year % 4 == 0) ? 1 : 0;
66 int days_in_year(year_t year)
68 return 365 + is_leap_year(year);
71 int days_in_month(year_t year, month_t month)
73 static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
74 int days = mdays[month];
76 days += is_leap_year(year);
80 int weeks_in_month(year_t year, month_t month)
82 int start = start_of_month(year, month);
83 int days = days_in_month(year, month);
84 return ((start + days)-1) / 7 + 1;
87 wday_t day_of_week(year_t year, month_t month, day_t day)
89 static int tmp[] = {0, 3, 2, 5, 0, 3,
93 int start = year + year / 4
97 return (start + day + 1) % 7;
100 wday_t start_of_month(year_t year, month_t month)
102 return day_of_week(year, month, 0);
105 void add_days(year_t *year, month_t *month, day_t *day, int days)
113 stamp_t stamp = get_stamp(date);
114 date_t then = get_date(stamp + days*24*60*60);
120 void add_months(year_t *year, month_t *month, int months)
122 int total = *year*12 + *month + months;
128 date_t get_date(stamp_t stamp)
130 time_t t = stamp ? stamp : time(NULL);
131 struct tm *tm = localtime(&t);
133 .year = tm->tm_year+1900,
135 .day = tm->tm_mday-1,
143 stamp_t get_stamp(date_t date)
146 .tm_year = date.year-1900,
147 .tm_mon = date.month,
148 .tm_mday = date.day+1,
149 .tm_hour = date.hour,
153 time_t t = mktime(&tm);
157 int get_mins(date_t *start, date_t *end)
159 return (get_stamp(*end)-get_stamp(*start))/60;
162 int compare(date_t *a, date_t *b)
164 int rval = a->year < b->year ? -1 : a->year > b->year ? 1 :
165 a->month < b->month ? -1 : a->month > b->month ? 1 :
166 a->day < b->day ? -1 : a->day > b->day ? 1 :
167 a->hour < b->hour ? -1 : a->hour > b->hour ? 1 :
168 a->min < b->min ? -1 : a->min > b->min ? 1 : 0;
172 int same_day(date_t *a, date_t *b)
174 return a->year == b->year &&
175 a->month == b->month &&
179 int before(date_t *start, int year, int month, int day, int hour, int min)
181 return compare(start, &(date_t){year, month, day, hour, min}) < 0;
184 int all_day(date_t *start, date_t *end)
186 date_t test = *start;
187 add_days(&test.year, &test.month, &test.day, 1);
188 return compare(&test, end) <= 0;
191 int no_date(date_t *date)
193 return date->year == 0;
196 /* Debug functions */
197 const char *month_to_str(month_t month)
199 static const char *map[] =
200 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
201 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
202 return map[month%12];
204 const char *month_to_string(month_t month)
206 static const char *map[] =
207 { "January", "February", "March", "April",
208 "May", "June", "July", "August",
209 "September", "October", "November", "December" };
210 return map[month%12];
213 const char *day_to_st(wday_t day)
215 static const char *map[] =
216 { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
219 const char *day_to_str(wday_t day)
221 static const char *map[] =
222 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
225 const char *day_to_string(wday_t day)
227 static const char *map[] =
228 { "Sunday", "Monday", "Tuesday", "Wednesday",
229 "Thursday", "Friday", "Saturday" };
236 setenv("TZ", "US/Central", 1);
238 time_t timet = time(NULL);
239 date_t date = get_date(timet);
240 stamp_t stamp = get_stamp(date);
243 printf(" time %ld\n", timet);
244 printf(" stamp %lld\n", stamp);
245 printf(" date %04d-%02d-%02d %02d:%02d %02ds\n",
246 date.year, date.month, date.day,
247 date.hour, date.min, date.sec);
250 printf(" Year Month Start Weeks Days\n");
251 for (int y = 2012; y <= 2012; y++)
252 for (int m = JAN; m <= DEC; m++) {
254 printf(" %-10s", month_to_string(m));
255 printf(" %-6s", day_to_str(start_of_month(y,m)));
256 printf(" %-6d", weeks_in_month(y,m));
257 printf(" %-2d", days_in_month(y,m));