OpenCoverage

mktime.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/lib/mktime.c
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4typedef long int long_int;-
5-
6-
7-
8_Static_assert (0x7fffffff <= ((long_int) (! (! ((long_int) 0 < (long_int) -1)) ? (long_int) -1 : ((((long_int) 1 << ((sizeof (long_int) * 8) - 2)) - 1) * 2 + 1))) / 3 / 366 / 24 / 60 / 60, "verify (" "INT_MAX <= TYPE_MAXIMUM (long_int) / 3 / 366 / 24 / 60 / 60" ")");-
9static long_int-
10shr (long_int a, int b)-
11{-
12 long_int one = 1;-
13 return
never executed: return (-one >> 1 == -1 ? a >> b : a / (one << b) - (a % (one << b) < 0));
(-one >> 1 == -1
never executed: return (-one >> 1 == -1 ? a >> b : a / (one << b) - (a % (one << b) < 0));
0
14 ? a >> b
never executed: return (-one >> 1 == -1 ? a >> b : a / (one << b) - (a % (one << b) < 0));
0
15 : a / (one << b) - (a % (one << b) < 0));
never executed: return (-one >> 1 == -1 ? a >> b : a / (one << b) - (a % (one << b) < 0));
0
16}-
17-
18-
19-
20static long_int const mktime_min-
21 = (((! ((time_t) 0 < (time_t) -1)) && ((time_t) ~ ((time_t) (! (! ((time_t) 0 < (time_t) -1)) ? (time_t) -1 : ((((time_t) 1 << ((sizeof (time_t) * 8) - 2)) - 1) * 2 + 1)))) < ((long_int) ~ ((long_int) (! (! ((long_int) 0 < (long_int) -1)) ? (long_int) -1 : ((((long_int) 1 << ((sizeof (long_int) * 8) - 2)) - 1) * 2 + 1)))))-
22 ? ((long_int) ~ ((long_int) (! (! ((long_int) 0 < (long_int) -1)) ? (long_int) -1 : ((((long_int) 1 << ((sizeof (long_int) * 8) - 2)) - 1) * 2 + 1)))) : ((time_t) ~ ((time_t) (! (! ((time_t) 0 < (time_t) -1)) ? (time_t) -1 : ((((time_t) 1 << ((sizeof (time_t) * 8) - 2)) - 1) * 2 + 1)))));-
23static long_int const mktime_max-
24 = (((long_int) (! (! ((long_int) 0 < (long_int) -1)) ? (long_int) -1 : ((((long_int) 1 << ((sizeof (long_int) * 8) - 2)) - 1) * 2 + 1))) < ((time_t) (! (! ((time_t) 0 < (time_t) -1)) ? (time_t) -1 : ((((time_t) 1 << ((sizeof (time_t) * 8) - 2)) - 1) * 2 + 1)))-
25 ? ((long_int) (! (! ((long_int) 0 < (long_int) -1)) ? (long_int) -1 : ((((long_int) 1 << ((sizeof (long_int) * 8) - 2)) - 1) * 2 + 1))) : ((time_t) (! (! ((time_t) 0 < (time_t) -1)) ? (time_t) -1 : ((((time_t) 1 << ((sizeof (time_t) * 8) - 2)) - 1) * 2 + 1))));-
26-
27_Static_assert (((time_t) 1.5 == 1), "verify (" "TYPE_IS_INTEGER (time_t)" ")");-
28-
29-
30-
31_Static_assert (1900 % 100 == 0, "verify (" "TM_YEAR_BASE % 100 == 0" ")");-
32-
33-
34static -
35 _Bool-
36-
37leapyear (long_int year)-
38{-
39-
40-
41 return
never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3)));
0
42 ((year & 3) == 0
never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3)));
0
43 && (year % 100 != 0
never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3)));
0
44 || ((year / 100) & 3) == (- (1900 / 100) & 3)));
never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3)));
0
45}-
46-
47-
48-
49static-
50-
51const unsigned short int __mon_yday[2][13] =-
52 {-
53-
54 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },-
55-
56 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }-
57 };-
58-
59-
60-
61-
62static -
63 _Bool-
64-
65isdst_differ (int a, int b)-
66{-
67 return
never executed: return (!a != !b) && (0 <= a) && (0 <= b);
(!a != !b) && (0 <= a) && (0 <= b);
never executed: return (!a != !b) && (0 <= a) && (0 <= b);
0
68}-
69static long_int-
70ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1,-
71 int year0, int yday0, int hour0, int min0, int sec0)-
72{-
73 _Static_assert (-1 / 2 == 0, "verify (" "-1 / 2 == 0" ")");-
74-
75-
76-
77 int a4 = shr (year1, 2) + shr (1900, 2) - ! (year1 & 3);-
78 int b4 = shr (year0, 2) + shr (1900, 2) - ! (year0 & 3);-
79 int a100 = a4 / 25 - (a4 % 25 < 0);-
80 int b100 = b4 / 25 - (b4 % 25 < 0);-
81 int a400 = shr (a100, 2);-
82 int b400 = shr (b100, 2);-
83 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);-
84-
85-
86 long_int years = year1 - year0;-
87 long_int days = 365 * years + yday1 - yday0 + intervening_leap_days;-
88 long_int hours = 24 * days + hour1 - hour0;-
89 long_int minutes = 60 * hours + min1 - min0;-
90 long_int seconds = 60 * minutes + sec1 - sec0;-
91 return
never executed: return seconds;
seconds;
never executed: return seconds;
0
92}-
93-
94-
95-
96static long_int-
97long_int_avg (long_int a, long_int b)-
98{-
99 return
never executed: return shr (a, 1) + shr (b, 1) + ((a | b) & 1);
shr (a, 1) + shr (b, 1) + ((a | b) & 1);
never executed: return shr (a, 1) + shr (b, 1) + ((a | b) & 1);
0
100}-
101static long_int-
102guess_time_tm (long_int year, long_int yday, int hour, int min, int sec,-
103 long_int t, const struct tm *tp)-
104{-
105 if (tp
tpDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
106 {-
107 long_int result;-
108 long_int d = ydhms_diff (year, yday, hour, min, sec,-
109 tp->tm_year, tp->tm_yday,-
110 tp->tm_hour, tp->tm_min, tp->tm_sec);-
111 if (! __builtin_add_overflow (t, d, &result)
! __builtin_ad...t, d, &result)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
112 return
never executed: return result;
result;
never executed: return result;
0
113 }
never executed: end of block
0
114-
115-
116-
117-
118-
119-
120 return
never executed: return (t < long_int_avg (mktime_min, mktime_max) ? (t <= mktime_min + 1 ? t + 1 : mktime_min) : (mktime_max - 1 <= t ? t - 1 : mktime_max));
(t < long_int_avg (mktime_min, mktime_max)
never executed: return (t < long_int_avg (mktime_min, mktime_max) ? (t <= mktime_min + 1 ? t + 1 : mktime_min) : (mktime_max - 1 <= t ? t - 1 : mktime_max));
0
121 ? (t <= mktime_min + 1 ? t + 1 : mktime_min)
never executed: return (t < long_int_avg (mktime_min, mktime_max) ? (t <= mktime_min + 1 ? t + 1 : mktime_min) : (mktime_max - 1 <= t ? t - 1 : mktime_max));
0
122 : (mktime_max - 1 <= t ? t - 1 : mktime_max));
never executed: return (t < long_int_avg (mktime_min, mktime_max) ? (t <= mktime_min + 1 ? t + 1 : mktime_min) : (mktime_max - 1 <= t ? t - 1 : mktime_max));
0
123}-
124-
125-
126-
127-
128static struct tm *-
129convert_time (struct tm *(*convert) (const time_t *, struct tm *),-
130 long_int t, struct tm *tm)-
131{-
132 time_t x = t;-
133 return
never executed: return convert (&x, tm);
convert (&x, tm);
never executed: return convert (&x, tm);
0
134}-
135-
136-
137-
138-
139-
140static struct tm *-
141ranged_convert (struct tm *(*convert) (const time_t *, struct tm *),-
142 long_int *t, struct tm *tp)-
143{-
144 struct tm *r;-
145 if (*
*t < mktime_minDescription
TRUEnever evaluated
FALSEnever evaluated
t < mktime_min
*t < mktime_minDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
146 *
never executed: *t = mktime_min;
t = mktime_min;
never executed: *t = mktime_min;
0
147 else if (mktime_max < *t
mktime_max < *tDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
148 *
never executed: *t = mktime_max;
t = mktime_max;
never executed: *t = mktime_max;
0
149 r = convert_time (convert, *t, tp);-
150-
151 if (!r
!rDescription
TRUEnever evaluated
FALSEnever evaluated
&& *
*tDescription
TRUEnever evaluated
FALSEnever evaluated
t
*tDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
152 {-
153 long_int bad = *t;-
154 long_int ok = 0;-
155-
156-
157-
158-
159 while (-
160 1-
161 )-
162 {-
163 long_int mid = long_int_avg (ok, bad);-
164 if (mid != ok
mid != okDescription
TRUEnever evaluated
FALSEnever evaluated
&& mid != bad
mid != badDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
165 break;
never executed: break;
0
166 r = convert_time (convert, mid, tp);-
167 if (r
rDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
168 ok = mid;
never executed: ok = mid;
0
169 else-
170 bad = mid;
never executed: bad = mid;
0
171 }-
172-
173 if (!r
!rDescription
TRUEnever evaluated
FALSEnever evaluated
&& ok
okDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
174 {-
175-
176-
177 r = convert_time (convert, ok, tp);-
178 }
never executed: end of block
0
179 }
never executed: end of block
0
180-
181 return
never executed: return r;
r;
never executed: return r;
0
182}-
183-
184-
185-
186-
187-
188-
189-
190time_t-
191mktime_internal (struct tm *tp,-
192 struct tm *(*convert) (const time_t *, struct tm *),-
193 mktime_offset_t *offset)-
194{-
195 long_int t, gt, t0, t1, t2, dt;-
196 struct tm tm;-
197-
198-
199-
200-
201-
202 int remaining_probes = 6;-
203-
204-
205-
206 int sec = tp->tm_sec;-
207 int min = tp->tm_min;-
208 int hour = tp->tm_hour;-
209 int mday = tp->tm_mday;-
210 int mon = tp->tm_mon;-
211 int year_requested = tp->tm_year;-
212 int isdst = tp->tm_isdst;-
213-
214-
215 int dst2;-
216-
217-
218 int mon_remainder = mon % 12;-
219 int negative_mon_remainder = mon_remainder < 0;-
220 int mon_years = mon / 12 - negative_mon_remainder;-
221 long_int lyear_requested = year_requested;-
222 long_int year = lyear_requested + mon_years;-
223-
224-
225-
226-
227-
228-
229 int mon_yday = ((__mon_yday[leapyear (year)]-
230 [mon_remainder + 12 * negative_mon_remainder])-
231 - 1);-
232 long_int lmday = mday;-
233 long_int yday = mon_yday + lmday;-
234-
235 int negative_offset_guess;-
236-
237 int sec_requested = sec;-
238-
239 if (1)-
240 {-
241-
242-
243 if (sec < 0
sec < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
244 sec = 0;
never executed: sec = 0;
0
245 if (59 < sec
59 < secDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
246 sec = 59;
never executed: sec = 59;
0
247 }
never executed: end of block
0
248-
249-
250-
251-
252 __builtin_sub_overflow (0, *offset, &negative_offset_guess);-
253 t0 = ydhms_diff (year, yday, hour, min, sec,-
254 1970 - 1900, 0, 0, 0, negative_offset_guess);-
255-
256-
257-
258 for (t = t1 = t2 = t0, dst2 = 0;-
259 (
(gt = guess_ti...tm)), t != gt)Description
TRUEnever evaluated
FALSEnever evaluated
gt = guess_time_tm (year, yday, hour, min, sec, t,
(gt = guess_ti...tm)), t != gt)Description
TRUEnever evaluated
FALSEnever evaluated
0
260 ranged_convert (convert, &t, &tm)),
(gt = guess_ti...tm)), t != gt)Description
TRUEnever evaluated
FALSEnever evaluated
0
261 t != gt)
(gt = guess_ti...tm)), t != gt)Description
TRUEnever evaluated
FALSEnever evaluated
;
0
262 t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0)-
263 if (t == t1
t == t1Description
TRUEnever evaluated
FALSEnever evaluated
&& t != t2
t != t2Description
TRUEnever evaluated
FALSEnever evaluated
0
264 && (tm.tm_isdst < 0
tm.tm_isdst < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
265 || (
(isdst < 0 ? d...m_isdst != 0))Description
TRUEnever evaluated
FALSEnever evaluated
isdst < 0
isdst < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
266 ? dst2 <= (tm.tm_isdst != 0)
(isdst < 0 ? d...m_isdst != 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
267 : (isdst != 0) != (tm.tm_isdst != 0))
(isdst < 0 ? d...m_isdst != 0))Description
TRUEnever evaluated
FALSEnever evaluated
))
0
268 goto
never executed: goto offset_found;
offset_found;
never executed: goto offset_found;
0
269 else if (--
--remaining_probes == 0Description
TRUEnever evaluated
FALSEnever evaluated
remaining_probes == 0
--remaining_probes == 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
270 return
never executed: return -1;
-1;
never executed: return -1;
0
271-
272-
273-
274 if (isdst_differ (isdst, tm.tm_isdst)
isdst_differ (..., tm.tm_isdst)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
275 {-
276 int stride = 601200;-
277-
278-
279-
280-
281-
282-
283 int duration_max = 536454000;-
284-
285-
286-
287 int delta_bound = duration_max / 2 + stride;-
288-
289 int delta, direction;-
290-
291 for (delta = stride; delta < delta_bound
delta < delta_boundDescription
TRUEnever evaluated
FALSEnever evaluated
; delta += stride)
0
292 for (direction = -1; direction <= 1
direction <= 1Description
TRUEnever evaluated
FALSEnever evaluated
; direction += 2)
0
293 {-
294 long_int ot;-
295 if (! __builtin_add_overflow (t, delta * direction, &ot)
! __builtin_ad...irection, &ot)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
296 {-
297 struct tm otm;-
298 ranged_convert (convert, &ot, &otm);-
299 if (! isdst_differ (isdst, otm.tm_isdst)
! isdst_differ... otm.tm_isdst)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
300 {-
301-
302-
303 t = guess_time_tm (year, yday, hour, min, sec, ot, &otm);-
304 ranged_convert (convert, &t, &tm);-
305 goto
never executed: goto offset_found;
offset_found;
never executed: goto offset_found;
0
306 }-
307 }
never executed: end of block
0
308 }
never executed: end of block
0
309 }
never executed: end of block
0
310-
311 offset_found:
code before this statement never executed: offset_found:
0
312-
313-
314-
315 __builtin_sub_overflow (t, t0, &dt);-
316 __builtin_sub_overflow (dt, negative_offset_guess, offset);-
317-
318 if (1 && sec_requested != tm.tm_sec
sec_requested != tm.tm_secDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
319 {-
320-
321-
322 long_int sec_adjustment = sec == 0
sec == 0Description
TRUEnever evaluated
FALSEnever evaluated
&& tm.tm_sec == 60
tm.tm_sec == 60Description
TRUEnever evaluated
FALSEnever evaluated
;
0
323 sec_adjustment -= sec;-
324 sec_adjustment += sec_requested;-
325 if (__builtin_add_overflow (t, sec_adjustment, &t)
__builtin_add_...djustment, &t)Description
TRUEnever evaluated
FALSEnever evaluated
0
326 || ! (mktime_min <= t
mktime_min <= tDescription
TRUEnever evaluated
FALSEnever evaluated
&& t <= mktime_max
t <= mktime_maxDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
327 || ! convert_time (convert, t, &tm)
! convert_time...nvert, t, &tm)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
328 return
never executed: return -1;
-1;
never executed: return -1;
0
329 }
never executed: end of block
0
330-
331 *tp = tm;-
332 return
never executed: return t;
t;
never executed: return t;
0
333}-
Switch to Source codePreprocessed file

Generated by Squish Coco 4.1.2