This source file includes following definitions.
- positive_mod
- century_value
- timelib_day_of_week_ex
- timelib_day_of_week
- timelib_iso_day_of_week
- timelib_day_of_year
- timelib_days_in_month
- timelib_isoweek_from_date
- timelib_daynr_from_weeknr
- timelib_valid_time
- timelib_valid_date
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 #include "timelib.h"
26
27 static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
28 static int m_table_leap[13] = { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
29
30 static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
31 {
32 timelib_sll tmp;
33
34 tmp = x % y;
35 if (tmp < 0) {
36 tmp += y;
37 }
38
39 return tmp;
40 }
41
42 static timelib_sll century_value(timelib_sll j)
43 {
44 return 6 - positive_mod(j, 4) * 2;
45 }
46
47 static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
48 {
49 timelib_sll c1, y1, m1, dow;
50
51
52
53
54 c1 = century_value(y / 100);
55 y1 = positive_mod(y, 100);
56 m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
57 dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
58 if (iso) {
59 if (dow == 0) {
60 dow = 7;
61 }
62 }
63 return dow;
64 }
65
66 timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
67 {
68 return timelib_day_of_week_ex(y, m, d, 0);
69 }
70
71 timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
72 {
73 return timelib_day_of_week_ex(y, m, d, 1);
74 }
75
76
77 static int d_table_common[13] = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
78 static int d_table_leap[13] = { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
79 static int ml_table_common[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
80 static int ml_table_leap[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
81
82 timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
83 {
84 return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
85 }
86
87 timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
88 {
89 return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
90 }
91
92 void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
93 {
94 int y_leap, prev_y_leap, doy, jan1weekday, weekday;
95
96 y_leap = timelib_is_leap(y);
97 prev_y_leap = timelib_is_leap(y-1);
98 doy = timelib_day_of_year(y, m, d) + 1;
99 if (y_leap && m > 2) {
100 doy++;
101 }
102 jan1weekday = timelib_day_of_week(y, 1, 1);
103 weekday = timelib_day_of_week(y, m, d);
104 if (weekday == 0) weekday = 7;
105 if (jan1weekday == 0) jan1weekday = 7;
106
107 if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
108 *iy = y - 1;
109 if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
110 *iw = 53;
111 } else {
112 *iw = 52;
113 }
114 } else {
115 *iy = y;
116 }
117
118 if (*iy == y) {
119 int i;
120
121 i = y_leap ? 366 : 365;
122 if ((i - (doy - y_leap)) < (4 - weekday)) {
123 *iy = y + 1;
124 *iw = 1;
125 return;
126 }
127 }
128
129 if (*iy == y) {
130 int j;
131
132 j = doy + (7 - weekday) + (jan1weekday - 1);
133 *iw = j / 7;
134 if (jan1weekday > 4) {
135 *iw -= 1;
136 }
137 }
138 }
139
140 timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
141 {
142 timelib_sll dow, day;
143
144
145 dow = timelib_day_of_week(y, 1, 1);
146
147 day = 0 - (dow > 4 ? dow - 7 : dow);
148
149
150 return day + ((w - 1) * 7) + d;
151 }
152
153 int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
154 {
155 if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
156 return 0;
157 }
158 return 1;
159 }
160
161 int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
162 {
163 if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
164 return 0;
165 }
166 return 1;
167 }
168 #if 0
169 int main(void)
170 {
171 printf("dow = %d\n", timelib_day_of_week(1978, 12, 22));
172 printf("dow = %d\n", timelib_day_of_week(2005, 2, 19));
173 }
174 #endif