Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# bigrquery (development version)

* Change rendering for uploading `POSIXt`-class objects to 6 digits
with `"%OS6"` (#660). This means that all timestamps will have
microsecond resolution when uploading data.

* Set the `"tzone"` attribute of uploaded `POSIXt` columns to
`Sys.timezone()` when the timezone is empty or not defined (#660).
Previously, if unset then it may be interpreted as UTC regardless of
the system's local timezone, resulting in a round-trip value
differing by the TZ offset.

# bigrquery 1.6.1

* Fix test failure on CRAN.
Expand Down
8 changes: 7 additions & 1 deletion R/bq-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,13 @@ export_json <- function(values) {

# Convert times to canonical format
is_time <- vapply(values, function(x) inherits(x, "POSIXt"), logical(1))
values[is_time] <- lapply(values[is_time], format, "%Y-%m-%d %H:%M:%S")
values[is_time] <- lapply(values[is_time], function(x) {
if (is.null(attr(x, "tzone")) || attr(x, "tzone") %in% c(NA, "")) {
attr(x, "tzone") <- Sys.timezone()
}
ifelse(is.na(x), NA_character_,
paste(format(x, "%Y-%m-%d %H:%M:%OS6"), attr(x, "tzone")))
})

# Convert wk_wkt to text
is_wk <- vapply(values, function(x) inherits(x, "wk_vctr"), logical(1))
Expand Down
45 changes: 45 additions & 0 deletions tests/testthat/test-bq-digits-secs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
test_that("'POSIXt' preserves microsecond precision", {
tbl <- data.frame(psx = trunc.Date(Sys.time(), units = "secs") + 0.123456)
attr(tbl$psx, "tzone") <- "UTC"

ds <- bq_table(bq_test_project(), "basedata", "datatypes")
defer(try(bq_table_delete(ds), silent = TRUE))

bq_table_upload(ds, tbl)
tbl2 <- bq_table_download(ds)

expect_equal(tbl$psx, tbl2$psx)
})

test_that("'tzone' unset and different values handled correctly", {
# the bug only produced problems when the system TZ has a non-zero
# offset (i.e., not UTC)
withr::local_envvar(TZ = "America/New_York")

ds <- bq_table(bq_test_project(), "basedata", "datatypes")
defer(try(bq_table_delete(ds), silent = TRUE))

tbl1in <- tibble(psx = trunc.Date(Sys.time(), units = "secs") + 0.123456)
attr(tbl1in$psx, "tzone") <- NULL
bq_table_upload(ds, tbl1in)
tbl1out <- bq_table_download(ds)
bq_table_delete(ds)
attr(tbl1out$psx, "tzone") <- NULL
expect_equal(tbl1in$psx, tbl1out$psx)
expect_true(abs(as.numeric(tbl5$psx - tbl$psx, units = "hours")) < 1)

tbl2in <- tibble(psx = trunc.Date(Sys.time(), units = "secs") + 0.123456)
attr(tbl2in$psx, "tzone") <- "UTC"
bq_table_upload(ds, tbl2in)
tbl2out <- bq_table_download(ds)
bq_table_delete(ds)
expect_equal(tbl2in$psx, tbl2out$psx)

tbl3in <- tibble(psx = trunc.Date(Sys.time(), units = "secs") + 0.123456)
attr(tbl3in$psx, "tzone") <- "America/New_York"
bq_table_upload(ds, tbl3in)
tbl3out <- bq_table_download(ds)
bq_table_delete(ds)
attr(tbl3out$psx, "tzone") <- attr(tbl3in$psx, "tzone")
expect_equal(tbl3in$psx, tbl3out$psx)
})