X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Futil.c;h=3d6aa200f542e830e77fbe56dc3010dd220a1d83;hb=ee82a6d611fb17b94462b7b2a87b0b5cc3ec1178;hp=55cd265a14b45812ea66a743f39ff139f83303f5;hpb=78e336e0f67c12e154121d293aeaca4d7c15c17a;p=lackey diff --git a/src/util.c b/src/util.c index 55cd265..3d6aa20 100644 --- a/src/util.c +++ b/src/util.c @@ -1,31 +1,35 @@ +/* + * Copyright (C) 2012 Andy Spencer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + /* Time Keeping Bugs Abound! */ #include +#include #include "util.h" -/* Helper functions */ -static int is_leap_year(year_t year) +/* Time functions */ +int is_leap_year(year_t year) { return (year % 400 == 0) ? 1 : (year % 100 == 0) ? 0 : (year % 4 == 0) ? 1 : 0; } -static wday_t day_of_week(year_t year, month_t month, day_t day) -{ - static int tmp[] = {0, 3, 2, 5, 0, 3, - 5, 1, 4, 6, 2, 4}; - if (month < 3) - year--; - int start = year + year / 4 - - year / 100 - + year / 400 - + tmp[month]; - return (start + day) % 7; -} - -/* Time functions */ int days_in_year(year_t year) { return 365 + is_leap_year(year); @@ -47,18 +51,45 @@ int weeks_in_month(year_t year, month_t month) return ((start + days)-1) / 7 + 1; } +wday_t day_of_week(year_t year, month_t month, day_t day) +{ + static int tmp[] = {0, 3, 2, 5, 0, 3, + 5, 1, 4, 6, 2, 4}; + if (month < 3) + year--; + int start = year + year / 4 + - year / 100 + + year / 400 + + tmp[month]; + return (start + day) % 7; +} + wday_t start_of_month(year_t year, month_t month) { return day_of_week(year, month, 1); } +void add_days(year_t *year, month_t *month, day_t *day, int days) +{ + time_t time = mktime(&(struct tm){ + .tm_year = *year-1900, + .tm_mon = *month, + .tm_mday = *day+1, + .tm_hour = 12}); + time += days*24*60*60; + struct tm *tm = localtime(&time); + *year = tm->tm_year+1900; + *month = tm->tm_mon; + *day = tm->tm_mday-1; +} + /* Debug functions */ const char *month_to_str(month_t month) { static const char *map[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; - return map[month]; + return map[month%12]; } const char *month_to_string(month_t month) { @@ -66,27 +97,27 @@ const char *month_to_string(month_t month) { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; - return map[month]; + return map[month%12]; } const char *day_to_st(wday_t day) { static const char *map[] = { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" }; - return map[day]; + return map[day%7]; } const char *day_to_str(wday_t day) { static const char *map[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; - return map[day]; + return map[day%7]; } const char *day_to_string(wday_t day) { static const char *map[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; - return map[day]; + return map[day%7]; } /* Test functions */