Line data Source code
1 : /*
2 : * The MIT License (MIT)
3 : *
4 : * Copyright (c) 2015-2019 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 :
31 0 : static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
32 : {
33 : timelib_sll tmp;
34 :
35 0 : tmp = x % y;
36 0 : if (tmp < 0) {
37 0 : tmp += y;
38 : }
39 :
40 0 : return tmp;
41 : }
42 :
43 0 : static timelib_sll century_value(timelib_sll j)
44 : {
45 0 : return 6 - positive_mod(j, 4) * 2;
46 : }
47 :
48 0 : 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 0 : c1 = century_value(positive_mod(y, 400) / 100);
56 0 : y1 = positive_mod(y, 100);
57 0 : m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
58 0 : dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
59 0 : if (iso) {
60 0 : if (dow == 0) {
61 0 : dow = 7;
62 : }
63 : }
64 0 : return dow;
65 : }
66 :
67 0 : timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
68 : {
69 0 : return timelib_day_of_week_ex(y, m, d, 0);
70 : }
71 :
72 0 : timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
73 : {
74 0 : 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 :
83 0 : timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
84 : {
85 0 : return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
86 : }
87 :
88 0 : timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
89 : {
90 0 : return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
91 : }
92 :
93 0 : 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 0 : y_leap = timelib_is_leap(y);
98 0 : prev_y_leap = timelib_is_leap(y-1);
99 0 : doy = timelib_day_of_year(y, m, d) + 1;
100 0 : if (y_leap && m > 2) {
101 0 : doy++;
102 : }
103 0 : jan1weekday = timelib_day_of_week(y, 1, 1);
104 0 : weekday = timelib_day_of_week(y, m, d);
105 0 : if (weekday == 0) weekday = 7;
106 0 : if (jan1weekday == 0) jan1weekday = 7;
107 : /* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
108 0 : if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
109 0 : *iy = y - 1;
110 0 : if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
111 0 : *iw = 53;
112 : } else {
113 0 : *iw = 52;
114 : }
115 : } else {
116 0 : *iy = y;
117 : }
118 : /* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
119 0 : if (*iy == y) {
120 : int i;
121 :
122 0 : i = y_leap ? 366 : 365;
123 0 : if ((i - (doy - y_leap)) < (4 - weekday)) {
124 0 : *iy = y + 1;
125 0 : *iw = 1;
126 0 : return;
127 : }
128 : }
129 : /* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
130 0 : if (*iy == y) {
131 : int j;
132 :
133 0 : j = doy + (7 - weekday) + (jan1weekday - 1);
134 0 : *iw = j / 7;
135 0 : if (jan1weekday > 4) {
136 0 : *iw -= 1;
137 : }
138 : }
139 : }
140 :
141 0 : 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 0 : timelib_isoweek_from_date(y, m, d, iw, iy);
144 0 : *id = timelib_day_of_week_ex(y, m, d, 1);
145 0 : }
146 :
147 0 : timelib_sll timelib_daynr_from_weeknr(timelib_sll iy, timelib_sll iw, timelib_sll id)
148 : {
149 : timelib_sll dow, day;
150 :
151 : /* Figure out the dayofweek for y-1-1 */
152 0 : dow = timelib_day_of_week(iy, 1, 1);
153 : /* then use that to figure out the offset for day 1 of week 1 */
154 0 : day = 0 - (dow > 4 ? dow - 7 : dow);
155 :
156 : /* Add weeks and days */
157 0 : return day + ((iw - 1) * 7) + id;
158 : }
159 :
160 0 : void timelib_date_from_isodate(timelib_sll iy, timelib_sll iw, timelib_sll id, timelib_sll *y, timelib_sll *m, timelib_sll *d)
161 : {
162 0 : timelib_sll daynr = timelib_daynr_from_weeknr(iy, iw, id) + 1;
163 : int *table;
164 : bool is_leap_year;
165 :
166 : // Invariant: is_leap_year == timelib_is_leap(*y)
167 0 : *y = iy;
168 0 : is_leap_year = timelib_is_leap(*y);
169 :
170 : // Establish invariant that daynr >= 0
171 0 : while (daynr <= 0) {
172 0 : *y -= 1;
173 0 : daynr += (is_leap_year = timelib_is_leap(*y)) ? 366 : 365;
174 : }
175 :
176 : // Establish invariant that daynr <= number of days in *yr
177 0 : while (daynr > (is_leap_year ? 366 : 365)) {
178 0 : daynr -= is_leap_year ? 366 : 365;
179 0 : *y += 1;
180 0 : is_leap_year = timelib_is_leap(*y);
181 : }
182 :
183 0 : table = is_leap_year ? ml_table_leap : ml_table_common;
184 :
185 : // Establish invariant that daynr <= number of days in *m
186 0 : *m = 1;
187 0 : while (daynr > table[*m]) {
188 0 : daynr -= table[*m];
189 0 : *m += 1;
190 : }
191 :
192 0 : *d = daynr;
193 0 : }
194 :
195 0 : int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
196 : {
197 0 : if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
198 0 : return 0;
199 : }
200 0 : return 1;
201 : }
202 :
203 0 : int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
204 : {
205 0 : if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
206 0 : return 0;
207 : }
208 0 : return 1;
209 : }
210 : #if 0
211 : int main(void)
212 : {
213 : printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
214 : printf("dow = %d\n", timelib_day_of_week(2005, 2, 19)); /* 6 */
215 : }
216 : #endif
|