1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015 Derick Rethans
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "timelib.h"
26 #include "timelib_private.h"
27
28 static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
29 static int m_table_leap[13] = { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
30
positive_mod(timelib_sll x,timelib_sll y)31 static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
32 {
33 timelib_sll tmp;
34
35 tmp = x % y;
36 if (tmp < 0) {
37 tmp += y;
38 }
39
40 return tmp;
41 }
42
century_value(timelib_sll j)43 static timelib_sll century_value(timelib_sll j)
44 {
45 return 6 - positive_mod(j, 4) * 2;
46 }
47
timelib_day_of_week_ex(timelib_sll y,timelib_sll m,timelib_sll d,int iso)48 static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
49 {
50 timelib_sll c1, y1, m1, dow;
51
52 /* Only valid for Gregorian calendar, commented out as we don't handle
53 * Julian calendar. We just return the 'wrong' day of week to be
54 * consistent. */
55 c1 = century_value(y / 100);
56 y1 = positive_mod(y, 100);
57 m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
58 dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
59 if (iso) {
60 if (dow == 0) {
61 dow = 7;
62 }
63 }
64 return dow;
65 }
66
timelib_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)67 timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
68 {
69 return timelib_day_of_week_ex(y, m, d, 0);
70 }
71
timelib_iso_day_of_week(timelib_sll y,timelib_sll m,timelib_sll d)72 timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
73 {
74 return timelib_day_of_week_ex(y, m, d, 1);
75 }
76
77 /* jan feb mar apr may jun jul aug sep oct nov dec */
78 static int d_table_common[13] = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
79 static int d_table_leap[13] = { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
80 static int ml_table_common[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
81 static int ml_table_leap[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
82
timelib_day_of_year(timelib_sll y,timelib_sll m,timelib_sll d)83 timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
84 {
85 return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
86 }
87
timelib_days_in_month(timelib_sll y,timelib_sll m)88 timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
89 {
90 return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
91 }
92
timelib_isoweek_from_date(timelib_sll y,timelib_sll m,timelib_sll d,timelib_sll * iw,timelib_sll * iy)93 void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
94 {
95 int y_leap, prev_y_leap, doy, jan1weekday, weekday;
96
97 y_leap = timelib_is_leap(y);
98 prev_y_leap = timelib_is_leap(y-1);
99 doy = timelib_day_of_year(y, m, d) + 1;
100 if (y_leap && m > 2) {
101 doy++;
102 }
103 jan1weekday = timelib_day_of_week(y, 1, 1);
104 weekday = timelib_day_of_week(y, m, d);
105 if (weekday == 0) weekday = 7;
106 if (jan1weekday == 0) jan1weekday = 7;
107 /* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
108 if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
109 *iy = y - 1;
110 if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
111 *iw = 53;
112 } else {
113 *iw = 52;
114 }
115 } else {
116 *iy = y;
117 }
118 /* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
119 if (*iy == y) {
120 int i;
121
122 i = y_leap ? 366 : 365;
123 if ((i - (doy - y_leap)) < (4 - weekday)) {
124 *iy = y + 1;
125 *iw = 1;
126 return;
127 }
128 }
129 /* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
130 if (*iy == y) {
131 int j;
132
133 j = doy + (7 - weekday) + (jan1weekday - 1);
134 *iw = j / 7;
135 if (jan1weekday > 4) {
136 *iw -= 1;
137 }
138 }
139 }
140
timelib_isodate_from_date(timelib_sll y,timelib_sll m,timelib_sll d,timelib_sll * iy,timelib_sll * iw,timelib_sll * id)141 void timelib_isodate_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iy, timelib_sll *iw, timelib_sll *id)
142 {
143 timelib_isoweek_from_date(y, m, d, iw, iy);
144 *id = timelib_day_of_week_ex(y, m, d, 1);
145 }
146
timelib_daynr_from_weeknr_ex(timelib_sll iy,timelib_sll iw,timelib_sll id,timelib_sll * y)147 static timelib_sll timelib_daynr_from_weeknr_ex(timelib_sll iy, timelib_sll iw, timelib_sll id, timelib_sll *y)
148 {
149 timelib_sll dow, day;
150
151 /* Figure out the dayofweek for y-1-1 */
152 dow = timelib_day_of_week(iy, 1, 1);
153 /* then use that to figure out the offset for day 1 of week 1 */
154 day = 0 - (dow > 4 ? dow - 7 : dow);
155 /* and adjust the year to the natural year if we need to */
156 *y = (iw == 1 && day < 0 && id < dow) ? iy - 1 : iy;
157
158 /* Add weeks and days */
159 return day + ((iw - 1) * 7) + id;
160 }
161
timelib_daynr_from_weeknr(timelib_sll iy,timelib_sll iw,timelib_sll id)162 timelib_sll timelib_daynr_from_weeknr(timelib_sll iy, timelib_sll iw, timelib_sll id)
163 {
164 timelib_sll dummy_iso_year;
165
166 return timelib_daynr_from_weeknr_ex(iy, iw, id, &dummy_iso_year);
167 }
168
timelib_date_from_isodate(timelib_sll iy,timelib_sll iw,timelib_sll id,timelib_sll * y,timelib_sll * m,timelib_sll * d)169 void timelib_date_from_isodate(timelib_sll iy, timelib_sll iw, timelib_sll id, timelib_sll *y, timelib_sll *m, timelib_sll *d)
170 {
171 timelib_sll daynr = timelib_daynr_from_weeknr_ex(iy, iw, id, y) + 1;
172 int *table;
173
174 *m = 0;
175
176 if (daynr <= 0) {
177 *y += 1;
178 }
179
180 if (timelib_is_leap(*y)) {
181 table = ml_table_leap;
182 if (daynr > 366) {
183 *y += 1;
184 daynr -= 366;
185 }
186 } else {
187 table = ml_table_common;
188 if (daynr > 365) {
189 *y += 1;
190 daynr -= 365;
191 }
192 }
193
194 do {
195 daynr -= table[*m];
196 (*m)++;
197 } while (daynr > table[*m]);
198
199 if (daynr <= 0) {
200 daynr += 31;
201 *y -= 1;
202 *m = 12;
203 }
204
205 *d = daynr;
206 }
207
timelib_valid_time(timelib_sll h,timelib_sll i,timelib_sll s)208 int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
209 {
210 if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
211 return 0;
212 }
213 return 1;
214 }
215
timelib_valid_date(timelib_sll y,timelib_sll m,timelib_sll d)216 int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
217 {
218 if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
219 return 0;
220 }
221 return 1;
222 }
223 #if 0
224 int main(void)
225 {
226 printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
227 printf("dow = %d\n", timelib_day_of_week(2005, 2, 19)); /* 6 */
228 }
229 #endif
230