Skip to content

Commit

Permalink
time_t size introspection
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd committed Jul 12, 2023
1 parent 3f26819 commit 16e364c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions c/driver/sqlite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ foreach(LIB_TARGET ${ADBC_LIBRARIES})
target_compile_definitions(${LIB_TARGET} PRIVATE ADBC_EXPORTING)
endforeach()

include(CheckTypeSize)
check_type_size("time_t" SIZEOF_TIME_T)
add_definitions(-DSIZEOF_TIME_T=${SIZEOF_TIME_T})

if(ADBC_TEST_LINKAGE STREQUAL "shared")
set(TEST_LINK_LIBS adbc_driver_sqlite_shared)
else()
Expand Down
22 changes: 18 additions & 4 deletions c/driver/sqlite/statement_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>

Expand Down Expand Up @@ -94,7 +95,6 @@ AdbcStatusCode AdbcSqliteBinderSetArrayStream(struct AdbcSqliteBinder* binder,

/* caller is responsible for freeing data */
static const char* ArrowTimestampToIsoString(int64_t value, enum ArrowTimeUnit unit) {
int64_t seconds;
int scale = 1;
int strlen = 20;
int rem = 0;
Expand All @@ -115,20 +115,34 @@ static const char* ArrowTimestampToIsoString(int64_t value, enum ArrowTimeUnit u
strlen = 30;
break;
}
seconds = value / scale;

int64_t seconds = value / scale;
time_t time;

#if SIZEOF_TIME_T < 8
if ((seconds > INT32_MAX) || (seconds < INT32_MIN)) {
// TODO: give better error visibility
return NULL;
}
time = (time_t)seconds;
#else
time = seconds;
#endif

rem = value % scale;

if (rem < 0) {
rem = scale + rem;
}

struct tm broken_down_time;

#if defined(_WIN32)
if (gmtime_s(&broken_down_time, &seconds) != 0) {
if (gmtime_s(&broken_down_time, &time) != 0) {
return NULL;
}
#else
if (gmtime_r(&seconds, &broken_down_time) != &broken_down_time) {
if (gmtime_r(&time, &broken_down_time) != &broken_down_time) {
return NULL;
}
#endif
Expand Down

0 comments on commit 16e364c

Please sign in to comment.