From 10b9414dc3f1cb573ee19bc03ebefac53b265ce9 Mon Sep 17 00:00:00 2001 From: toby <43851547+toppyy@users.noreply.github.com> Date: Wed, 8 Jan 2025 19:24:35 +0200 Subject: [PATCH] floor sub-day precision date before casting to int --- src/types.cpp | 2 +- tests/testthat/test-date.R | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-date.R diff --git a/src/types.cpp b/src/types.cpp index b691bfd5b..8858d0a78 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -328,7 +328,7 @@ double RDoubleType::Convert(double val) { } date_t RDateType::Convert(double val) { - return date_t((int32_t)val); + return date_t((int32_t) std::floor(val)); } timestamp_t RTimestampType::Convert(double val) { diff --git a/tests/testthat/test-date.R b/tests/testthat/test-date.R new file mode 100644 index 000000000..bb2bd8b79 --- /dev/null +++ b/tests/testthat/test-date.R @@ -0,0 +1,20 @@ + + +test_that("Conversion of sub-dates prior Posix origin is correct", { + con <- dbConnect(duckdb()) + on.exit(dbDisconnect(con, shutdown = TRUE)) + + df <- data.frame( + d = as.Date(c(-1.1, -0.1, 0, 0.1, 1.1), origin = "1970-01-01") + ) + + duckdb_register(con, "df", df) + + res <- sql("from df", con) + + expect_identical( + as.character(df$d), + as.character(res$d) + ) +}) +