datetime.c

filter: Statementstmt 93.1% 311 / 334 Branchbran 67.2% 343 / 510 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.4% 654 / 844 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/ClickHouse/Encoder/TCP.pm ↑ Summary scalar_kind.c »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <math.h>
7 #include <stdint.h>
8  
9 #include "types.h"
10 #include "datetime.h"
11 #include "decimal.h"
12 #include "scalar_kind.h"
13  
14 typedef struct {
15 int kind, month, week, day, seconds, clock;
16 } TZRule;
17  
18 struct CHTimezone {
19 CHTimezone *next;
20 char *name;
21 int64_t *transitions;
22 unsigned char *indices;
23 uint32_t count, type_count;
24 int32_t offsets[256];
25 int footer, has_dst, standard, daylight;
26 TZRule start, end;
27 };
28  
29 static const int days_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
30  
31 23669 static int is_leap_year(int year) {
32 23669 ▶ return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
Branch: 3 branches
BranchTrueFalseTotal
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 5881 17788 23669
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 267 5614 5881
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 130 17925 18055
33 }
34  
35 289 static int64_t year_days(int year) {
36 289 int y = year - 1;
37 289 return 365LL * (year - 1970) + y / 4 - y / 100 + y / 400 - 477;
38 }
39  
40 36 static int epoch_year(int64_t epoch) {
41 36 int year = 1970 + (int)(epoch / 31556952);
42 37 ▶ while (epoch < year_days(year) * 86400) year--;
Branch: 1 branch
BranchTrueFalseTotal
while (epoch < year_days(year) * 86400) year--; 1 36 37
43 36 ▶ while (epoch >= year_days(year + 1) * 86400) year++;
Branch: 1 branch
BranchTrueFalseTotal
while (epoch >= year_days(year + 1) * 86400) year++; 0 36 36
44 36 return year;
45 }
46  
47 216 static int64_t rule_epoch(const TZRule *rule, int year, int standard, int before) {
48 216 int day = rule->day, month;
49 216 int64_t days = year_days(year);
50 216 ▶ if (rule->kind == 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if (rule->kind == 'M') { 72 144 216
51 int weekday, length;
52 468 ▶ for (month = 1; month < rule->month; month++)
Branch: 1 branch
BranchTrueFalseTotal
for (month = 1; month < rule->month; month++) 396 72 468
53 396 ▶ days += days_in_month[month - 1] + (month == 2 && is_leap_year(year));
Branch: 2 branches
BranchTrueFalseTotal
days += days_in_month[month - 1] + (month == 2 && is_leap_year(year)); 72 324 396
days += days_in_month[month - 1] + (month == 2 && is_leap_year(year)); 0 72 72
54 72 weekday = (int)((days + 4) % 7);
55 72 ▶ if (weekday < 0) weekday += 7;
Branch: 1 branch
BranchTrueFalseTotal
if (weekday < 0) weekday += 7; 0 72 72
56 72 day = (rule->day - weekday + 7) % 7 + 7 * (rule->week - 1);
57 144 length = days_in_month[rule->month - 1]
58 72 ▶ + (rule->month == 2 && is_leap_year(year));
Branch: 2 branches
BranchTrueFalseTotal
+ (rule->month == 2 && is_leap_year(year)); 0 72 72
+ (rule->month == 2 && is_leap_year(year)); 0 0 0
59 72 ▶ if (day >= length) day -= 7;
Branch: 1 branch
BranchTrueFalseTotal
if (day >= length) day -= 7; 36 36 72
60 144 ▶ } else if (rule->kind == 'J') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (rule->kind == 'J') { 72 72 144
61 72 day--;
62 72 ▶ if (is_leap_year(year) && rule->day >= 60) day++;
Branch: 2 branches
BranchTrueFalseTotal
if (is_leap_year(year) && rule->day >= 60) day++; 2 70 72
if (is_leap_year(year) && rule->day >= 60) day++; 2 0 2
63 }
64 216 return (days + day) * 86400 + rule->seconds
65 216 ▶ - (rule->clock == 'u' ? 0 : rule->clock == 's' ? standard : before);
Branch: 2 branches
BranchTrueFalseTotal
- (rule->clock == 'u' ? 0 : rule->clock == 's' ? standard : before); 216 0 216
- (rule->clock == 'u' ? 0 : rule->clock == 's' ? standard : before); 0 216 216
66 }
67  
68 74 int timezone_offset(const CHTimezone *tz, int64_t epoch) {
69 74 uint32_t lo = 0, hi;
70 74 ▶ if (!tz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz) return 0; 0 74 74
71 74 ▶ if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) {
Branch: 3 branches
BranchTrueFalseTotal
if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) { 36 38 74
if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) { 0 36 36
if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) { 0 0 0
72 36 int year, y, offset = tz->standard;
73 36 int64_t latest = INT64_MIN;
74 36 ▶ if (!tz->has_dst) return tz->standard;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz->has_dst) return tz->standard; 0 36 36
75 36 year = epoch_year(epoch);
76 144 ▶ for (y = year - 1; y <= year + 1; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = year - 1; y <= year + 1; y++) { 108 36 144
77 108 int64_t start = rule_epoch(&tz->start, y, tz->standard, tz->standard);
78 108 int64_t end = rule_epoch(&tz->end, y, tz->standard, tz->daylight);
79 /* >= so a year-round DST rule (end(y) == start(y+1)) stays in DST */
80 108 ▶ if (start <= epoch && start >= latest) { latest = start; offset = tz->daylight; }
Branch: 2 branches
BranchTrueFalseTotal
if (start <= epoch && start >= latest) { latest = start; offset = tz->daylight; } 56 52 108
if (start <= epoch && start >= latest) { latest = start; offset = tz->daylight; } 56 0 56
81 108 ▶ if (end <= epoch && end > latest) { latest = end; offset = tz->standard; }
Branch: 2 branches
BranchTrueFalseTotal
if (end <= epoch && end > latest) { latest = end; offset = tz->standard; } 32 76 108
if (end <= epoch && end > latest) { latest = end; offset = tz->standard; } 32 0 32
82 }
83 36 return offset;
84 }
85 38 hi = tz->count;
86 42 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 4 38 42
87 4 uint32_t mid = lo + (hi - lo) / 2;
88 4 ▶ if (tz->transitions[mid] <= epoch) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (tz->transitions[mid] <= epoch) lo = mid + 1; 4 0 4
89 0 else hi = mid;
90 }
91 38 ▶ return tz->offsets[lo ? tz->indices[lo - 1] : 0];
Branch: 1 branch
BranchTrueFalseTotal
return tz->offsets[lo ? tz->indices[lo - 1] : 0]; 4 34 38
92 }
93  
94 78 static int tz_number(const char **p, int limit) {
95 78 int n = 0;
96 78 ▶ if (**p < '0' || **p > '9') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (**p < '0' || **p > '9') return -1; 78 0 78
if (**p < '0' || **p > '9') return -1; 2 76 78
97 179 ▶ while (**p >= '0' && **p <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
while (**p >= '0' && **p <= '9') { 116 63 179
while (**p >= '0' && **p <= '9') { 103 13 116
98 103 int d = *(*p)++ - '0';
99 103 ▶ if (n > (limit - d) / 10) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n > (limit - d) / 10) return -1; 0 103 103
100 103 n = n * 10 + d;
101 }
102 76 ▶ return n <= limit ? n : -1;
Branch: 1 branch
BranchTrueFalseTotal
return n <= limit ? n : -1; 76 0 76
103 }
104  
105 26 static int tz_abbreviation(const char **p) {
106 26 const char *start = *p;
107 26 ▶ if (**p == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (**p == '<') { 0 26 26
108 0 (*p)++;
109 0 ▶ while (**p && **p != '>') (*p)++;
Branch: 2 branches
BranchTrueFalseTotal
while (**p && **p != '>') (*p)++; 0 0 0
while (**p && **p != '>') (*p)++; 0 0 0
110 0 ▶ if (**p != '>') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (**p != '>') return 0; 0 0 0
111 0 (*p)++;
112 0 return *p - start >= 3;
113 }
114 104 ▶ while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++;
Branch: 4 branches
BranchTrueFalseTotal
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 78 26 104
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 78 0 78
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 0 26 26
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 0 0 0
115 26 return *p - start >= 3;
116 }
117  
118 42 static int tz_seconds(const char **p, int *out) {
119 42 int sign = 1, h, m = 0, s = 0;
120 42 ▶ if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1;
Branch: 3 branches
BranchTrueFalseTotal
if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1; 26 16 42
if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1; 0 26 26
if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1; 16 0 16
121 42 h = tz_number(p, 167);
122 42 ▶ if (h < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (h < 0) return 0; 0 42 42
123 42 ▶ if (**p == ':') { (*p)++; m = tz_number(p, 59); }
Branch: 1 branch
BranchTrueFalseTotal
if (**p == ':') { (*p)++; m = tz_number(p, 59); } 0 42 42
124 42 ▶ if (**p == ':') { (*p)++; s = tz_number(p, 59); }
Branch: 1 branch
BranchTrueFalseTotal
if (**p == ':') { (*p)++; s = tz_number(p, 59); } 0 42 42
125 42 ▶ if (m < 0 || s < 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (m < 0 || s < 0) return 0; 42 0 42
if (m < 0 || s < 0) return 0; 0 42 42
126 42 *out = sign * (h * 3600 + m * 60 + s);
127 42 return 1;
128 }
129  
130 24 static int tz_rule(const char **p, TZRule *rule) {
131 24 rule->clock = 'w';
132 24 rule->seconds = 7200;
133 24 ▶ if (**p == 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if (**p == 'M') { 6 18 24
134 6 rule->kind = *(*p)++;
135 6 rule->month = tz_number(p, 12);
136 6 ▶ if (rule->month < 1 || *(*p)++ != '.') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rule->month < 1 || *(*p)++ != '.') return 0; 6 0 6
if (rule->month < 1 || *(*p)++ != '.') return 0; 0 6 6
137 6 rule->week = tz_number(p, 5);
138 6 ▶ if (rule->week < 1 || *(*p)++ != '.') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rule->week < 1 || *(*p)++ != '.') return 0; 6 0 6
if (rule->week < 1 || *(*p)++ != '.') return 0; 0 6 6
139 6 rule->day = tz_number(p, 6);
140 } else {
141 18 ▶ rule->kind = **p == 'J' ? *(*p)++ : 'n';
Branch: 1 branch
BranchTrueFalseTotal
rule->kind = **p == 'J' ? *(*p)++ : 'n'; 8 10 18
142 18 rule->day = tz_number(p, 365);
143 18 ▶ if (rule->kind == 'J' && rule->day == 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rule->kind == 'J' && rule->day == 0) return 0; 8 10 18
if (rule->kind == 'J' && rule->day == 0) return 0; 0 8 8
144 }
145 24 ▶ if (rule->day < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rule->day < 0) return 0; 2 22 24
146 22 ▶ if (**p == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (**p == '/') { 22 0 22
147 22 (*p)++;
148 22 ▶ if (!tz_seconds(p, &rule->seconds)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz_seconds(p, &rule->seconds)) return 0; 0 22 22
149 22 ▶ if (**p == 's' || **p == 'w') rule->clock = *(*p)++;
Branch: 2 branches
BranchTrueFalseTotal
if (**p == 's' || **p == 'w') rule->clock = *(*p)++; 22 0 22
if (**p == 's' || **p == 'w') rule->clock = *(*p)++; 0 22 22
150 22 ▶ else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; }
Branch: 3 branches
BranchTrueFalseTotal
else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; } 22 0 22
else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; } 22 0 22
else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; } 0 22 22
151 }
152 22 return 1;
153 }
154  
155 13 static int tz_footer(CHTimezone *tz, const char *p) {
156 int offset;
157 13 ▶ if (!tz_abbreviation(&p) || !tz_seconds(&p, &offset)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!tz_abbreviation(&p) || !tz_seconds(&p, &offset)) return 0; 13 0 13
if (!tz_abbreviation(&p) || !tz_seconds(&p, &offset)) return 0; 0 13 13
158 13 tz->standard = -offset;
159 13 tz->footer = 1;
160 13 ▶ if (!*p) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) return 1; 0 13 13
161 13 ▶ if (!tz_abbreviation(&p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz_abbreviation(&p)) return 0; 0 13 13
162 13 tz->has_dst = 1;
163 13 tz->daylight = tz->standard + 3600;
164 13 ▶ if (*p != ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p != ',') { 7 6 13
165 7 ▶ if (!tz_seconds(&p, &offset)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz_seconds(&p, &offset)) return 0; 0 7 7
166 7 tz->daylight = -offset;
167 }
168 13 ▶ if (*p++ != ',' || !tz_rule(&p, &tz->start)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*p++ != ',' || !tz_rule(&p, &tz->start)) return 0; 13 0 13
if (*p++ != ',' || !tz_rule(&p, &tz->start)) return 0; 2 11 13
169 11 ▶ if (*p++ != ',' || !tz_rule(&p, &tz->end)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*p++ != ',' || !tz_rule(&p, &tz->end)) return 0; 11 0 11
if (*p++ != ',' || !tz_rule(&p, &tz->end)) return 0; 0 11 11
170 11 return !*p;
171 }
172  
173 240 static uint32_t tz_u32(const unsigned char *p) {
174 240 return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16
175 240 | (uint32_t)p[2] << 8 | p[3];
176 }
177  
178 31 static uint64_t tz_section_size(const unsigned char *h, int width) {
179 31 return (uint64_t)tz_u32(h + 32) * (width + 1)
180 31 + (uint64_t)tz_u32(h + 36) * 6 + tz_u32(h + 40)
181 31 + (uint64_t)tz_u32(h + 28) * (width + 4)
182 31 + tz_u32(h + 20) + tz_u32(h + 24);
183 }
184  
185 1815 void timezone_free(pTHX_ CHTimezone *tz) {
186 3609 ▶ while (tz) {
Branch: 1 branch
BranchTrueFalseTotal
while (tz) { 1794 1815 3609
187 1794 CHTimezone *next = tz->next;
188 1794 Safefree(tz->name);
189 1794 Safefree(tz->transitions);
190 1794 Safefree(tz->indices);
191 1794 Safefree(tz);
192 1794 tz = next;
193 }
194 1815 }
195  
196 1815 CHTimezone *timezone_load(pTHX_ CHTimezone **zones, const char *name) {
197 static const char *dirs[] = {"/usr/share/zoneinfo", "/usr/lib/zoneinfo", "/usr/share/lib/zoneinfo"};
198 CHTimezone *tz;
199 1815 PerlIO *file = NULL;
200 SV *data;
201 const unsigned char *h, *p;
202 STRLEN length;
203 uint64_t size;
204 uint32_t i;
205 1815 int width = 4, d;
206 1815 const char *tzdir = PerlEnv_getenv("TZDIR");
207 1839 ▶ for (tz = *zones; tz; tz = tz->next)
Branch: 1 branch
BranchTrueFalseTotal
for (tz = *zones; tz; tz = tz->next) 35 1804 1839
208 35 ▶ if (!strcmp(tz->name, name)) return tz;
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(tz->name, name)) return tz; 11 24 35
209 1804 ▶ if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT")) {
Branch: 3 branches
BranchTrueFalseTotal
if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT")) { 28 1776 1804
if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT")) { 27 1 28
if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT")) { 1 26 27
210 1778 Newxz(tz, 1, CHTimezone);
211 1778 goto loaded;
212 }
213 26 ▶ if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\'))
Branch: 4 branches
BranchTrueFalseTotal
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\')) 26 0 26
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\')) 26 0 26
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\')) 26 0 26
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\')) 0 26 26
214 0 croak("Invalid timezone '%s'", name);
215 42 ▶ for (d = 0; d < (tzdir ? 1 : 3); d++) {
Branch: 2 branches
BranchTrueFalseTotal
for (d = 0; d < (tzdir ? 1 : 3); d++) { 26 16 42
for (d = 0; d < (tzdir ? 1 : 3); d++) { 34 8 42
216 34 ▶ SV *path = sv_2mortal(newSVpvf("%s/%s", tzdir ? tzdir : dirs[d], name));
Branch: 1 branch
BranchTrueFalseTotal
SV *path = sv_2mortal(newSVpvf("%s/%s", tzdir ? tzdir : dirs[d], name)); 12 22 34
217 34 file = PerlIO_open(SvPV_nolen(path), "rb");
218 34 ▶ if (file) break;
Branch: 1 branch
BranchTrueFalseTotal
if (file) break; 18 16 34
219 }
220 26 ▶ if (!file) croak("Unknown timezone '%s' or missing timezone database", name);
Branch: 1 branch
BranchTrueFalseTotal
if (!file) croak("Unknown timezone '%s' or missing timezone database", name); 8 18 26
221 18 data = sv_2mortal(newSVpvs(""));
222 18 for (;;) {
223 char chunk[4096];
224 36 SSize_t n = PerlIO_read(file, chunk, sizeof(chunk));
225 36 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 18 18 36
226 18 ▶ int failed = n < 0 || PerlIO_error(file);
Branch: 2 branches
BranchTrueFalseTotal
int failed = n < 0 || PerlIO_error(file); 18 0 18
int failed = n < 0 || PerlIO_error(file); 0 18 18
227 18 PerlIO_close(file);
228 18 ▶ if (failed) croak("Cannot read timezone '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (failed) croak("Cannot read timezone '%s'", name); 0 18 18
229 18 break;
230 }
231 18 sv_catpvn(data, chunk, n);
232 18 ▶ if (SvCUR(data) > 16777216) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(data) > 16777216) { 0 18 18
233 0 PerlIO_close(file);
234 0 croak("Timezone '%s' TZif file is too large", name);
235 }
236 }
237 18 h = (const unsigned char *)SvPV(data, length);
238 18 ▶ if (length < 44 || memcmp(h, "TZif", 4)) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (length < 44 || memcmp(h, "TZif", 4)) goto invalid; 16 2 18
if (length < 44 || memcmp(h, "TZif", 4)) goto invalid; 0 16 16
239 16 size = tz_section_size(h, width);
240 16 ▶ if (size > length - 44) goto invalid;
Branch: 1 branch
BranchTrueFalseTotal
if (size > length - 44) goto invalid; 0 16 16
241 16 ▶ if (h[4] == '2' || h[4] == '3') {
Branch: 2 branches
BranchTrueFalseTotal
if (h[4] == '2' || h[4] == '3') { 15 1 16
if (h[4] == '2' || h[4] == '3') { 14 1 15
242 15 h += 44 + (STRLEN)size;
243 15 length -= 44 + (STRLEN)size;
244 15 ▶ if (length < 44 || memcmp(h, "TZif", 4)) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (length < 44 || memcmp(h, "TZif", 4)) goto invalid; 15 0 15
if (length < 44 || memcmp(h, "TZif", 4)) goto invalid; 0 15 15
245 15 width = 8;
246 15 size = tz_section_size(h, width);
247 15 ▶ if (size > length - 44) goto invalid;
Branch: 1 branch
BranchTrueFalseTotal
if (size > length - 44) goto invalid; 0 15 15
248 1 ▶ } else if (h[4] != 0) goto invalid;
Branch: 1 branch
BranchTrueFalseTotal
} else if (h[4] != 0) goto invalid; 0 1 1
249 16 Newxz(tz, 1, CHTimezone);
250 16 tz->count = tz_u32(h + 32);
251 16 tz->type_count = tz_u32(h + 36);
252 16 ▶ if (!tz->type_count || tz->type_count > 256) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (!tz->type_count || tz->type_count > 256) goto invalid; 16 0 16
if (!tz->type_count || tz->type_count > 256) goto invalid; 0 16 16
253 16 p = h + 44;
254 16 ▶ if (tz->count) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz->count) { 2 14 16
255 2 Newx(tz->transitions, tz->count, int64_t);
256 2 Newx(tz->indices, tz->count, unsigned char);
257 }
258 18 ▶ for (i = 0; i < tz->count; i++, p += width) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tz->count; i++, p += width) { 2 16 18
259 2 uint64_t v = tz_u32(p);
260 2 tz->transitions[i] = width == 8 ? (int64_t)((v << 32) | tz_u32(p + 4))
261 4 ▶ : (int64_t)(int32_t)v;
Branch: 1 branch
BranchTrueFalseTotal
: (int64_t)(int32_t)v; 2 0 2
262 2 ▶ if (i && tz->transitions[i] <= tz->transitions[i - 1]) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (i && tz->transitions[i] <= tz->transitions[i - 1]) goto invalid; 0 2 2
if (i && tz->transitions[i] <= tz->transitions[i - 1]) goto invalid; 0 0 0
263 }
264 18 ▶ for (i = 0; i < tz->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tz->count; i++) { 2 16 18
265 2 ▶ if (p[i] >= tz->type_count) goto invalid;
Branch: 1 branch
BranchTrueFalseTotal
if (p[i] >= tz->type_count) goto invalid; 0 2 2
266 2 tz->indices[i] = p[i];
267 }
268 16 p += tz->count;
269 34 ▶ for (i = 0; i < tz->type_count; i++, p += 6) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tz->type_count; i++, p += 6) { 18 16 34
270 18 tz->offsets[i] = (int32_t)tz_u32(p);
271 18 ▶ if (tz->offsets[i] < -604800 || tz->offsets[i] > 604800) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (tz->offsets[i] < -604800 || tz->offsets[i] > 604800) goto invalid; 18 0 18
if (tz->offsets[i] < -604800 || tz->offsets[i] > 604800) goto invalid; 0 18 18
272 }
273 16 ▶ if (width == 8 && length > 44 + size) {
Branch: 2 branches
BranchTrueFalseTotal
if (width == 8 && length > 44 + size) { 15 1 16
if (width == 8 && length > 44 + size) { 0 15 15
274 15 STRLEN footer_len = length - 44 - (STRLEN)size;
275 15 const char *footer = (const char *)h + 44 + (STRLEN)size;
276 15 ▶ if (footer_len < 2 || footer[0] != '\n' || footer[footer_len - 1] != '\n') goto invalid;
Branch: 3 branches
BranchTrueFalseTotal
if (footer_len < 2 || footer[0] != '\n' || footer[footer_len - 1] != '\n') goto invalid; 15 0 15
if (footer_len < 2 || footer[0] != '\n' || footer[footer_len - 1] != '\n') goto invalid; 15 0 15
if (footer_len < 2 || footer[0] != '\n' || footer[footer_len - 1] != '\n') goto invalid; 0 15 15
277 15 ▶ if (footer_len > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (footer_len > 2) { 2 13 15
278 13 SV *text = sv_2mortal(newSVpvn(footer + 1, footer_len - 2));
279 13 ▶ if (memchr(SvPVX(text), 0, SvCUR(text)) || !tz_footer(tz, SvPVX(text))) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (memchr(SvPVX(text), 0, SvCUR(text)) || !tz_footer(tz, SvPVX(text))) goto invalid; 13 0 13
if (memchr(SvPVX(text), 0, SvCUR(text)) || !tz_footer(tz, SvPVX(text))) goto invalid; 11 2 13
280 }
281 }
282 14 loaded:
283 1792 tz->name = savepv(name);
284 1792 tz->next = *zones;
285 1792 *zones = tz;
286 1792 return tz;
287 4 invalid:
288 4 timezone_free(aTHX_ tz);
289 4 croak("Invalid timezone '%s' TZif data or POSIX rule", name);
290 return NULL;
291 }
292  
293 56 static const CHTimezone *column_timezone(pTHX_ TypeInfo *t) {
294 56 ▶ if (t->zones) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->zones) { 35 21 56
295 35 t->tz = timezone_load(aTHX_ t->zones, t->timezone);
296 27 t->zones = NULL;
297 }
298 48 return t->tz;
299 }
300  
301 48 static int64_t wall_to_epoch(const CHTimezone *tz, int64_t wall) {
302 48 int offsets[258], count = 0, i;
303 48 int64_t best = INT64_MAX;
304 uint32_t j;
305 48 ▶ if (!tz) return wall;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz) return wall; 0 48 48
306 65 ▶ for (j = 0; j < tz->type_count; j++) offsets[count++] = tz->offsets[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < tz->type_count; j++) offsets[count++] = tz->offsets[j]; 17 48 65
307 48 ▶ if (tz->footer) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz->footer) { 12 36 48
308 12 offsets[count++] = tz->standard;
309 12 ▶ if (tz->has_dst) offsets[count++] = tz->daylight;
Branch: 1 branch
BranchTrueFalseTotal
if (tz->has_dst) offsets[count++] = tz->daylight; 12 0 12
310 }
311 89 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 41 48 89
312 41 int64_t candidate = wall - offsets[i];
313 41 ▶ if (timezone_offset(tz, candidate) == offsets[i] && candidate < best) best = candidate;
Branch: 2 branches
BranchTrueFalseTotal
if (timezone_offset(tz, candidate) == offsets[i] && candidate < best) best = candidate; 15 26 41
if (timezone_offset(tz, candidate) == offsets[i] && candidate < best) best = candidate; 15 0 15
314 }
315 48 ▶ if (best != INT64_MAX) return best;
Branch: 1 branch
BranchTrueFalseTotal
if (best != INT64_MAX) return best; 15 33 48
316 /* ClickHouse resolves gaps using the offset after the forward jump. */
317 33 ▶ for (j = 0; j < tz->count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < tz->count; j++) { 0 33 33
318 0 ▶ int before = tz->offsets[j ? tz->indices[j - 1] : 0];
Branch: 1 branch
BranchTrueFalseTotal
int before = tz->offsets[j ? tz->indices[j - 1] : 0]; 0 0 0
319 0 int after = tz->offsets[tz->indices[j]];
320 0 ▶ if (after > before && wall >= tz->transitions[j] + before
Branch: 2 branches
BranchTrueFalseTotal
if (after > before && wall >= tz->transitions[j] + before 0 0 0
if (after > before && wall >= tz->transitions[j] + before 0 0 0
321 0 ▶ && wall < tz->transitions[j] + after) return wall - after;
Branch: 1 branch
BranchTrueFalseTotal
&& wall < tz->transitions[j] + after) return wall - after; 0 0 0
322 }
323 33 ▶ if (tz->footer && tz->has_dst) {
Branch: 2 branches
BranchTrueFalseTotal
if (tz->footer && tz->has_dst) { 0 33 33
if (tz->footer && tz->has_dst) { 0 0 0
324 0 int year = epoch_year(wall), y;
325 0 ▶ for (y = year - 1; y <= year + 1; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = year - 1; y <= year + 1; y++) { 0 0 0
326 0 int64_t start = rule_epoch(&tz->start, y, tz->standard, tz->standard);
327 0 int64_t end = rule_epoch(&tz->end, y, tz->standard, tz->daylight);
328 0 ▶ if (tz->daylight > tz->standard && wall >= start + tz->standard
Branch: 2 branches
BranchTrueFalseTotal
if (tz->daylight > tz->standard && wall >= start + tz->standard 0 0 0
if (tz->daylight > tz->standard && wall >= start + tz->standard 0 0 0
329 0 ▶ && wall < start + tz->daylight) return wall - tz->daylight;
Branch: 1 branch
BranchTrueFalseTotal
&& wall < start + tz->daylight) return wall - tz->daylight; 0 0 0
330 0 ▶ if (tz->standard > tz->daylight && wall >= end + tz->daylight
Branch: 2 branches
BranchTrueFalseTotal
if (tz->standard > tz->daylight && wall >= end + tz->daylight 0 0 0
if (tz->standard > tz->daylight && wall >= end + tz->daylight 0 0 0
331 0 ▶ && wall < end + tz->standard) return wall - tz->standard;
Branch: 1 branch
BranchTrueFalseTotal
&& wall < end + tz->standard) return wall - tz->standard; 0 0 0
332 }
333 }
334 33 return wall - timezone_offset(tz, wall);
335 }
336  
337 127 int32_t parse_date_string(pTHX_ const char *s, STRLEN len) {
338 127 ▶ if (!looks_like_date(s, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!looks_like_date(s, len)) 2 125 127
339 2 ▶ croak("Invalid date string (expected YYYY-MM-DD): %.*s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Invalid date string (expected YYYY-MM-DD): %.*s", 2 0 2
340 (int)(len > 30 ? 30 : len), s);
341  
342 125 int year = (s[0]-'0')*1000 + (s[1]-'0')*100 + (s[2]-'0')*10 + (s[3]-'0');
343 125 int month = (s[5]-'0')*10 + (s[6]-'0');
344 125 int day = (s[8]-'0')*10 + (s[9]-'0');
345  
346 /* looks_like_date only validates the digit shape; reject out-of-range
347 * components so the days_in_month loop can't read past the array, and
348 * reject impossible day-of-month combinations (e.g. 2024-04-31). */
349 125 ▶ if (month < 1 || month > 12 || day < 1 || day > 31)
Branch: 4 branches
BranchTrueFalseTotal
if (month < 1 || month > 12 || day < 1 || day > 31) 124 1 125
if (month < 1 || month > 12 || day < 1 || day > 31) 121 3 124
if (month < 1 || month > 12 || day < 1 || day > 31) 121 0 121
if (month < 1 || month > 12 || day < 1 || day > 31) 1 120 121
350 5 ▶ croak("Invalid date '%.*s': month/day out of range",
Branch: 1 branch
BranchTrueFalseTotal
croak("Invalid date '%.*s': month/day out of range", 5 0 5
351 (int)(len > 30 ? 30 : len), s);
352 240 int max_day = days_in_month[month-1]
353 120 ▶ + (month == 2 && is_leap_year(year) ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
+ (month == 2 && is_leap_year(year) ? 1 : 0); 3 117 120
+ (month == 2 && is_leap_year(year) ? 1 : 0); 2 1 3
354 120 ▶ if (day > max_day)
Branch: 1 branch
BranchTrueFalseTotal
if (day > max_day) 3 117 120
355 3 ▶ croak("Invalid date '%.*s': day %d exceeds month %d's length (%d)",
Branch: 1 branch
BranchTrueFalseTotal
croak("Invalid date '%.*s': day %d exceeds month %d's length (%d)", 3 0 3
356 (int)(len > 30 ? 30 : len), s, day, month, max_day);
357  
358 117 int32_t days = 0;
359 int y, m;
360  
361 23129 ▶ for (y = 1970; y < year; y++) days += is_leap_year(y) ? 366 : 365;
Branch: 2 branches
BranchTrueFalseTotal
for (y = 1970; y < year; y++) days += is_leap_year(y) ? 366 : 365; 5597 17415 23012
for (y = 1970; y < year; y++) days += is_leap_year(y) ? 366 : 365; 23012 117 23129
362 542 ▶ for (y = year; y < 1970; y++) days -= is_leap_year(y) ? 366 : 365;
Branch: 2 branches
BranchTrueFalseTotal
for (y = year; y < 1970; y++) days -= is_leap_year(y) ? 366 : 365; 102 323 425
for (y = year; y < 1970; y++) days -= is_leap_year(y) ? 366 : 365; 425 117 542
363  
364 633 ▶ for (m = 1; m < month; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 1; m < month; m++) { 516 117 633
365 516 days += days_in_month[m-1];
366 516 ▶ if (m == 2 && is_leap_year(year)) days++;
Branch: 2 branches
BranchTrueFalseTotal
if (m == 2 && is_leap_year(year)) days++; 85 431 516
if (m == 2 && is_leap_year(year)) days++; 41 44 85
367 }
368  
369 117 days += day - 1;
370 117 return days;
371 }
372  
373 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
374  
375 15 static void invalid_datetime(pTHX_ const char *s, STRLEN len) {
376 15 ▶ croak("Invalid datetime string: %.*s", (int)(len > 30 ? 30 : len), s);
Branch: 1 branch
BranchTrueFalseTotal
croak("Invalid datetime string: %.*s", (int)(len > 30 ? 30 : len), s); 15 0 15
377 }
378  
379 /* Parses " HH:MM[:SS]" after the date; returns the position just past it. */
380 86 static STRLEN parse_time_part(pTHX_ const char *s, STRLEN len, int64_t *seconds) {
381 86 STRLEN pos = 10;
382 86 *seconds = 0;
383 156 ▶ while (pos < len && s[pos] == ' ') pos++;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && s[pos] == ' ') pos++; 154 2 156
while (pos < len && s[pos] == ' ') pos++; 70 84 154
384 86 ▶ if (pos == len || s[10] == 'Z' || s[10] == 'z' || s[10] == '+' || s[10] == '-')
Branch: 5 branches
BranchTrueFalseTotal
if (pos == len || s[10] == 'Z' || s[10] == 'z' || s[10] == '+' || s[10] == '-') 84 2 86
if (pos == len || s[10] == 'Z' || s[10] == 'z' || s[10] == '+' || s[10] == '-') 84 0 84
if (pos == len || s[10] == 'Z' || s[10] == 'z' || s[10] == '+' || s[10] == '-') 84 0 84
if (pos == len || s[10] == 'Z' || s[10] == 'z' || s[10] == '+' || s[10] == '-') 84 0 84
if (pos == len || s[10] == 'Z' || s[10] == 'z' || s[10] == '+' || s[10] == '-') 0 84 84
385 2 return 10;
386 84 ▶ if ((s[10] != ' ' && s[10] != 'T') || len < 16 || s[13] != ':'
Branch: 4 branches
BranchTrueFalseTotal
if ((s[10] != ' ' && s[10] != 'T') || len < 16 || s[13] != ':' 14 70 84
if ((s[10] != ' ' && s[10] != 'T') || len < 16 || s[13] != ':' 14 0 14
if ((s[10] != ' ' && s[10] != 'T') || len < 16 || s[13] != ':' 80 4 84
if ((s[10] != ' ' && s[10] != 'T') || len < 16 || s[13] != ':' 78 2 80
387 78 ▶ || !IS_DIGIT(s[11]) || !IS_DIGIT(s[12])
Branch: 4 branches
BranchTrueFalseTotal
|| !IS_DIGIT(s[11]) || !IS_DIGIT(s[12]) 78 0 78
|| !IS_DIGIT(s[11]) || !IS_DIGIT(s[12]) 77 1 78
|| !IS_DIGIT(s[11]) || !IS_DIGIT(s[12]) 77 0 77
|| !IS_DIGIT(s[11]) || !IS_DIGIT(s[12]) 76 1 77
388 76 ▶ || !IS_DIGIT(s[14]) || !IS_DIGIT(s[15]))
Branch: 4 branches
BranchTrueFalseTotal
|| !IS_DIGIT(s[14]) || !IS_DIGIT(s[15])) 76 0 76
|| !IS_DIGIT(s[14]) || !IS_DIGIT(s[15])) 76 0 76
|| !IS_DIGIT(s[14]) || !IS_DIGIT(s[15])) 76 0 76
|| !IS_DIGIT(s[14]) || !IS_DIGIT(s[15])) 0 76 76
389 8 invalid_datetime(aTHX_ s, len);
390 76 *seconds = ((s[11]-'0')*10 + (s[12]-'0')) * 3600
391 76 + ((s[14]-'0')*10 + (s[15]-'0')) * 60;
392 76 ▶ if (len == 16 || s[16] != ':') return 16;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 16 || s[16] != ':') return 16; 72 4 76
if (len == 16 || s[16] != ':') return 16; 6 66 72
393 66 ▶ if (len < 19 || !IS_DIGIT(s[17]) || !IS_DIGIT(s[18]))
Branch: 5 branches
BranchTrueFalseTotal
if (len < 19 || !IS_DIGIT(s[17]) || !IS_DIGIT(s[18])) 64 2 66
if (len < 19 || !IS_DIGIT(s[17]) || !IS_DIGIT(s[18])) 64 0 64
if (len < 19 || !IS_DIGIT(s[17]) || !IS_DIGIT(s[18])) 64 0 64
if (len < 19 || !IS_DIGIT(s[17]) || !IS_DIGIT(s[18])) 64 0 64
if (len < 19 || !IS_DIGIT(s[17]) || !IS_DIGIT(s[18])) 1 63 64
394 3 invalid_datetime(aTHX_ s, len);
395 63 *seconds += (s[17]-'0')*10 + (s[18]-'0');
396 63 return 19;
397 }
398  
399 15 static int parse_tz_offset(pTHX_ const char *s, STRLEN len, STRLEN pos) {
400 15 ▶ if (pos >= len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= len) return 0; 0 15 15
401 15 char c = s[pos];
402 15 ▶ if (c == 'Z' || c == 'z') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'Z' || c == 'z') return 0; 8 7 15
if (c == 'Z' || c == 'z') return 0; 0 8 8
403 8 ▶ if (c != '+' && c != '-') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c != '+' && c != '-') return 0; 1 7 8
if (c != '+' && c != '-') return 0; 0 1 1
404 8 ▶ int sign = (c == '-') ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
int sign = (c == '-') ? -1 : 1; 1 7 8
405 8 pos++;
406 8 ▶ if (pos + 2 > len
Branch: 1 branch
BranchTrueFalseTotal
if (pos + 2 > len 8 0 8
407 8 ▶ || s[pos] < '0' || s[pos] > '9'
Branch: 2 branches
BranchTrueFalseTotal
|| s[pos] < '0' || s[pos] > '9' 8 0 8
|| s[pos] < '0' || s[pos] > '9' 8 0 8
408 8 ▶ || s[pos+1] < '0' || s[pos+1] > '9')
Branch: 2 branches
BranchTrueFalseTotal
|| s[pos+1] < '0' || s[pos+1] > '9') 8 0 8
|| s[pos+1] < '0' || s[pos+1] > '9') 0 8 8
409 0 ▶ croak("Invalid datetime timezone: %.*s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Invalid datetime timezone: %.*s", 0 0 0
410 (int)(len > 30 ? 30 : len), s);
411 8 int hh = (s[pos]-'0')*10 + (s[pos+1]-'0');
412 8 pos += 2;
413 8 int mm = 0;
414 8 ▶ if (pos < len && s[pos] == ':') pos++;
Branch: 2 branches
BranchTrueFalseTotal
if (pos < len && s[pos] == ':') pos++; 8 0 8
if (pos < len && s[pos] == ':') pos++; 7 1 8
415 8 ▶ if (pos + 2 <= len
Branch: 1 branch
BranchTrueFalseTotal
if (pos + 2 <= len 8 0 8
416 8 ▶ && s[pos] >= '0' && s[pos] <= '9'
Branch: 2 branches
BranchTrueFalseTotal
&& s[pos] >= '0' && s[pos] <= '9' 8 0 8
&& s[pos] >= '0' && s[pos] <= '9' 8 0 8
417 8 ▶ && s[pos+1] >= '0' && s[pos+1] <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
&& s[pos+1] >= '0' && s[pos+1] <= '9') { 8 0 8
&& s[pos+1] >= '0' && s[pos+1] <= '9') { 8 0 8
418 8 mm = (s[pos]-'0')*10 + (s[pos+1]-'0');
419 }
420 8 return sign * (hh * 3600 + mm * 60);
421 }
422  
423 75 static int64_t zone_wall_time(pTHX_ const char *s, STRLEN len, STRLEN pos,
424 int64_t wall, TypeInfo *t) {
425 75 ▶ if (pos < len && (s[pos] == 'Z' || s[pos] == 'z' || s[pos] == '+' || s[pos] == '-'))
Branch: 5 branches
BranchTrueFalseTotal
if (pos < len && (s[pos] == 'Z' || s[pos] == 'z' || s[pos] == '+' || s[pos] == '-')) 21 54 75
if (pos < len && (s[pos] == 'Z' || s[pos] == 'z' || s[pos] == '+' || s[pos] == '-')) 14 7 21
if (pos < len && (s[pos] == 'Z' || s[pos] == 'z' || s[pos] == '+' || s[pos] == '-')) 14 0 14
if (pos < len && (s[pos] == 'Z' || s[pos] == 'z' || s[pos] == '+' || s[pos] == '-')) 7 7 14
if (pos < len && (s[pos] == 'Z' || s[pos] == 'z' || s[pos] == '+' || s[pos] == '-')) 1 6 7
426 15 return wall - parse_tz_offset(aTHX_ s, len, pos);
427 62 ▶ while (pos < len && s[pos] == ' ') pos++;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && s[pos] == ' ') pos++; 6 56 62
while (pos < len && s[pos] == ' ') pos++; 2 4 6
428 60 ▶ if (pos != len) invalid_datetime(aTHX_ s, len);
Branch: 1 branch
BranchTrueFalseTotal
if (pos != len) invalid_datetime(aTHX_ s, len); 4 56 60
429 56 return wall_to_epoch(column_timezone(aTHX_ t), wall);
430 }
431  
432 58 uint32_t parse_datetime_string(pTHX_ const char *s, STRLEN len, TypeInfo *t) {
433 58 int32_t days = parse_date_string(aTHX_ s, len);
434 int64_t seconds, epoch;
435 57 STRLEN pos = parse_time_part(aTHX_ s, len, &seconds);
436 51 ▶ if (pos == 19 && pos < len && s[pos] == '.') {
Branch: 3 branches
BranchTrueFalseTotal
if (pos == 19 && pos < len && s[pos] == '.') { 45 6 51
if (pos == 19 && pos < len && s[pos] == '.') { 10 35 45
if (pos == 19 && pos < len && s[pos] == '.') { 2 8 10
437 2 pos++;
438 8 ▶ while (pos < len && IS_DIGIT(s[pos])) pos++;
Branch: 3 branches
BranchTrueFalseTotal
while (pos < len && IS_DIGIT(s[pos])) pos++; 8 0 8
while (pos < len && IS_DIGIT(s[pos])) pos++; 7 1 8
while (pos < len && IS_DIGIT(s[pos])) pos++; 6 1 7
439 }
440 51 epoch = zone_wall_time(aTHX_ s, len, pos, (int64_t)days * 86400 + seconds, t);
441 /* DateTime is UInt32 on the wire (1970-01-01 .. 2106-02-07). Reject
442 * out-of-range epochs explicitly rather than silently wrapping. */
443 41 ▶ if (epoch < 0 || epoch > 0xFFFFFFFFLL)
Branch: 2 branches
BranchTrueFalseTotal
if (epoch < 0 || epoch > 0xFFFFFFFFLL) 40 1 41
if (epoch < 0 || epoch > 0xFFFFFFFFLL) 1 39 40
444 2 ▶ croak("DateTime out of UInt32 range "
Branch: 1 branch
BranchTrueFalseTotal
croak("DateTime out of UInt32 range " 2 0 2
445 "(1970-01-01 .. 2106-02-07 06:28:15): %.*s",
446 (int)(len > 30 ? 30 : len), s);
447 39 return (uint32_t)epoch;
448 }
449  
450 /* Multiply a Perl double by 10^precision, croak on Inf/NaN/overflow,
451 * return the rounded int64. Shared by the SvNOK and string-number
452 * branches of the T_DATETIME64 encode path. */
453 7 int64_t dt64_double_to_int64(pTHX_ double v, int precision) {
454 7 double d = v * decimal_pow10(precision);
455 /* Same bound as the T_DECIMAL64 path: 2^63 in double form. Strict
456 * inequality so 2^63 itself (which would overflow int64_t after
457 * llround) is rejected. */
458 7 ▶ if (!(isfinite(d) && d > -9.223372036854776e18 && d < 9.223372036854776e18))
Branch: 3 branches
BranchTrueFalseTotal
if (!(isfinite(d) && d > -9.223372036854776e18 && d < 9.223372036854776e18)) 5 2 7
if (!(isfinite(d) && d > -9.223372036854776e18 && d < 9.223372036854776e18)) 5 0 5
if (!(isfinite(d) && d > -9.223372036854776e18 && d < 9.223372036854776e18)) 0 5 5
459 2 croak("DateTime64 overflow (or non-finite value)");
460 5 return (int64_t)llround(d);
461 }
462  
463 29 int64_t parse_datetime64_string(pTHX_ const char *s, STRLEN len, int precision, TypeInfo *t) {
464 29 int32_t days = parse_date_string(aTHX_ s, len);
465 29 int64_t seconds, base, frac = 0;
466 29 STRLEN tz_pos = parse_time_part(aTHX_ s, len, &seconds);
467  
468 24 ▶ if (tz_pos == 19 && tz_pos < len && s[tz_pos] == '.') {
Branch: 3 branches
BranchTrueFalseTotal
if (tz_pos == 19 && tz_pos < len && s[tz_pos] == '.') { 18 6 24
if (tz_pos == 19 && tz_pos < len && s[tz_pos] == '.') { 13 5 18
if (tz_pos == 19 && tz_pos < len && s[tz_pos] == '.') { 11 2 13
469 int i;
470 53 ▶ for (i = 0; i < precision && (20 + i) < (int)len; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < precision && (20 + i) < (int)len; i++) { 42 11 53
for (i = 0; i < precision && (20 + i) < (int)len; i++) { 42 0 42
471 42 char c = s[20 + i];
472 42 ▶ if (!IS_DIGIT(c)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!IS_DIGIT(c)) break; 42 0 42
if (!IS_DIGIT(c)) break; 42 0 42
473 42 frac = frac * 10 + (c - '0');
474 }
475 11 ▶ for (; i < precision; i++) frac *= 10;
Branch: 1 branch
BranchTrueFalseTotal
for (; i < precision; i++) frac *= 10; 0 11 11
476 11 tz_pos = 20;
477 53 ▶ while (tz_pos < len && IS_DIGIT(s[tz_pos])) tz_pos++;
Branch: 3 branches
BranchTrueFalseTotal
while (tz_pos < len && IS_DIGIT(s[tz_pos])) tz_pos++; 45 8 53
while (tz_pos < len && IS_DIGIT(s[tz_pos])) tz_pos++; 44 1 45
while (tz_pos < len && IS_DIGIT(s[tz_pos])) tz_pos++; 42 2 44
478 }
479 24 base = zone_wall_time(aTHX_ s, len, tz_pos, (int64_t)days * 86400 + seconds, t);
480  
481 /* precision is capped at 0..9, so scale fits int64 comfortably - but
482 * base * scale does not, and would wrap to a negative tick count
483 * (signed overflow being undefined besides). Check before the
484 * multiply, not after. */
485 22 ▶ int64_t scale = (precision >= 0 && precision <= 18)
Branch: 1 branch
BranchTrueFalseTotal
int64_t scale = (precision >= 0 && precision <= 18) 22 0 22
486 44 ▶ ? (int64_t)pow10_u64[precision] : 1;
Branch: 1 branch
BranchTrueFalseTotal
? (int64_t)pow10_u64[precision] : 1; 22 0 22
487 22 ▶ if (scale > 1 && (base > INT64_MAX / scale || base < INT64_MIN / scale))
Branch: 3 branches
BranchTrueFalseTotal
if (scale > 1 && (base > INT64_MAX / scale || base < INT64_MIN / scale)) 22 0 22
if (scale > 1 && (base > INT64_MAX / scale || base < INT64_MIN / scale)) 20 2 22
if (scale > 1 && (base > INT64_MAX / scale || base < INT64_MIN / scale)) 0 20 20
488 2 ▶ croak("DateTime64(%d) out of range (value overflows 64-bit ticks): "
Branch: 1 branch
BranchTrueFalseTotal
croak("DateTime64(%d) out of range (value overflows 64-bit ticks): " 2 0 2
489 "%.*s", precision, (int)(len > 30 ? 30 : len), s);
490 20 int64_t ticks = base * scale;
491 20 ▶ if (frac > 0 && ticks > INT64_MAX - frac)
Branch: 2 branches
BranchTrueFalseTotal
if (frac > 0 && ticks > INT64_MAX - frac) 9 11 20
if (frac > 0 && ticks > INT64_MAX - frac) 0 9 9
492 0 ▶ croak("DateTime64(%d) out of range (value overflows 64-bit ticks): "
Branch: 1 branch
BranchTrueFalseTotal
croak("DateTime64(%d) out of range (value overflows 64-bit ticks): " 0 0 0
493 "%.*s", precision, (int)(len > 30 ? 30 : len), s);
494 20 return ticks + frac;
495 }
« blib/lib/ClickHouse/Encoder/TCP.pm ↑ Summary scalar_kind.c »