Repository navigation
Closed
Description
If I am not mistaken, the timezone argument of the connection is ignored when casting a date string to a timestamp with timezone.
library(DBI)
Sys.timezone()
#> [1] "Europe/Berlin"
query <- "SELECT '2020-05-04'::TIMESTAMPTZ;"
con <- dbConnect(RPostgres::Postgres(), dbname="postgres", timezone = "America/Chicago")
dbGetQuery(con, "SHOW TIMEZONE;")
#> TimeZone
#> 1 America/Chicago
res <- dbGetQuery(con, query)
res
#> timestamptz
#> 1 2020-05-04 02:00:00
res[[1,1]]
#> [1] "2020-05-04 02:00:00 CEST"
dbDisconnect(con)With #222 in mind, it seems that this is returned as 2020-05-04 00:00:00 in UTC and then a +2 hour offset for my Sys.timezone() is added.
The offset from ‘America/Chicago’ to ‘Europe/Berlin’ is usually +7 hours, so IMHO this should either return 2020-05-04 00:00:00 CDT or 2020-05-04 07:00:00 CEST?
When setting a different timezone for the connection, I get the same output.
con <- dbConnect(RPostgres::Postgres(), dbname="postgres", timezone = "Europe/London")
dbGetQuery(con, "SHOW TIMEZONE;")
#> TimeZone
#> 1 Europe/London
res <- dbGetQuery(con, query)
res
#> timestamptz
#> 1 2020-05-04 02:00:00
res[[1,1]]
#> [1] "2020-05-04 02:00:00 CEST"
dbDisconnect(con)psql shows this:
postgres=# SET TIMEZONE TO 'America/Chicago';
SET
postgres=# SHOW TIMEZONE;
TimeZone
-----------------
America/Chicago
(1 Zeile)
postgres=# SELECT '2020-05-04'::TIMESTAMPTZ;
timestamptz
------------------------
2020-05-04 00:00:00-05
(1 Zeile)Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#> setting value
#> version R version 3.6.3 (2020-02-29)
#> os Windows 10 x64
#> system x86_64, mingw32
#> ui RTerm
#> language en
#> collate German_Germany.1252
#> ctype German_Germany.1252
#> tz America/Chicago
#> date 2020-05-05
#>
#> - Packages -------------------------------------------------------------------
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.1)
#> backports 1.1.6 2020-04-05 [1] CRAN (R 3.6.2)
#> bit 1.1-15.2 2020-02-10 [1] CRAN (R 3.6.2)
#> bit64 0.9-7 2017-05-08 [1] CRAN (R 3.6.0)
#> blob 1.2.1 2020-01-20 [1] CRAN (R 3.6.2)
#> cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3)
#> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.1)
#> DBI * 1.1.0 2019-12-15 [1] CRAN (R 3.6.1)
#> digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.2)
#> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.1)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.1)
#> fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.2)
#> fs 1.4.1.9000 2020-05-04 [1] Github (r-lib/fs@bdc82be)
#> glue 1.4.0 2020-04-03 [1] CRAN (R 3.6.2)
#> highr 0.8 2019-03-20 [1] CRAN (R 3.6.1)
#> hms 0.5.3 2020-01-08 [1] CRAN (R 3.6.2)
#> htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.1)
#> knitr 1.28 2020-02-06 [1] CRAN (R 3.6.2)
#> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.3)
#> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.1)
#> pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.2)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.1)
#> purrr 0.3.4 2020-04-17 [1] CRAN (R 3.6.3)
#> Rcpp 1.0.4.6 2020-04-09 [1] CRAN (R 3.6.3)
#> reprex 0.3.0.9001 2020-05-04 [1] Github (tidyverse/reprex@a019cc4)
#> rlang 0.4.6 2020-05-02 [1] CRAN (R 3.6.3)
#> rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.6.2)
#> RPostgres 1.2.0.9000 2020-05-04 [1] Github (r-dbi/RPostgres@f2d698c)
#> rstudioapi 0.11 2020-02-07 [1] CRAN (R 3.6.2)
#> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.1)
#> stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.2)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.1)
#> styler 1.3.2 2020-02-23 [1] CRAN (R 3.6.2)
#> tibble 3.0.1 2020-04-20 [1] CRAN (R 3.6.3)
#> vctrs 0.2.99.9011 2020-05-04 [1] Github (r-lib/vctrs@0ca806c)
#> withr 2.2.0 2020-04-20 [1] CRAN (R 3.6.3)
#> xfun 0.13 2020-04-13 [1] CRAN (R 3.6.3)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.2)
#>
#> [1] C:/Users/daniel/Documents/.R/win-library
#> [2] C:/Program Files/R/R-3.6.3/libraryMetadata
Metadata
Assignees
Labels
No labels
Activity
iangow commentedon Jun 6, 2020
The PostgreSQL manual is quite clear here. "If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's timezone parameter, and is converted to UTC using the offset for the timezone zone." And this does seem to be what's happening on the PostgreSQL side. In other words, the argument is not ignored, but
RPostgresdoes not interpret the data correctly when reading from PostgreSQL (this is very concerning, as it affects anytimestamp with time zonevalues).Created on 2020-06-06 by the reprex package (v0.3.0)
The issue appears to be in reading the data into R from PostgreSQL.
It seems that
RPostgreSQLhandles this properly:Created on 2020-06-06 by the reprex package (v0.3.0)
(And just to confirm that it's only the driver that is causing this:
Created on 2020-06-06 by the reprex package (v0.3.0))
iangow commentedon Jun 6, 2020
Further digging suggests that
RPostgressomehow assumes that the server is set totimezoneequal toUTC. I think the only way to get correct behaviour is to usetimezone = "UTC"when connecting.You can see below that having
timezone = "UTC"gets the right values every time.Created on 2020-06-06 by the reprex package (v0.3.0)
dpprdan commentedon Aug 19, 2020
I initially thought that a date string is handled differently than a datetime string when cast to
timestamp with timezoneIt seems that I was mistaken, though, and @iangow is correct here:
When casting to text, we get the same output as with psql, like Ian showed.
When explicitly casting to a time zone in Postgres, we get the correct time, but the time zone is
incorrectunexpected, because it is theSys.timezone(). This is formally correct, because Postgres’timezone()strips the time zone designation when applied to atimestamp with time zone.Long story short, this is the same problem as #222, so closing this now.
github-actions commentedon Aug 20, 2021
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.