lib/duckdb/converter.rb in duckdb-1.0.0.1 vs lib/duckdb/converter.rb in duckdb-1.0.0.2

- old
+ new

@@ -8,10 +8,12 @@ module Converter HALF_HUGEINT_BIT = 64 HALF_HUGEINT = 1 << HALF_HUGEINT_BIT FLIP_HUGEINT = 1 << 63 + EPOCH = Time.local(1970, 1, 1) + EPOCH_UTC = Time.new(1970, 1, 1, 0, 0, 0, 0) module_function def _to_date(year, month, day) Date.new(year, month, day) @@ -27,9 +29,66 @@ '%<hour>02d:%<minute>02d:%<second>02d.%<microsecond>06d', hour: hour, minute: minute, second: second, microsecond: microsecond + ) + ) + end + + def _to_time_from_duckdb_timestamp_s(time) + EPOCH + time + end + + def _to_time_from_duckdb_timestamp_ms(time) + tm = EPOCH + (time / 1000) + Time.local(tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec, time % 1000 * 1000) + end + + def _to_time_from_duckdb_timestamp_ns(time) + tm = EPOCH + (time / 1_000_000_000) + Time.local(tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec, time % 1_000_000_000 / 1000) + end + + def _to_time_from_duckdb_time_tz(hour, min, sec, micro, timezone) + sign = '+' + if timezone.negative? + timezone = -timezone + sign = '-' + end + + tzhour = timezone / 3600 + tzmin = (timezone % 3600) / 60 + + Time.parse( + format( + '%<hour>02d:%<min>02d:%<sec>02d.%<micro>06d%<sign>s%<tzhour>02d:%<tzmin>02d', + hour: hour, + min: min, + sec: sec, + micro: micro, + sign: sign, + tzhour: tzhour, + tzmin: tzmin + ) + ) + end + + def _to_time_from_duckdb_timestamp_tz(bits) + micro = bits % 1_000_000 + sec = (bits / 1_000_000) + time = EPOCH_UTC + sec + + Time.parse( + format( + '%<year>04d-%<mon>02d-%<day>02d %<hour>02d:%<min>02d:%<sec>02d.%<micro>06d +0000', + year: time.year, + mon: time.month, + day: time.day, + hour: time.hour, + min: time.min, + sec: time.sec, + micro: micro ) ) end def _to_hugeint_from_vector(lower, upper)