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