ClickHouse-Encoder
view release on metacpan or search on metacpan
*seconds = ((s[11]-'0')*10 + (s[12]-'0')) * 3600
+ ((s[14]-'0')*10 + (s[15]-'0')) * 60;
if (len == 16 || s[16] != ':') return 16;
if (len < 19 || !IS_DIGIT(s[17]) || !IS_DIGIT(s[18]))
invalid_datetime(aTHX_ s, len);
*seconds += (s[17]-'0')*10 + (s[18]-'0');
return 19;
}
static int parse_tz_offset(pTHX_ const char *s, STRLEN len, STRLEN pos) {
if (pos >= len) return 0;
char c = s[pos];
if (c == 'Z' || c == 'z') return 0;
if (c != '+' && c != '-') return 0;
int sign = (c == '-') ? -1 : 1;
pos++;
if (pos + 2 > len
|| s[pos] < '0' || s[pos] > '9'
|| s[pos+1] < '0' || s[pos+1] > '9')
croak("Invalid datetime timezone: %.*s",
(int)(len > 30 ? 30 : len), s);
int hh = (s[pos]-'0')*10 + (s[pos+1]-'0');
pos += 2;
int mm = 0;
if (pos < len && s[pos] == ':') pos++;
if (pos + 2 <= len
&& s[pos] >= '0' && s[pos] <= '9'
&& s[pos+1] >= '0' && s[pos+1] <= '9') {
mm = (s[pos]-'0')*10 + (s[pos+1]-'0');
}
return sign * (hh * 3600 + mm * 60);
}
static int64_t zone_wall_time(pTHX_ const char *s, STRLEN len, STRLEN pos,
int64_t wall, TypeInfo *t) {
if (pos < len && (s[pos] == 'Z' || s[pos] == 'z' || s[pos] == '+' || s[pos] == '-'))
return wall - parse_tz_offset(aTHX_ s, len, pos);
while (pos < len && s[pos] == ' ') pos++;
if (pos != len) invalid_datetime(aTHX_ s, len);
return wall_to_epoch(column_timezone(aTHX_ t), wall);
}
uint32_t parse_datetime_string(pTHX_ const char *s, STRLEN len, TypeInfo *t) {
int32_t days = parse_date_string(aTHX_ s, len);
int64_t seconds, epoch;
STRLEN pos = parse_time_part(aTHX_ s, len, &seconds);
if (pos == 19 && pos < len && s[pos] == '.') {
pos++;
while (pos < len && IS_DIGIT(s[pos])) pos++;
}
epoch = zone_wall_time(aTHX_ s, len, pos, (int64_t)days * 86400 + seconds, t);
/* DateTime is UInt32 on the wire (1970-01-01 .. 2106-02-07). Reject
* out-of-range epochs explicitly rather than silently wrapping. */
if (epoch < 0 || epoch > 0xFFFFFFFFLL)
croak("DateTime out of UInt32 range "
"(1970-01-01 .. 2106-02-07 06:28:15): %.*s",
(int)(len > 30 ? 30 : len), s);
return (uint32_t)epoch;
}
/* Multiply a Perl double by 10^precision, croak on Inf/NaN/overflow,
* return the rounded int64. Shared by the SvNOK and string-number
* branches of the T_DATETIME64 encode path. */
int64_t dt64_double_to_int64(pTHX_ double v, int precision) {
double d = v * decimal_pow10(precision);
/* Same bound as the T_DECIMAL64 path: 2^63 in double form. Strict
* inequality so 2^63 itself (which would overflow int64_t after
* llround) is rejected. */
if (!(isfinite(d) && d > -9.223372036854776e18 && d < 9.223372036854776e18))
croak("DateTime64 overflow (or non-finite value)");
return (int64_t)llround(d);
}
int64_t parse_datetime64_string(pTHX_ const char *s, STRLEN len, int precision, TypeInfo *t) {
int32_t days = parse_date_string(aTHX_ s, len);
int64_t seconds, base, frac = 0;
STRLEN tz_pos = parse_time_part(aTHX_ s, len, &seconds);
if (tz_pos == 19 && tz_pos < len && s[tz_pos] == '.') {
int i;
for (i = 0; i < precision && (20 + i) < (int)len; i++) {
char c = s[20 + i];
if (!IS_DIGIT(c)) break;
frac = frac * 10 + (c - '0');
}
for (; i < precision; i++) frac *= 10;
tz_pos = 20;
while (tz_pos < len && IS_DIGIT(s[tz_pos])) tz_pos++;
}
base = zone_wall_time(aTHX_ s, len, tz_pos, (int64_t)days * 86400 + seconds, t);
/* precision is capped at 0..9, so scale fits int64 comfortably - but
* base * scale does not, and would wrap to a negative tick count
* (signed overflow being undefined besides). Check before the
* multiply, not after. */
int64_t scale = (precision >= 0 && precision <= 18)
? (int64_t)pow10_u64[precision] : 1;
if (scale > 1 && (base > INT64_MAX / scale || base < INT64_MIN / scale))
croak("DateTime64(%d) out of range (value overflows 64-bit ticks): "
"%.*s", precision, (int)(len > 30 ? 30 : len), s);
int64_t ticks = base * scale;
if (frac > 0 && ticks > INT64_MAX - frac)
croak("DateTime64(%d) out of range (value overflows 64-bit ticks): "
"%.*s", precision, (int)(len > 30 ? 30 : len), s);
return ticks + frac;
}
( run in 1.478 second using v1.01-cache-2.11-cpan-60884a8df76 )