Skip to content

Commit 42a8275

Browse files
committed
Always interpret dates as UTC / database local time
This avoids dates traveling across 24 hour boundaries, with no good way to avoid. I think most people would expect this behavior, rather than the previous behavior. Fixes #398
1 parent 411997a commit 42a8275

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* `dbBind()` and `dbFetch()` now support multiple result sets (@vkapartzianis, #234)
1515
* `dbFetch()` now verifies that `n` is a valid input.
1616
* `dbQuoteIdentifier()` now uses the input names (if any).
17+
* Dates are now always interpreted as being in the database's local time zone, regardless of the `timezone` parameter (#398)
1718

1819
# odbc 1.2.3
1920

src/odbc_result.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ nanodbc::date odbc_result::as_date(double value) {
355355
using namespace std::chrono;
356356
auto utc_time = system_clock::from_time_t(static_cast<std::time_t>(value));
357357

358-
auto civil_time = cctz::convert(utc_time, c_->timezone());
358+
auto civil_time = cctz::convert(utc_time, cctz::utc_time_zone());
359359
dt.day = civil_time.day();
360360
dt.month = civil_time.month();
361361
dt.year = civil_time.year();
@@ -547,8 +547,8 @@ void odbc_result::add_classes(
547547
case raw_t:
548548
// FIXME: Use new_blob()
549549
x.attr("ptype") = Rcpp::RawVector::create();
550-
x.attr("class") =
551-
Rcpp::CharacterVector::create("blob", "vctrs_list_of", "vctrs_vctr", "list");
550+
x.attr("class") = Rcpp::CharacterVector::create(
551+
"blob", "vctrs_list_of", "vctrs_vctr", "list");
552552
break;
553553
default:
554554
break;

tests/testthat/test-SQLServer.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,10 @@ test_that("SQLServer", {
147147
received <- DBI::dbReadTable(con, tblName)
148148
expect_equal(values, received)
149149
})
150+
151+
test_that("dates should always be interpreted in the system time zone (#398)", {
152+
con <- DBItest:::connect(DBItest:::get_default_context(), timezone = "America/Chicago")
153+
res <- dbGetQuery(con, "SELECT ?", params = as.Date("2019-01-01"))
154+
expect_equal(res[[1]], "2019-01-01")
155+
})
150156
})

0 commit comments

Comments
 (0)