dtl_posix.h

filter: Statementstmt 72.8% 153 / 210 Branchbran 59.8% 91 / 152 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.4% 244 / 362 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« DateTime-Lite.xs ↑ Summary
1 /*---------------------------------------------------------------------------
2 * dtl_posix.h - POSIX TZ footer parser for DateTime::Lite
3 *
4 * Derived from tzcode2026a/localtime.c and tzcode2026a/private.h,
5 * which are in the public domain:
6 * "Unless specified below, all files in the tz code and data are in the
7 * public domain." (tzcode2026a/LICENSE)
8 *
9 * Adaptations for DateTime::Lite XS:
10 * - Extracted only the functions required to evaluate a POSIX TZ footer
11 * string for a single UTC timestamp: is_digit, getzname, getqzname,
12 * getnum, getsecs, getoffset, getrule, transtime.
13 * - All functions prefixed with dtl_ to avoid symbol collisions.
14 * - ATTRIBUTE_PURE_114833 and unreachable() replaced with portable
15 * equivalents safe under any C89/C99/C11 compiler.
16 * - Static linkage retained throughout; nothing is exported.
17 * - No dynamic allocation, no system calls, no global state.
18 * - Added dtl_posix_tz_lookup(): the single public entry point used by the
19 * XS layer. Given a UTC Unix timestamp and a POSIX TZ string, such as
20 * "EST5EDT,M3.2.0,M11.1.0", it returns the UTC offset in seconds, the
21 * DST flag, and the time-zone abbreviation.
22 *
23 * This file is included once, directly into DateTime-Lite.xs.
24 *---------------------------------------------------------------------------*/
25  
26 #ifndef DTL_POSIX_H
27 #define DTL_POSIX_H
28  
29 #include <stdint.h> /* int_fast32_t */
30 #include <string.h> /* memcpy */
31 #include <stddef.h> /* ptrdiff_t */
32 #include <limits.h>
33  
34 /*---------------------------------------------------------------------------
35 * Portability shims
36 *---------------------------------------------------------------------------*/
37  
38 /* Portable no-return hint for unreachable code paths.
39 * The default is a no-op; the compiler may warn about missing returns
40 * but the code is correct. */
41 #ifndef dtl_unreachable
42 /* __builtin_unreachable() requires GCC 4.5+ (April 2010) or clang. On
43 * older GCC such as GCC 4.4 (Strawberry Perl 5.12 on MSWin32), it is
44 * absent and using it produces an "undefined reference" link error.
45 * Falling back to a no-op is safe: the compiler may emit a warning
46 * about missing returns but the generated code remains correct. */
47 # if defined(__clang__)
48 # if __has_builtin(__builtin_unreachable)
49 # define dtl_unreachable() __builtin_unreachable()
50 # else
51 # define dtl_unreachable() ((void)0)
52 # endif
53 # elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
54 # define dtl_unreachable() __builtin_unreachable()
55 # else
56 # define dtl_unreachable() ((void)0)
57 # endif
58 #endif
59  
60 /*---------------------------------------------------------------------------
61 * Constants (from tzcode private.h, public domain)
62 *---------------------------------------------------------------------------*/
63 enum
64 {
65 DTL_SECSPERMIN = 60,
66 DTL_MINSPERHOUR = 60,
67 DTL_SECSPERHOUR = DTL_SECSPERMIN * DTL_MINSPERHOUR,
68 DTL_HOURSPERDAY = 24,
69 DTL_DAYSPERWEEK = 7,
70 DTL_DAYSPERNYEAR = 365,
71 DTL_DAYSPERLYEAR = DTL_DAYSPERNYEAR + 1,
72 DTL_MONSPERYEAR = 12,
73 DTL_EPOCH_YEAR = 1970
74 };
75  
76 #define DTL_SECSPERDAY ((int_fast32_t) DTL_SECSPERHOUR * DTL_HOURSPERDAY)
77  
78 /* Maximum length of a TZ abbreviation, per POSIX (we follow tzcode). */
79 #define DTL_TZNAME_MAXIMUM 254
80  
81 /* Default DST rule used when footer has a DST name but no explicit rule.
82 * This matches the tzcode default: US Eastern DST rules (spring forward
83 * second Sunday in March, fall back first Sunday in November). */
84 #define DTL_TZDEFRULESTRING ",M3.2.0,M11.1.0"
85  
86 /*---------------------------------------------------------------------------
87 * isleap (from tzcode private.h, public domain)
88 *---------------------------------------------------------------------------*/
89 #define dtl_isleap(y) \
90 (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
91  
92 /*---------------------------------------------------------------------------
93 * Month and year length tables (from tzcode localtime.c, public domain)
94 *---------------------------------------------------------------------------*/
95 static const int dtl_mon_lengths[2][DTL_MONSPERYEAR] =
96 {
97 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
98 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
99 };
100  
101 static const int dtl_year_lengths[2] =
102 {
103 DTL_DAYSPERNYEAR,
104 DTL_DAYSPERLYEAR
105 };
106  
107 /*---------------------------------------------------------------------------
108 * struct dtl_rule (derived from tzcode localtime.c, public domain)
109 *
110 * Encodes one half of a DST transition rule (start or end).
111 *---------------------------------------------------------------------------*/
112  
113 enum dtl_r_type
114 {
115 DTL_JULIAN_DAY, /* Jn = Julian day 1-365 (no leap day) */
116 DTL_DAY_OF_YEAR, /* n = day 0-365 (leap day counted) */
117 DTL_MONTH_NTH_DAY_OF_WEEK /* Mm.w.d = month m, week w (1-5), day d */
118 };
119  
120 struct dtl_rule
121 {
122 enum dtl_r_type r_type; /* which form of rule */
123 int r_day; /* day number */
124 int r_week; /* week number (Mm.w.d only) */
125 int r_mon; /* month number (Mm.w.d only) */
126 int_fast32_t r_time; /* transition wall-clock time in seconds */
127 };
128  
129 /*---------------------------------------------------------------------------
130 * is_digit (from tzcode localtime.c, public domain)
131 *---------------------------------------------------------------------------*/
132 static int
133 3258 dtl_is_digit(char c)
134 {
135 3258 ▶ return '0' <= c && c <= '9';
Branch: 2 branches
BranchTrueFalseTotal
return '0' <= c && c <= '9'; 2203 1055 3258
return '0' <= c && c <= '9'; 1100 1103 2203
136 }
137  
138 /*---------------------------------------------------------------------------
139 * getzname (from tzcode localtime.c, public domain)
140 *
141 * Advance past an unquoted TZ abbreviation (e.g. "EST") and return a
142 * pointer to the first character that is not part of the name.
143 *---------------------------------------------------------------------------*/
144 static const char *
145 318 dtl_getzname(register const char *strp)
146 {
147 register char c;
148  
149 1626 ▶ while( ( c = *strp ) != '\0' && !dtl_is_digit(c) &&
Branch: 2 branches
BranchTrueFalseTotal
while( ( c = *strp ) != '\0' && !dtl_is_digit(c) && 1250 58 1308
while( ( c = *strp ) != '\0' && !dtl_is_digit(c) && 1149 101 1250
150 2457 ▶ c != ',' && c != '-' && c != '+' )
Branch: 3 branches
BranchTrueFalseTotal
c != ',' && c != '-' && c != '+' ) 1308 0 1308
c != ',' && c != '-' && c != '+' ) 990 159 1149
c != ',' && c != '-' && c != '+' ) 990 0 990
151 990 ++strp;
152 318 return strp;
153 }
154  
155 /*---------------------------------------------------------------------------
156 * getqzname (from tzcode localtime.c, public domain)
157 *
158 * Advance past a quoted TZ abbreviation enclosed by delim (e.g. '<+05:30>')
159 * and return a pointer to the delimiter character.
160 *---------------------------------------------------------------------------*/
161 static const char *
162 12 dtl_getqzname(register const char *strp, const int delim)
163 {
164 register int c;
165  
166 72 ▶ while( ( c = *strp ) != '\0' && c != delim )
Branch: 2 branches
BranchTrueFalseTotal
while( ( c = *strp ) != '\0' && c != delim ) 72 0 72
while( ( c = *strp ) != '\0' && c != delim ) 60 12 72
167 60 ++strp;
168 12 return strp;
169 }
170  
171 /*---------------------------------------------------------------------------
172 * getnum (from tzcode localtime.c, public domain)
173 *
174 * Extract a decimal integer in [min, max] from strp.
175 * Returns a pointer to the first non-digit character, or NULL on error.
176 *---------------------------------------------------------------------------*/
177 static const char *
178 908 dtl_getnum(register const char *strp, int *const nump,
179 const int min, const int max)
180 {
181 register char c;
182 register int num;
183  
184 908 ▶ if( strp == NULL || !dtl_is_digit( c = *strp ) )
Branch: 2 branches
BranchTrueFalseTotal
if( strp == NULL || !dtl_is_digit( c = *strp ) ) 908 0 908
if( strp == NULL || !dtl_is_digit( c = *strp ) ) 0 908 908
185 0 return NULL;
186 908 num = 0;
187 do
188 {
189 1042 num = num * 10 + ( c - '0' );
190 1042 ▶ if( num > max )
Branch: 1 branch
BranchTrueFalseTotal
if( num > max ) 0 1042 1042
191 0 return NULL; /* value out of range */
192 1042 c = *++strp;
193 }
194 1042 ▶ while( dtl_is_digit(c) );
Branch: 1 branch
BranchTrueFalseTotal
while( dtl_is_digit(c) ); 134 908 1042
195 908 ▶ if( num < min )
Branch: 1 branch
BranchTrueFalseTotal
if( num < min ) 0 908 908
196 0 return NULL; /* value out of range */
197 908 *nump = num;
198 908 return strp;
199 }
200  
201 /*---------------------------------------------------------------------------
202 * getsecs (from tzcode localtime.c, public domain)
203 *
204 * Parse hh[:mm[:ss]] into *secsp. The hour is clamped to the range
205 * [0, HOURSPERDAY * DAYSPERWEEK - 1] to allow quasi-POSIX rules like
206 * "M10.4.6/26" (hour 26 = 02:00 two days later).
207 * Returns a pointer past the parsed text, or NULL on error.
208 *---------------------------------------------------------------------------*/
209 static const char *
210 290 dtl_getsecs(register const char *strp, int_fast32_t *const secsp)
211 {
212 int num;
213  
214 290 strp = dtl_getnum( strp, &num, 0,
215 DTL_HOURSPERDAY * DTL_DAYSPERWEEK - 1 );
216 290 ▶ if( strp == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( strp == NULL ) 0 290 290
217 0 return NULL;
218 290 *secsp = (int_fast32_t) num * DTL_SECSPERHOUR;
219 290 ▶ if( *strp == ':' )
Branch: 1 branch
BranchTrueFalseTotal
if( *strp == ':' ) 12 278 290
220 {
221 12 ++strp;
222 12 strp = dtl_getnum( strp, &num, 0, DTL_MINSPERHOUR - 1 );
223 12 ▶ if( strp == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( strp == NULL ) 0 12 12
224 0 return NULL;
225 12 *secsp += num * DTL_SECSPERMIN;
226 12 ▶ if( *strp == ':' )
Branch: 1 branch
BranchTrueFalseTotal
if( *strp == ':' ) 0 12 12
227 {
228 0 ++strp;
229 /* DTL_SECSPERMIN + 1 allows for a leap second. */
230 0 strp = dtl_getnum( strp, &num, 0, DTL_SECSPERMIN );
231 0 ▶ if( strp == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( strp == NULL ) 0 0 0
232 0 return NULL;
233 0 *secsp += num;
234 }
235 }
236 290 return strp;
237 }
238  
239 /*---------------------------------------------------------------------------
240 * getoffset (from tzcode localtime.c, public domain)
241 *
242 * Parse [+-]hh[:mm[:ss]] into *offsetp.
243 * NOTE: POSIX convention - positive value means WEST of UTC.
244 * We preserve that convention here; callers negate as needed.
245 * Returns a pointer past the parsed text, or NULL on error.
246 *---------------------------------------------------------------------------*/
247 static const char *
248 290 dtl_getoffset(register const char *strp, int_fast32_t *const offsetp)
249 {
250 290 register int neg = 0;
251  
252 290 ▶ if( *strp == '-' )
Branch: 1 branch
BranchTrueFalseTotal
if( *strp == '-' ) 171 119 290
253 {
254 171 neg = 1;
255 171 ++strp;
256 }
257 119 ▶ else if( *strp == '+' )
Branch: 1 branch
BranchTrueFalseTotal
else if( *strp == '+' ) 0 119 119
258 {
259 0 ++strp;
260 }
261  
262 290 strp = dtl_getsecs( strp, offsetp );
263 290 ▶ if( strp == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( strp == NULL ) 0 290 290
264 0 return NULL;
265 290 ▶ if( neg )
Branch: 1 branch
BranchTrueFalseTotal
if( neg ) 171 119 290
266 171 *offsetp = -*offsetp;
267 290 return strp;
268 }
269  
270 /*---------------------------------------------------------------------------
271 * getrule (from tzcode localtime.c, public domain)
272 *
273 * Parse a DST transition rule in one of three forms:
274 * Jn Julian day (1-365, leap day not counted)
275 * n Day of year (0-365, leap day counted)
276 * Mm.w.d Month m (1-12), week w (1-5), day-of-week d (0=Sun)
277 * Optionally followed by /time (default 02:00:00).
278 *
279 * Per RFC 9636 section 3.3.2, time may be negative or exceed 24 h
280 * (TZif v3+ extension); dtl_getoffset() already handles the sign.
281 *
282 * Returns a pointer past the parsed rule, or NULL on error.
283 *---------------------------------------------------------------------------*/
284 static const char *
285 202 dtl_getrule(const char *strp, register struct dtl_rule *const rulep)
286 {
287 202 ▶ if( *strp == 'J' )
Branch: 1 branch
BranchTrueFalseTotal
if( *strp == 'J' ) 0 202 202
288 {
289 /* Julian day: 1-365, leap day not counted. */
290 0 rulep->r_type = DTL_JULIAN_DAY;
291 0 ++strp;
292 0 strp = dtl_getnum( strp, &rulep->r_day, 1, DTL_DAYSPERNYEAR );
293 }
294 202 ▶ else if( *strp == 'M' )
Branch: 1 branch
BranchTrueFalseTotal
else if( *strp == 'M' ) 202 0 202
295 {
296 /* Month, week, day of week. */
297 202 rulep->r_type = DTL_MONTH_NTH_DAY_OF_WEEK;
298 202 ++strp;
299 202 strp = dtl_getnum( strp, &rulep->r_mon, 1, DTL_MONSPERYEAR );
300 202 ▶ if( strp == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( strp == NULL ) 0 202 202
301 0 return NULL;
302 202 ▶ if( *strp++ != '.' )
Branch: 1 branch
BranchTrueFalseTotal
if( *strp++ != '.' ) 0 202 202
303 0 return NULL;
304 202 strp = dtl_getnum( strp, &rulep->r_week, 1, 5 );
305 202 ▶ if( strp == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( strp == NULL ) 0 202 202
306 0 return NULL;
307 202 ▶ if( *strp++ != '.' )
Branch: 1 branch
BranchTrueFalseTotal
if( *strp++ != '.' ) 0 202 202
308 0 return NULL;
309 202 strp = dtl_getnum( strp, &rulep->r_day, 0, DTL_DAYSPERWEEK - 1 );
310 }
311 0 ▶ else if( dtl_is_digit(*strp) )
Branch: 1 branch
BranchTrueFalseTotal
else if( dtl_is_digit(*strp) ) 0 0 0
312 {
313 /* Day of year: 0-365. */
314 0 rulep->r_type = DTL_DAY_OF_YEAR;
315 0 strp = dtl_getnum( strp, &rulep->r_day, 0, DTL_DAYSPERLYEAR - 1 );
316 }
317 else
318 {
319 0 return NULL; /* unrecognised rule format */
320 }
321  
322 202 ▶ if( strp == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( strp == NULL ) 0 202 202
323 0 return NULL;
324  
325 202 ▶ if( *strp == '/' )
Branch: 1 branch
BranchTrueFalseTotal
if( *strp == '/' ) 61 141 202
326 {
327 /* Explicit transition time. */
328 61 ++strp;
329 61 strp = dtl_getoffset( strp, &rulep->r_time );
330 }
331 else
332 {
333 141 rulep->r_time = 2 * DTL_SECSPERHOUR; /* default: 02:00:00 */
334 }
335  
336 202 return strp;
337 }
338  
339 /*---------------------------------------------------------------------------
340 * transtime (from tzcode localtime.c, public domain)
341 *
342 * Given a year, a parsed rule, and the UTC offset in effect when the
343 * transition occurs (signed, seconds east of UTC), return the YEAR-RELATIVE
344 * time (seconds since midnight Jan 1 of that year, UTC) at which the
345 * transition occurs.
346 *
347 * Uses Zeller's Congruence for the Mm.w.d case; purely arithmetic, no
348 * library calls.
349 *---------------------------------------------------------------------------*/
350 static int_fast32_t
351 202 dtl_transtime(const int year,
352 register const struct dtl_rule *const rulep,
353 const int_fast32_t offset)
354 {
355 register int leapyear;
356 register int_fast32_t value;
357 register int i;
358 int d, m1, yy0, yy1, yy2, dow;
359  
360 202 ▶ leapyear = dtl_isleap(year);
Branch: 3 branches
BranchTrueFalseTotal
leapyear = dtl_isleap(year); 0 202 202
leapyear = dtl_isleap(year); 0 0 0
leapyear = dtl_isleap(year); 0 0 0
361 202 switch( rulep->r_type )
362 {
363 0 case DTL_JULIAN_DAY:
364 /*
365 * Jn: Julian day 1-365. Leap day is not counted; in a leap year,
366 * day 60 and later are shifted by one extra day.
367 */
368 0 value = ( rulep->r_day - 1 ) * DTL_SECSPERDAY;
369 0 ▶ if( leapyear && rulep->r_day >= 60 )
Branch: 2 branches
BranchTrueFalseTotal
if( leapyear && rulep->r_day >= 60 ) 0 0 0
if( leapyear && rulep->r_day >= 60 ) 0 0 0
370 0 value += DTL_SECSPERDAY;
371 0 break;
372  
373 0 case DTL_DAY_OF_YEAR:
374 /*
375 * n: day of year 0-365. Simple multiplication.
376 */
377 0 value = rulep->r_day * DTL_SECSPERDAY;
378 0 break;
379  
380 202 case DTL_MONTH_NTH_DAY_OF_WEEK:
381 /*
382 * Mm.w.d: nth occurrence of weekday d in month m.
383 * Use Zeller's Congruence to find the day-of-week of the first
384 * day of month m in year.
385 */
386 202 m1 = ( rulep->r_mon + 9 ) % 12 + 1;
387 202 ▶ yy0 = ( rulep->r_mon <= 2 ) ? ( year - 1 ) : year;
Branch: 1 branch
BranchTrueFalseTotal
yy0 = ( rulep->r_mon <= 2 ) ? ( year - 1 ) : year; 0 202 202
388 202 yy1 = yy0 / 100;
389 202 yy2 = yy0 % 100;
390 202 dow = ( ( 26 * m1 - 2 ) / 10 +
391 202 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1 ) % 7;
392 202 ▶ if( dow < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if( dow < 0 ) 12 190 202
393 12 dow += DTL_DAYSPERWEEK;
394  
395 /* d = zero-origin day-of-month of first occurrence of r_day in
396 * this month. */
397 202 d = rulep->r_day - dow;
398 202 ▶ if( d < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if( d < 0 ) 77 125 202
399 77 d += DTL_DAYSPERWEEK;
400  
401 /* Advance by (r_week - 1) weeks, clamping to month end. */
402 624 ▶ for( i = 1; i < rulep->r_week; ++i )
Branch: 1 branch
BranchTrueFalseTotal
for( i = 1; i < rulep->r_week; ++i ) 479 145 624
403 {
404 479 if( d + DTL_DAYSPERWEEK >=
405 479 ▶ dtl_mon_lengths[leapyear][rulep->r_mon - 1] )
Branch: 1 branch
BranchTrueFalseTotal
dtl_mon_lengths[leapyear][rulep->r_mon - 1] ) 57 422 479
406 57 break;
407 422 d += DTL_DAYSPERWEEK;
408 }
409  
410 /* d is zero-origin day-of-month. Accumulate month offsets. */
411 202 value = d * DTL_SECSPERDAY;
412 1357 ▶ for( i = 0; i < rulep->r_mon - 1; ++i )
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < rulep->r_mon - 1; ++i ) 1155 202 1357
413 1155 value += dtl_mon_lengths[leapyear][i] * DTL_SECSPERDAY;
414 202 break;
415  
416 0 default:
417 0 dtl_unreachable();
418 value = 0;
419 break;
420 }
421  
422 /*
423 * value is seconds since Jan 1 00:00:00 UTC of this year on the transition
424 * day. Add the wall-clock time and subtract the UTC offset to convert to
425 * UTC seconds-since-Jan-1.
426 */
427 202 return value + rulep->r_time - offset;
428 }
429  
430 /*---------------------------------------------------------------------------
431 * dtl_year_to_jan1 (new, not from tzcode)
432 *
433 * Return the Unix timestamp of midnight UTC on January 1 of year.
434 * Valid for years >= 1970. Uses pure arithmetic; no library calls.
435 *---------------------------------------------------------------------------*/
436 static int_fast64_t
437 202 dtl_year_to_jan1(int year)
438 {
439 int_fast64_t days;
440 int y;
441  
442 /* Count days from 1970-01-01 to year-01-01. */
443 202 y = year - 1;
444 202 days = (int_fast64_t)(year - DTL_EPOCH_YEAR) * DTL_DAYSPERNYEAR
445 202 + ( y / 4 - (DTL_EPOCH_YEAR - 1) / 4 )
446 202 - ( y / 100 - (DTL_EPOCH_YEAR - 1) / 100 )
447 202 + ( y / 400 - (DTL_EPOCH_YEAR - 1) / 400 );
448  
449 202 return days * DTL_SECSPERDAY;
450 }
451  
452 /*---------------------------------------------------------------------------
453 * dtl_posix_tz_result (new, not from tzcode)
454 *
455 * Convenience struct for the result of dtl_posix_tz_lookup().
456 *---------------------------------------------------------------------------*/
457 #define DTL_POSIX_ABBR_MAX 32
458  
459 typedef struct
460 {
461 int_fast32_t offset; /* seconds east of UTC */
462 int is_dst; /* 1 if DST, 0 if std */
463 char abbr[DTL_POSIX_ABBR_MAX]; /* NUL-terminated abbr */
464 int valid; /* 1 if result is valid */
465 } dtl_posix_tz_result;
466  
467 /*---------------------------------------------------------------------------
468 * dtl_posix_tz_lookup (new, not from tzcode)
469 *
470 * Given a UTC Unix timestamp and a POSIX TZ footer string, compute the UTC
471 * offset, DST status, and time-zone abbreviation in effect at that timestamp.
472 *
473 * Algorithm:
474 * 1. Parse the standard name and offset from the footer string.
475 * 2. If no DST suffix is present, return the standard offset.
476 * 3. Parse the DST name, optional DST offset (default std - 1 h in POSIX
477 * convention, i.e. std_offset - 3600 in UTC terms).
478 * 4. Parse the start and end DST rules.
479 * 5. Use dtl_transtime() to find the transition timestamps for the year
480 * containing unix_secs.
481 * 6. Compare unix_secs with the two transition times to decide which half
482 * of the year it falls in. Handle both northern-hemisphere (start < end)
483 * and southern-hemisphere (start > end) cases.
484 *
485 * The result is written into *res. On parse error, res->valid = 0.
486 *---------------------------------------------------------------------------*/
487 static void
488 229 dtl_posix_tz_lookup(int_fast64_t unix_secs,
489 const char *tz_str,
490 dtl_posix_tz_result *res)
491 {
492 229 const char *p = tz_str;
493 const char *stdname, *dstname;
494 ptrdiff_t stdlen, dstlen;
495 int_fast32_t stdoffset, dstoffset;
496 struct dtl_rule start_rule, end_rule;
497 int have_dst_rule;
498 int_fast64_t jan1;
499 int_fast32_t start_trans, end_trans;
500 int year;
501 int is_dst;
502  
503 229 res->valid = 0;
504  
505 /*-----------------------------------------------------------------------
506 * Parse the standard abbreviation.
507 *-----------------------------------------------------------------------*/
508 229 ▶ if( *p == '<' )
Branch: 1 branch
BranchTrueFalseTotal
if( *p == '<' ) 12 217 229
509 {
510 12 ++p;
511 12 stdname = p;
512 12 p = dtl_getqzname( p, '>' );
513 12 ▶ if( *p != '>' )
Branch: 1 branch
BranchTrueFalseTotal
if( *p != '>' ) 0 12 12
514 128 return;
515 12 stdlen = p - stdname;
516 12 ++p;
517 }
518 else
519 {
520 217 stdname = p;
521 217 p = dtl_getzname( p );
522 217 stdlen = p - stdname;
523 }
524 229 ▶ if( stdlen <= 0 || stdlen >= DTL_TZNAME_MAXIMUM )
Branch: 2 branches
BranchTrueFalseTotal
if( stdlen <= 0 || stdlen >= DTL_TZNAME_MAXIMUM ) 229 0 229
if( stdlen <= 0 || stdlen >= DTL_TZNAME_MAXIMUM ) 0 229 229
525 0 return;
526  
527 /*-----------------------------------------------------------------------
528 * Parse the standard UTC offset (mandatory).
529 * POSIX convention: positive = WEST. We negate to get UTC east.
530 *-----------------------------------------------------------------------*/
531 229 p = dtl_getoffset( p, &stdoffset );
532 229 ▶ if( p == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( p == NULL ) 0 229 229
533 0 return;
534 /* stdoffset is now seconds WEST of UTC; negate for east convention. */
535 229 stdoffset = -stdoffset;
536  
537 /*-----------------------------------------------------------------------
538 * If no DST suffix, return standard time unconditionally.
539 *-----------------------------------------------------------------------*/
540 229 ▶ if( *p == '\0' )
Branch: 1 branch
BranchTrueFalseTotal
if( *p == '\0' ) 128 101 229
541 {
542 128 res->valid = 1;
543 128 res->is_dst = 0;
544 128 res->offset = stdoffset;
545 {
546 128 ▶ int n = (int)( stdlen < DTL_POSIX_ABBR_MAX - 1
Branch: 1 branch
BranchTrueFalseTotal
int n = (int)( stdlen < DTL_POSIX_ABBR_MAX - 1 128 0 128
547 ? stdlen : DTL_POSIX_ABBR_MAX - 1 );
548 128 memcpy( res->abbr, stdname, (size_t)n );
549 128 res->abbr[n] = '\0';
550 }
551 128 return;
552 }
553  
554 /*-----------------------------------------------------------------------
555 * Parse the DST abbreviation.
556 *-----------------------------------------------------------------------*/
557 101 ▶ if( *p == '<' )
Branch: 1 branch
BranchTrueFalseTotal
if( *p == '<' ) 0 101 101
558 {
559 0 ++p;
560 0 dstname = p;
561 0 p = dtl_getqzname( p, '>' );
562 0 ▶ if( *p != '>' )
Branch: 1 branch
BranchTrueFalseTotal
if( *p != '>' ) 0 0 0
563 0 return;
564 0 dstlen = p - dstname;
565 0 ++p;
566 }
567 else
568 {
569 101 dstname = p;
570 101 p = dtl_getzname( p );
571 101 dstlen = p - dstname;
572 }
573 101 ▶ if( dstlen <= 0 || dstlen >= DTL_TZNAME_MAXIMUM )
Branch: 2 branches
BranchTrueFalseTotal
if( dstlen <= 0 || dstlen >= DTL_TZNAME_MAXIMUM ) 101 0 101
if( dstlen <= 0 || dstlen >= DTL_TZNAME_MAXIMUM ) 0 101 101
574 0 return;
575  
576 /*-----------------------------------------------------------------------
577 * Optional DST offset (default: std + 1 h in UTC east convention,
578 * which corresponds to stdoffset_POSIX - 1 h, i.e. one hour closer
579 * to UTC than the standard offset in POSIX terms).
580 *-----------------------------------------------------------------------*/
581 101 ▶ if( *p != '\0' && *p != ',' && *p != ';' )
Branch: 3 branches
BranchTrueFalseTotal
if( *p != '\0' && *p != ',' && *p != ';' ) 101 0 101
if( *p != '\0' && *p != ',' && *p != ';' ) 0 101 101
if( *p != '\0' && *p != ',' && *p != ';' ) 0 0 0
582 0 {
583 int_fast32_t raw_dst;
584 0 p = dtl_getoffset( p, &raw_dst );
585 0 ▶ if( p == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( p == NULL ) 0 0 0
586 0 return;
587 0 dstoffset = -raw_dst; /* negate POSIX to UTC east */
588 }
589 else
590 {
591 /* Default: DST = standard + 1 h. */
592 101 dstoffset = stdoffset + DTL_SECSPERHOUR;
593 }
594  
595 /*-----------------------------------------------------------------------
596 * If no rule follows, use the POSIX default rule.
597 *-----------------------------------------------------------------------*/
598 101 ▶ if( *p == '\0' )
Branch: 1 branch
BranchTrueFalseTotal
if( *p == '\0' ) 0 101 101
599 0 p = DTL_TZDEFRULESTRING;
600  
601 101 ▶ if( *p != ',' && *p != ';' )
Branch: 2 branches
BranchTrueFalseTotal
if( *p != ',' && *p != ';' ) 0 101 101
if( *p != ',' && *p != ';' ) 0 0 0
602 0 return;
603  
604 /*-----------------------------------------------------------------------
605 * Parse the DST start rule.
606 *-----------------------------------------------------------------------*/
607 101 p = dtl_getrule( p + 1, &start_rule );
608 101 ▶ if( p == NULL || *p != ',' )
Branch: 2 branches
BranchTrueFalseTotal
if( p == NULL || *p != ',' ) 101 0 101
if( p == NULL || *p != ',' ) 0 101 101
609 0 return;
610  
611 /*-----------------------------------------------------------------------
612 * Parse the DST end rule.
613 *-----------------------------------------------------------------------*/
614 101 p = dtl_getrule( p + 1, &end_rule );
615 101 ▶ if( p == NULL || *p != '\0' )
Branch: 2 branches
BranchTrueFalseTotal
if( p == NULL || *p != '\0' ) 101 0 101
if( p == NULL || *p != '\0' ) 0 101 101
616 0 return;
617  
618 101 have_dst_rule = 1;
619 (void)have_dst_rule;
620  
621 /*-----------------------------------------------------------------------
622 * Determine the year and compute Jan 1 UTC for that year.
623 *-----------------------------------------------------------------------*/
624 {
625 /* Approximate year from unix timestamp (good enough for +-9999). */
626 101 int_fast64_t days_since_epoch = unix_secs / DTL_SECSPERDAY;
627 101 year = DTL_EPOCH_YEAR + (int)( days_since_epoch / DTL_DAYSPERNYEAR );
628 /* Adjust: make sure jan1 <= unix_secs. */
629 101 jan1 = dtl_year_to_jan1( year );
630 101 ▶ while( jan1 > unix_secs )
Branch: 1 branch
BranchTrueFalseTotal
while( jan1 > unix_secs ) 0 101 101
631 {
632 0 --year;
633 0 jan1 = dtl_year_to_jan1( year );
634 }
635 101 ▶ while( dtl_year_to_jan1( year + 1 ) <= unix_secs )
Branch: 1 branch
BranchTrueFalseTotal
while( dtl_year_to_jan1( year + 1 ) <= unix_secs ) 0 101 101
636 {
637 0 ++year;
638 0 jan1 = dtl_year_to_jan1( year );
639 }
640 }
641  
642 /*-----------------------------------------------------------------------
643 * Compute the UTC timestamps of the DST start and end transitions.
644 *
645 * dtl_transtime() returns year-relative seconds (from Jan 1 00:00 UTC)
646 * with offset already factored in; add jan1 to get Unix timestamp.
647 *
648 * For the start (std -> DST): offset in effect = stdoffset
649 * For the end (DST -> std): offset in effect = dstoffset
650 *-----------------------------------------------------------------------*/
651 101 start_trans = (int_fast32_t)( jan1 +
652 101 dtl_transtime( year, &start_rule, stdoffset ) );
653 101 end_trans = (int_fast32_t)( jan1 +
654 101 dtl_transtime( year, &end_rule, dstoffset ) );
655  
656 /*-----------------------------------------------------------------------
657 * Determine DST vs standard.
658 *
659 * Northern hemisphere: DST active when start_trans <= t < end_trans.
660 * Southern hemisphere: DST active when t >= start_trans OR t < end_trans.
661 *-----------------------------------------------------------------------*/
662 101 ▶ if( start_trans < end_trans )
Branch: 1 branch
BranchTrueFalseTotal
if( start_trans < end_trans ) 97 4 101
663 97 ▶ is_dst = ( unix_secs >= start_trans && unix_secs < end_trans ) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
is_dst = ( unix_secs >= start_trans && unix_secs < end_trans ) ? 1 : 0; 70 27 97
is_dst = ( unix_secs >= start_trans && unix_secs < end_trans ) ? 1 : 0; 67 3 70
664 else
665 4 ▶ is_dst = ( unix_secs >= start_trans || unix_secs < end_trans ) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
is_dst = ( unix_secs >= start_trans || unix_secs < end_trans ) ? 1 : 0; 4 0 4
is_dst = ( unix_secs >= start_trans || unix_secs < end_trans ) ? 1 : 0; 2 2 4
666  
667 /*-----------------------------------------------------------------------
668 * Fill in result.
669 *-----------------------------------------------------------------------*/
670 101 res->valid = 1;
671 101 res->is_dst = is_dst;
672 101 ▶ res->offset = is_dst ? dstoffset : stdoffset;
Branch: 1 branch
BranchTrueFalseTotal
res->offset = is_dst ? dstoffset : stdoffset; 69 32 101
673 {
674 101 ▶ const char *name = is_dst ? dstname : stdname;
Branch: 1 branch
BranchTrueFalseTotal
const char *name = is_dst ? dstname : stdname; 69 32 101
675 101 ▶ ptrdiff_t namelen = is_dst ? dstlen : stdlen;
Branch: 1 branch
BranchTrueFalseTotal
ptrdiff_t namelen = is_dst ? dstlen : stdlen; 69 32 101
676 101 ▶ int n = (int)( namelen < DTL_POSIX_ABBR_MAX - 1
Branch: 1 branch
BranchTrueFalseTotal
int n = (int)( namelen < DTL_POSIX_ABBR_MAX - 1 101 0 101
677 ? namelen : DTL_POSIX_ABBR_MAX - 1 );
678 101 memcpy( res->abbr, name, (size_t)n );
679 101 res->abbr[n] = '\0';
680 }
681 }
682  
683 #endif /* DTL_POSIX_H */
« DateTime-Lite.xs ↑ Summary