include/tkz/tkz_engine.h

filter: Statementstmt 97.7% 260 / 266 Branchbran 86.7% 203 / 234 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.6% 463 / 500 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/zone.xs ↑ Summary xs/rules.xs »
1 #ifndef TKZ_ENGINE_H
2 #define TKZ_ENGINE_H
3  
4 /* The zone engine: the local time type at an instant, the next change
5 * after it, and the instant a wall-clock time names.
6 *
7 * A zone id is an index into tkz_lookup, so a link keeps its own name and
8 * shares its target's data. Everything here is plain C over const tables:
9 * no Perl, no globals, nothing to lock.
10 *
11 * Before a zone's first transition it is in its initial type. From its
12 * last transition on (or always, if it has none) its footer, a POSIX TZ
13 * string, decides. A footer's rule times may be negative or past 24 hours
14 * (RFC 9636), so a year's transitions can fall in the year before or
15 * after it: the footer is worked out over four years around the instant
16 * and the results sorted, rather than assumed to lie in their own year. */
17  
18 static int64_t
19 15275 tkz_floor_div(int64_t a, int64_t b)
20 {
21 15275 int64_t q = a / b;
22 15275 ▶ if ((a % b != 0) && ((a < 0) != (b < 0)))
Branch: 2 branches
BranchTrueFalseTotal
if ((a % b != 0) && ((a < 0) != (b < 0))) 15071 204 15275
if ((a % b != 0) && ((a < 0) != (b < 0))) 2 15069 15071
23 2 q--;
24 15275 return q;
25 }
26  
27 static int
28 41 tkz_is_leap(int64_t y)
29 {
30 41 ▶ return (y % 4 == 0) && (y % 100 != 0 || y % 400 == 0);
Branch: 3 branches
BranchTrueFalseTotal
return (y % 4 == 0) && (y % 100 != 0 || y % 400 == 0); 16 25 41
return (y % 4 == 0) && (y % 100 != 0 || y % 400 == 0); 4 12 16
return (y % 4 == 0) && (y % 100 != 0 || y % 400 == 0); 2 2 4
31 }
32  
33 static int64_t
34 122232 tkz_days_from_civil(int64_t y, unsigned m, unsigned d)
35 {
36 int64_t era, yoe, doy, doe;
37 122232 y -= m <= 2;
38 122232 ▶ era = (y >= 0 ? y : y - 399) / 400;
Branch: 1 branch
BranchTrueFalseTotal
era = (y >= 0 ? y : y - 399) / 400; 0 122232 122232
39 122232 yoe = y - era * 400;
40 122232 ▶ doy = (153 * (int64_t)(m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1;
Branch: 1 branch
BranchTrueFalseTotal
doy = (153 * (int64_t)(m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1; 122152 80 122232
41 122232 doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
42 122232 return era * 146097 + doe - 719468;
43 }
44  
45 static int64_t
46 15275 tkz_year_of_day(int64_t z)
47 {
48 int64_t era, doe, yoe, doy, mp;
49 15275 z += 719468;
50 15275 ▶ era = (z >= 0 ? z : z - 146096) / 146097;
Branch: 1 branch
BranchTrueFalseTotal
era = (z >= 0 ? z : z - 146096) / 146097; 0 15275 15275
51 15275 doe = z - era * 146097;
52 15275 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
53 15275 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
54 15275 mp = (5 * doy + 2) / 153;
55 15275 return yoe + era * 400 + (mp >= 10);
56 }
57  
58 /* Seconds from 1970 to the local midnight the rule names in year y, plus
59 * the rule's time: local seconds, not yet UTC. */
60 static int64_t
61 122220 tkz_rule_local(const tkz_rule *r, int64_t y)
62 {
63 int64_t day;
64 122220 switch (r->kind) {
65 122156 case TKZ_RULE_MONTH: {
66 static const unsigned char dim[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
67 122156 int64_t first = tkz_days_from_civil(y, r->month, 1);
68 122156 ▶ unsigned last = dim[r->month - 1] + (r->month == 2 && tkz_is_leap(y));
Branch: 2 branches
BranchTrueFalseTotal
unsigned last = dim[r->month - 1] + (r->month == 2 && tkz_is_leap(y)); 3 122153 122156
unsigned last = dim[r->month - 1] + (r->month == 2 && tkz_is_leap(y)); 1 2 3
69 122156 unsigned wd1 = (unsigned)((first % 7 + 7 + 4) % 7); /* 0 = Sunday; day 0 was a Thursday */
70 122156 unsigned d = 1 + (r->wday + 7 - wd1) % 7 + (r->week - 1) * 7;
71 150171 ▶ while (d > last)
Branch: 1 branch
BranchTrueFalseTotal
while (d > last) 28015 122156 150171
72 28015 d -= 7;
73 122156 day = first + d - 1;
74 122156 break;
75 }
76 38 case TKZ_RULE_JULIAN:
77 38 day = tkz_days_from_civil(y, 1, 1) + r->day - 1
78 38 ▶ + (tkz_is_leap(y) && r->day >= 60);
Branch: 2 branches
BranchTrueFalseTotal
+ (tkz_is_leap(y) && r->day >= 60); 13 25 38
+ (tkz_is_leap(y) && r->day >= 60); 11 2 13
79 38 break;
80 26 default:
81 26 day = tkz_days_from_civil(y, 1, 1) + r->day;
82 26 break;
83 }
84 122220 return day * 86400 + r->time;
85 }
86  
87 /* The footer's state at t: the type in force, and the next change after
88 * t (TKZ_NO_CHANGE if it has no daylight time). */
89 static uint16_t
90 35698 tkz_footer_at(const tkz_footer *f, int64_t t, int64_t *next)
91 {
92 int64_t at[8], y;
93 uint16_t ty[8], cur;
94 35698 int n = 0, i, j;
95  
96 35698 ▶ if (f->dst_type == 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->dst_type == 0xFFFF) { 20427 15271 35698
97 20427 *next = TKZ_NO_CHANGE;
98 20427 return f->std_type;
99 }
100 15271 y = tkz_year_of_day(tkz_floor_div(t + f->std_utoff, 86400));
101 76355 ▶ for (j = -1; j <= 2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = -1; j <= 2; j++) { 61084 15271 76355
102 61084 at[n] = tkz_rule_local(&f->start, y + j) - f->std_utoff;
103 61084 ty[n++] = f->dst_type;
104 61084 at[n] = tkz_rule_local(&f->end, y + j) - f->dst_utoff;
105 61084 ty[n++] = f->std_type;
106 }
107 /* Insertion sort, stable: a year's end and the next year's start at
108 * the same instant keep that order, so the later one wins. */
109 122168 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 106897 15271 122168
110 106897 int64_t a = at[i];
111 106897 uint16_t b = ty[i];
112 114181 ▶ for (j = i - 1; j >= 0 && at[j] > a; j--) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = i - 1; j >= 0 && at[j] > a; j--) { 112360 1821 114181
for (j = i - 1; j >= 0 && at[j] > a; j--) { 7284 105076 112360
113 7284 at[j + 1] = at[j];
114 7284 ty[j + 1] = ty[j];
115 }
116 106897 at[j + 1] = a;
117 106897 ty[j + 1] = b;
118 }
119 15271 ▶ cur = ty[0] == f->dst_type ? f->std_type : f->dst_type;
Branch: 1 branch
BranchTrueFalseTotal
cur = ty[0] == f->dst_type ? f->std_type : f->dst_type; 13450 1821 15271
120 15271 *next = TKZ_NO_CHANGE;
121 60580 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 60580 0 60580
122 60580 ▶ if (at[i] <= t) {
Branch: 1 branch
BranchTrueFalseTotal
if (at[i] <= t) { 45309 15271 60580
123 45309 cur = ty[i];
124 }
125 else {
126 15271 *next = at[i];
127 15271 break;
128 }
129 }
130 15271 return cur;
131 }
132  
133 static const tkz_zone *
134 70900 tkz_zone_of(int id)
135 {
136 70900 return &tkz_zones[tkz_lookup[id].zone];
137 }
138  
139 /* The type at t, and the instant of the next change after t, or
140 * TKZ_NO_CHANGE. A "change" here is a transition in the table or the footer;
141 * it may leave the offset, DST flag and abbreviation all as they were. */
142 static uint16_t
143 70629 tkz_type_at(int id, int64_t t, int64_t *next)
144 {
145 70629 const tkz_zone *z = tkz_zone_of(id);
146 70629 const int64_t *at = tkz_trans_at + z->first_trans;
147 70629 uint32_t n = z->n_trans, lo, hi;
148  
149 70629 ▶ if (n == 0 || t >= at[n - 1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || t >= at[n - 1]) { 65650 4979 70629
if (n == 0 || t >= at[n - 1]) { 30704 34946 65650
150 35683 ▶ if (z->footer == TKZ_NO_FOOTER) {
Branch: 1 branch
BranchTrueFalseTotal
if (z->footer == TKZ_NO_FOOTER) { 0 35683 35683
151 0 *next = TKZ_NO_CHANGE;
152 0 ▶ return n ? tkz_trans_type[z->first_trans + n - 1] : z->initial_type;
Branch: 1 branch
BranchTrueFalseTotal
return n ? tkz_trans_type[z->first_trans + n - 1] : z->initial_type; 0 0 0
153 }
154 35683 return tkz_footer_at(&tkz_footers[z->footer], t, next);
155 }
156 34946 ▶ if (t < at[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (t < at[0]) { 13131 21815 34946
157 13131 *next = at[0];
158 13131 return z->initial_type;
159 }
160 /* The last transition at or before t: at[lo] <= t < at[hi]. */
161 21815 lo = 0;
162 21815 hi = n - 1;
163 137546 ▶ while (hi - lo > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (hi - lo > 1) { 115731 21815 137546
164 115731 uint32_t mid = lo + (hi - lo) / 2;
165 115731 ▶ if (at[mid] <= t)
Branch: 1 branch
BranchTrueFalseTotal
if (at[mid] <= t) 43871 71860 115731
166 43871 lo = mid;
167 else
168 71860 hi = mid;
169 }
170 21815 *next = at[hi];
171 21815 return tkz_trans_type[z->first_trans + lo];
172 }
173  
174 static int
175 20980 tkz_offset_at(int id, int64_t t, int32_t *utoff, int *isdst, const char **abbr)
176 {
177 int64_t next;
178 const tkz_type *ty;
179 20980 ▶ if (id < 0 || id >= TKZ_LOOKUP_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 20979 1 20980
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 1 20978 20979
180 2 return 0;
181 20978 ty = &tkz_types[tkz_type_at(id, t, &next)];
182 20978 ▶ if (utoff) *utoff = ty->utoff;
Branch: 1 branch
BranchTrueFalseTotal
if (utoff) *utoff = ty->utoff; 20970 8 20978
183 20978 ▶ if (isdst) *isdst = ty->isdst;
Branch: 1 branch
BranchTrueFalseTotal
if (isdst) *isdst = ty->isdst; 20739 239 20978
184 20978 ▶ if (abbr) *abbr = tkz_abbrs + ty->abbr;
Branch: 1 branch
BranchTrueFalseTotal
if (abbr) *abbr = tkz_abbrs + ty->abbr; 20731 247 20978
185 20978 return 1;
186 }
187  
188 /* Whether two types differ in offset, DST flag or abbreviation: what makes
189 * a transition a change. */
190 static int
191 596 tkz_types_differ(uint16_t i, uint16_t j)
192 {
193 596 const tkz_type *a = &tkz_types[i], *b = &tkz_types[j];
194 596 ▶ if (i == j)
Branch: 1 branch
BranchTrueFalseTotal
if (i == j) 3 593 596
195 3 return 0;
196 5 ▶ return a->utoff != b->utoff || a->isdst != b->isdst
Branch: 1 branch
BranchTrueFalseTotal
return a->utoff != b->utoff || a->isdst != b->isdst 0 5 5
197 598 ▶ || strcmp(tkz_abbrs + a->abbr, tkz_abbrs + b->abbr) != 0;
Branch: 2 branches
BranchTrueFalseTotal
|| strcmp(tkz_abbrs + a->abbr, tkz_abbrs + b->abbr) != 0; 5 588 593
|| strcmp(tkz_abbrs + a->abbr, tkz_abbrs + b->abbr) != 0; 0 0 0
198 }
199  
200 /* The next instant after t at which the offset, DST flag or abbreviation
201 * really changes, or TKZ_NO_CHANGE. */
202 static int64_t
203 836 tkz_next_change(int id, int64_t t)
204 {
205 int64_t next;
206 uint16_t now;
207 836 ▶ if (id < 0 || id >= TKZ_LOOKUP_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 836 0 836
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 0 836 836
208 0 return TKZ_NO_CHANGE;
209 836 now = tkz_type_at(id, t, &next);
210 838 ▶ while (next != TKZ_NO_CHANGE) {
Branch: 1 branch
BranchTrueFalseTotal
while (next != TKZ_NO_CHANGE) { 590 248 838
211 int64_t after;
212 590 uint16_t then = tkz_type_at(id, next, &after);
213 590 ▶ if (tkz_types_differ(now, then))
Branch: 1 branch
BranchTrueFalseTotal
if (tkz_types_differ(now, then)) 588 2 590
214 588 return next;
215 2 ▶ if (after <= next)
Branch: 1 branch
BranchTrueFalseTotal
if (after <= next) 0 2 2
216 0 break;
217 2 next = after;
218 }
219 248 return TKZ_NO_CHANGE;
220 }
221  
222 /* The last instant strictly before t at which the footer moves between
223 * standard and daylight time, or TKZ_NO_CHANGE if it has no daylight time.
224 * A rule's time may carry its instant up to a week into the next year, so
225 * the two years before t's are worked out as well as t's own. */
226 static int64_t
227 6 tkz_footer_prev(const tkz_footer *f, int64_t t)
228 {
229 6 int64_t y, a, best = TKZ_NO_CHANGE;
230 int j;
231  
232 6 ▶ if (f->dst_type == 0xFFFF)
Branch: 1 branch
BranchTrueFalseTotal
if (f->dst_type == 0xFFFF) 2 4 6
233 2 return TKZ_NO_CHANGE;
234 4 y = tkz_year_of_day(tkz_floor_div(t + f->std_utoff, 86400));
235 20 ▶ for (j = -2; j <= 1; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = -2; j <= 1; j++) { 16 4 20
236 16 a = tkz_rule_local(&f->start, y + j) - f->std_utoff;
237 16 ▶ if (a < t && (best == TKZ_NO_CHANGE || a > best))
Branch: 3 branches
BranchTrueFalseTotal
if (a < t && (best == TKZ_NO_CHANGE || a > best)) 10 6 16
if (a < t && (best == TKZ_NO_CHANGE || a > best)) 6 4 10
if (a < t && (best == TKZ_NO_CHANGE || a > best)) 6 0 6
238 10 best = a;
239 16 a = tkz_rule_local(&f->end, y + j) - f->dst_utoff;
240 16 ▶ if (a < t && (best == TKZ_NO_CHANGE || a > best))
Branch: 3 branches
BranchTrueFalseTotal
if (a < t && (best == TKZ_NO_CHANGE || a > best)) 8 8 16
if (a < t && (best == TKZ_NO_CHANGE || a > best)) 8 0 8
if (a < t && (best == TKZ_NO_CHANGE || a > best)) 8 0 8
241 8 best = a;
242 }
243 4 return best;
244 }
245  
246 /* The last transition strictly before t, in the table or the footer, or
247 * TKZ_NO_CHANGE. The footer rules only from the table's last transition
248 * on: an instant it gives at or before that one is the table's to answer. */
249 static int64_t
250 7 tkz_trans_prev(int id, int64_t t)
251 {
252 7 const tkz_zone *z = tkz_zone_of(id);
253 7 const int64_t *at = tkz_trans_at + z->first_trans;
254 7 uint32_t n = z->n_trans, lo, hi;
255  
256 7 ▶ if (z->footer != TKZ_NO_FOOTER && (n == 0 || t > at[n - 1])) {
Branch: 3 branches
BranchTrueFalseTotal
if (z->footer != TKZ_NO_FOOTER && (n == 0 || t > at[n - 1])) { 7 0 7
if (z->footer != TKZ_NO_FOOTER && (n == 0 || t > at[n - 1])) { 6 1 7
if (z->footer != TKZ_NO_FOOTER && (n == 0 || t > at[n - 1])) { 5 1 6
257 6 int64_t p = tkz_footer_prev(&tkz_footers[z->footer], t);
258 6 ▶ if (p != TKZ_NO_CHANGE && (n == 0 || p > at[n - 1]))
Branch: 3 branches
BranchTrueFalseTotal
if (p != TKZ_NO_CHANGE && (n == 0 || p > at[n - 1])) 4 2 6
if (p != TKZ_NO_CHANGE && (n == 0 || p > at[n - 1])) 4 0 4
if (p != TKZ_NO_CHANGE && (n == 0 || p > at[n - 1])) 3 1 4
259 3 return p;
260 }
261 4 ▶ if (n == 0 || t <= at[0])
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || t <= at[0]) 3 1 4
if (n == 0 || t <= at[0]) 0 3 3
262 1 return TKZ_NO_CHANGE;
263 /* The last transition before t: at[lo] < t <= at[hi], or hi == n. */
264 3 lo = 0;
265 3 hi = n;
266 23 ▶ while (hi - lo > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (hi - lo > 1) { 20 3 23
267 20 uint32_t mid = lo + (hi - lo) / 2;
268 20 ▶ if (at[mid] < t)
Branch: 1 branch
BranchTrueFalseTotal
if (at[mid] < t) 19 1 20
269 19 lo = mid;
270 else
271 1 hi = mid;
272 }
273 3 return at[lo];
274 }
275  
276 /* The last instant strictly before t at which the offset, DST flag or
277 * abbreviation really changes, or TKZ_NO_CHANGE. */
278 static int64_t
279 7 tkz_prev_change(int id, int64_t t)
280 {
281 int64_t c, next;
282 7 ▶ if (id < 0 || id >= TKZ_LOOKUP_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 7 0 7
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 1 6 7
283 1 return TKZ_NO_CHANGE;
284 7 ▶ while ((c = tkz_trans_prev(id, t)) != TKZ_NO_CHANGE) {
Branch: 1 branch
BranchTrueFalseTotal
while ((c = tkz_trans_prev(id, t)) != TKZ_NO_CHANGE) { 6 1 7
285 6 ▶ if (tkz_types_differ(tkz_type_at(id, c - 1, &next), tkz_type_at(id, c, &next)))
Branch: 1 branch
BranchTrueFalseTotal
if (tkz_types_differ(tkz_type_at(id, c - 1, &next), tkz_type_at(id, c, &next))) 5 1 6
286 5 return c;
287 1 t = c;
288 }
289 1 return TKZ_NO_CHANGE;
290 }
291  
292 /* Every UTC instant whose wall clock reads `local` (seconds since 1970, as
293 * if the wall clock were UTC). Every stretch of constant offset within a
294 * day and a bit either side is tried: an instant t in a stretch with offset
295 * o matches when t + o == local. No offset has ever been more than a day.
296 *
297 * Returns how many there are, 0 (a gap), 1 or 2 (an overlap), or -1 for a
298 * bad id. Two or more fill utc[] with the earliest and the latest. One
299 * fills both slots with it. A gap fills utoff[] with the offsets before
300 * and after it, and utc[] with local read in each. In every case
301 * utc[i] == local - utoff[i]. */
302 static int
303 42991 tkz_resolve_local_all(int id, int64_t local, int64_t utc[2], int32_t utoff[2])
304 {
305 42991 int64_t from = local - 2 * 86400, to = local + 2 * 86400;
306 42991 int64_t t = from;
307 42991 int found = 0, have_gap = 0;
308  
309 42991 ▶ if (id < 0 || id >= TKZ_LOOKUP_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 42991 0 42991
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 2 42989 42991
310 2 return -1;
311 42989 utc[0] = utc[1] = local;
312 42989 utoff[0] = utoff[1] = 0;
313 75674 ▶ while (t <= to) {
Branch: 1 branch
BranchTrueFalseTotal
while (t <= to) { 46129 29545 75674
314 int64_t next, cand;
315 46129 uint16_t ty = tkz_type_at(id, t, &next);
316 46129 int32_t off = tkz_types[ty].utoff;
317 46129 cand = local - off;
318 46129 ▶ if (cand >= t && (next == TKZ_NO_CHANGE || cand < next)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cand >= t && (next == TKZ_NO_CHANGE || cand < next)) { 44055 2074 46129
if (cand >= t && (next == TKZ_NO_CHANGE || cand < next)) { 30613 13442 44055
if (cand >= t && (next == TKZ_NO_CHANGE || cand < next)) { 28531 2082 30613
319 41973 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 41323 650 41973
320 41323 utc[0] = cand;
321 41323 utoff[0] = off;
322 }
323 41973 utc[1] = cand;
324 41973 utoff[1] = off;
325 41973 found++;
326 }
327 4156 ▶ else if (!found && !have_gap && next != TKZ_NO_CHANGE && cand >= next) {
Branch: 4 branches
BranchTrueFalseTotal
else if (!found && !have_gap && next != TKZ_NO_CHANGE && cand >= next) { 3748 408 4156
else if (!found && !have_gap && next != TKZ_NO_CHANGE && cand >= next) { 2082 1666 3748
else if (!found && !have_gap && next != TKZ_NO_CHANGE && cand >= next) { 2082 0 2082
else if (!found && !have_gap && next != TKZ_NO_CHANGE && cand >= next) { 2082 0 2082
328 int64_t after;
329 2082 int32_t noff = tkz_types[tkz_type_at(id, next, &after)].utoff;
330 2082 ▶ if (local - noff < next) {
Branch: 1 branch
BranchTrueFalseTotal
if (local - noff < next) { 1666 416 2082
331 /* next + off <= local < next + noff: the clocks jumped
332 * from next + off to next + noff, over local */
333 1666 have_gap = 1;
334 1666 utoff[0] = off;
335 1666 utoff[1] = noff;
336 1666 utc[0] = local - off;
337 1666 utc[1] = local - noff;
338 }
339 }
340 46129 ▶ if (next == TKZ_NO_CHANGE)
Branch: 1 branch
BranchTrueFalseTotal
if (next == TKZ_NO_CHANGE) 13444 32685 46129
341 13444 break;
342 32685 t = next;
343 }
344 42989 return found > 2 ? 2 : found;
345 }
346  
347 /* The UTC instant of local time `local`: one of resolve_local_all's
348 * readings, picked by gap and overlap. */
349 static int
350 42975 tkz_resolve_local(int id, int64_t local, int gap, int overlap, int64_t *utc)
351 {
352 int64_t u[2];
353 int32_t o[2];
354 42975 int n = tkz_resolve_local_all(id, local, u, o);
355  
356 42975 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 1 42974 42975
357 1 return -1;
358 42974 ▶ if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) { 40660 2314 42974
359 40660 *utc = u[0];
360 40660 return TKZ_LOCAL_EXACT;
361 }
362 2314 ▶ if (n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) { 649 1665 2314
363 649 ▶ *utc = overlap == TKZ_OVERLAP_EARLIER ? u[0] : u[1];
Branch: 1 branch
BranchTrueFalseTotal
*utc = overlap == TKZ_OVERLAP_EARLIER ? u[0] : u[1]; 325 324 649
364 649 return TKZ_LOCAL_OVERLAP;
365 }
366 1665 ▶ if (gap == TKZ_GAP_FORWARD)
Branch: 1 branch
BranchTrueFalseTotal
if (gap == TKZ_GAP_FORWARD) 666 999 1665
367 666 *utc = u[0];
368 999 ▶ else if (gap == TKZ_GAP_BACKWARD)
Branch: 1 branch
BranchTrueFalseTotal
else if (gap == TKZ_GAP_BACKWARD) 666 333 999
369 666 *utc = u[1];
370 1665 return TKZ_LOCAL_GAP;
371 }
372  
373 /* The offset of standard time at t, as the database's types say it: past
374 * the table's last transition, the footer's standard offset; before that,
375 * the offset of the latest type at or before t whose DST flag is clear. The
376 * database decides what is standard: Europe/Dublin's is IST, +01:00, and
377 * its winter GMT is the daylight time, a negative one. A zone that has
378 * been on DST since its first transition gives its initial type's offset.
379 * 0 for a bad id. */
380 static int
381 9 tkz_standard_offset_at(int id, int64_t t, int32_t *utoff)
382 {
383 const tkz_zone *z;
384 const int64_t *at;
385 uint32_t n, i, lo, hi;
386 int64_t next;
387 uint16_t ty;
388  
389 9 ▶ if (id < 0 || id >= TKZ_LOOKUP_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 9 0 9
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 1 8 9
390 1 return 0;
391 8 z = tkz_zone_of(id);
392 8 at = tkz_trans_at + z->first_trans;
393 8 n = z->n_trans;
394 8 ▶ if (z->footer != TKZ_NO_FOOTER && (n == 0 || t >= at[n - 1])) {
Branch: 3 branches
BranchTrueFalseTotal
if (z->footer != TKZ_NO_FOOTER && (n == 0 || t >= at[n - 1])) { 8 0 8
if (z->footer != TKZ_NO_FOOTER && (n == 0 || t >= at[n - 1])) { 8 0 8
if (z->footer != TKZ_NO_FOOTER && (n == 0 || t >= at[n - 1])) { 6 2 8
395 6 *utoff = tkz_footers[z->footer].std_utoff;
396 6 return 1;
397 }
398 2 ty = tkz_type_at(id, t, &next);
399 2 ▶ if (!tkz_types[ty].isdst) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkz_types[ty].isdst) { 1 1 2
400 1 *utoff = tkz_types[ty].utoff;
401 1 return 1;
402 }
403 /* Back through the transitions at or before t: at[i - 1] <= t < at[i]. */
404 1 lo = 0;
405 1 hi = n;
406 8 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 7 1 8
407 7 uint32_t mid = lo + (hi - lo) / 2;
408 7 ▶ if (at[mid] <= t)
Branch: 1 branch
BranchTrueFalseTotal
if (at[mid] <= t) 5 2 7
409 5 lo = mid + 1;
410 else
411 2 hi = mid;
412 }
413 2 ▶ for (i = lo; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = lo; i > 0; i--) { 2 0 2
414 2 ty = tkz_trans_type[z->first_trans + i - 1];
415 2 ▶ if (!tkz_types[ty].isdst) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkz_types[ty].isdst) { 1 1 2
416 1 *utoff = tkz_types[ty].utoff;
417 1 return 1;
418 }
419 }
420 0 *utoff = tkz_types[z->initial_type].utoff;
421 0 return 1;
422 }
423  
424 /* Case-blind binary search of the names. */
425 static int
426 540446 tkz_strncasecmp_nul(const char *a, const char *b, size_t blen)
427 {
428 size_t i;
429 4039152 ▶ for (i = 0; i < blen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < blen; i++) { 3972565 66587 4039152
430 3972565 int ca = (unsigned char)a[i], cb = (unsigned char)b[i];
431 3972565 ▶ if (ca >= 'A' && ca <= 'Z') ca += 32;
Branch: 2 branches
BranchTrueFalseTotal
if (ca >= 'A' && ca <= 'Z') ca += 32; 3547739 424826 3972565
if (ca >= 'A' && ca <= 'Z') ca += 32; 984051 2563688 3547739
432 3972565 ▶ if (cb >= 'A' && cb <= 'Z') cb += 32;
Branch: 2 branches
BranchTrueFalseTotal
if (cb >= 'A' && cb <= 'Z') cb += 32; 3547550 425015 3972565
if (cb >= 'A' && cb <= 'Z') cb += 32; 995588 2551962 3547550
433 3972565 ▶ if (ca == 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ca == 0) return -1; 1218 3971347 3972565
434 3971347 ▶ if (ca != cb) return ca - cb;
Branch: 1 branch
BranchTrueFalseTotal
if (ca != cb) return ca - cb; 472641 3498706 3971347
435 }
436 66587 return a[blen] == 0 ? 0 : 1;
437 }
438  
439 static int
440 65469 tkz_lookup_name(const char *name, size_t len)
441 {
442 65469 int lo = 0, hi = TKZ_LOOKUP_COUNT - 1;
443 65469 ▶ if (len == 0 || memchr(name, 0, len))
Branch: 2 branches
BranchTrueFalseTotal
if (len == 0 || memchr(name, 0, len)) 65468 1 65469
if (len == 0 || memchr(name, 0, len)) 1 65467 65468
444 2 return -1;
445 540461 ▶ while (lo <= hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo <= hi) { 540446 15 540461
446 540446 int mid = lo + (hi - lo) / 2;
447 540446 int c = tkz_strncasecmp_nul(tkz_names + tkz_lookup[mid].name, name, len);
448 540446 ▶ if (c == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) 65452 474994 540446
449 65452 return mid;
450 474994 ▶ if (c < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) 244844 230150 474994
451 244844 lo = mid + 1;
452 else
453 230150 hi = mid - 1;
454 }
455 15 return -1;
456 }
457  
458 static const char *
459 312 tkz_id_name(int id)
460 {
461 312 ▶ return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? NULL : tkz_names + tkz_lookup[id].name;
Branch: 2 branches
BranchTrueFalseTotal
return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? NULL : tkz_names + tkz_lookup[id].name; 311 1 312
return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? NULL : tkz_names + tkz_lookup[id].name; 310 1 311
462 }
463  
464 static const char *
465 256 tkz_id_canonical(int id)
466 {
467 256 ▶ return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? NULL : tkz_names + tkz_zone_of(id)->name;
Branch: 2 branches
BranchTrueFalseTotal
return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? NULL : tkz_names + tkz_zone_of(id)->name; 256 0 256
return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? NULL : tkz_names + tkz_zone_of(id)->name; 256 0 256
468 }
469  
470 static int
471 2 tkz_id_is_link(int id)
472 {
473 2 ▶ return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? 0 : tkz_lookup[id].is_link;
Branch: 2 branches
BranchTrueFalseTotal
return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? 0 : tkz_lookup[id].is_link; 2 0 2
return (id < 0 || id >= TKZ_LOOKUP_COUNT) ? 0 : tkz_lookup[id].is_link; 2 0 2
474 }
475  
476 /* The ISO 3166 code of the country a name belongs to, or NULL. */
477 static const char *
478 8 tkz_country_of(int id)
479 {
480 uint8_t c;
481 8 ▶ if (id < 0 || id >= TKZ_LOOKUP_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 6 2 8
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 0 6 6
482 2 return NULL;
483 6 c = tkz_lookup_country[id];
484 6 ▶ return c == TKZ_NO_COUNTRY ? NULL : tkz_countries[c].code;
Branch: 1 branch
BranchTrueFalseTotal
return c == TKZ_NO_COUNTRY ? NULL : tkz_countries[c].code; 5 1 6
485 }
486  
487 /* The Windows name of the zone a name follows, or NULL. */
488 static const char *
489 8 tkz_windows_name(int id)
490 {
491 uint16_t w;
492 8 ▶ if (id < 0 || id >= TKZ_LOOKUP_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 7 1 8
if (id < 0 || id >= TKZ_LOOKUP_COUNT) 1 6 7
493 2 return NULL;
494 6 w = tkz_zone_windows[tkz_lookup[id].zone];
495 6 ▶ return w == TKZ_NO_WINDOWS ? NULL : tkz_windows_names + tkz_windows[w].name;
Branch: 1 branch
BranchTrueFalseTotal
return w == TKZ_NO_WINDOWS ? NULL : tkz_windows_names + tkz_windows[w].name; 5 1 6
496 }
497  
498 #endif
« xs/zone.xs ↑ Summary xs/rules.xs »