]> Pileus Git - lackey/blob - src/util.c
Make month view interactive
[lackey] / src / util.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 "util.h"
24
25 /* Time functions */
26 int is_leap_year(year_t year)
27 {
28         return (year % 400 == 0) ? 1 :
29                (year % 100 == 0) ? 0 :
30                (year % 4   == 0) ? 1 : 0;
31 }
32
33 int days_in_year(year_t year)
34 {
35         return 365 + is_leap_year(year);
36 }
37
38 int days_in_month(year_t year, month_t month)
39 {
40         static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
41         int days = mdays[month];
42         if (month == FEB)
43                 days += is_leap_year(year);
44         return days;
45 }
46
47 int weeks_in_month(year_t year, month_t month)
48 {
49         int start = start_of_month(year, month);
50         int days  = days_in_month(year, month);
51         return ((start + days)-1) / 7 + 1;
52 }
53
54 wday_t day_of_week(year_t year, month_t month, day_t day)
55 {
56         static int tmp[] = {0, 3, 2, 5, 0, 3,
57                             5, 1, 4, 6, 2, 4};
58         if (month < MAR)
59                 year--;
60         int start = year + year / 4
61                          - year / 100
62                          + year / 400
63                          + tmp[month];
64         return (start + day + 1) % 7;
65 }
66
67 wday_t start_of_month(year_t year, month_t month)
68 {
69         return day_of_week(year, month, 0);
70 }
71
72 void add_days(year_t *year, month_t *month, day_t *day, int days)
73 {
74         time_t time = mktime(&(struct tm){
75                         .tm_year = *year-1900,
76                         .tm_mon  = *month,
77                         .tm_mday = *day+1,
78                         .tm_hour = 12});
79         time  += days*24*60*60;
80         struct tm *tm = localtime(&time);
81         *year  = tm->tm_year+1900;
82         *month = tm->tm_mon;
83         *day   = tm->tm_mday-1;
84 }
85
86 void add_months(year_t *year, month_t *month, int months)
87 {
88         int total = *year*12 + *month + months;
89         *year  = total / 12;
90         *month = total % 12;
91 }
92
93 /* Debug functions */
94 const char *month_to_str(month_t month)
95 {
96         static const char *map[] =
97                 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
98                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
99         return map[month%12];
100 }
101 const char *month_to_string(month_t month)
102 {
103         static const char *map[] =
104                 { "January",   "February", "March",    "April",
105                   "May",       "June",     "July",     "August",
106                   "September", "October",  "November", "December" };
107         return map[month%12];
108 }
109
110 const char *day_to_st(wday_t day)
111 {
112         static const char *map[] =
113                 { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
114         return map[day%7];
115 }
116 const char *day_to_str(wday_t day)
117 {
118         static const char *map[] =
119                 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
120         return map[day%7];
121 }
122 const char *day_to_string(wday_t day)
123 {
124         static const char *map[] =
125                 { "Sunday",   "Monday", "Tuesday", "Wednesday",
126                   "Thursday", "Friday", "Saturday" };
127         return map[day%7];
128 }
129
130 /* Test functions */
131 void test_time(void)
132 {
133         printf("Info\n");
134         printf("  Year Month     Start Weeks Days\n");
135         for (int y = 2012; y <= 2012; y++)
136         for (int m = JAN;  m <= DEC;  m++) {
137                 printf("  %-5d",  y);
138                 printf("  %-10s", month_to_string(m));
139                 printf("  %-6s",  day_to_str(start_of_month(y,m)));
140                 printf("  %-6d",  weeks_in_month(y,m));
141                 printf("  %-2d",  days_in_month(y,m));
142                 printf("\n");
143         }
144 }