Skip to content

timezone connection argument ignored when casting a date string #245

Closed

Description

@dpprdan

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/library

Activity

  1. iangow commented on Jun 6, 2020

    @iangow

    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 RPostgres does not interpret the data correctly when reading from PostgreSQL (this is very concerning, as it affects any timestamp with time zone values).

    library(DBI)
    Sys.timezone()
    #> [1] "America/New_York"
    query <- "SELECT '2020-05-04'::TIMESTAMPTZ::TEXT"
    
    con <- dbConnect(RPostgres::Postgres(), , timezone = "America/Chicago")
    dbGetQuery(con, "SHOW TIMEZONE;")
    #>          TimeZone
    #> 1 America/Chicago
    res <- dbGetQuery(con, query)
    res
    #>                     text
    #> 1 2020-05-04 00:00:00-05
    
    res[[1,1]]
    #> [1] "2020-05-04 00:00:00-05"
    
    dbDisconnect(con)

    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 RPostgreSQL handles this properly:

    library(DBI)
    Sys.timezone()
    #> [1] "America/New_York"
    query <- "SELECT '2020-05-04'::TIMESTAMPTZ"
    
    con <- dbConnect(RPostgreSQL::PostgreSQL())
    dbExecute(con, "SET timezone TO 'America/Chicago'")
    #> [1] 0
    dbGetQuery(con, "SHOW timezone")
    #>          TimeZone
    #> 1 America/Chicago
    res <- dbGetQuery(con, query)
    res
    #>           timestamptz
    #> 1 2020-05-04 01:00:00
    
    res[[1,1]]
    #> [1] "2020-05-04 01:00:00 EDT"
    
    dbDisconnect(con)
    #> [1] TRUE

    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:

    library(DBI)
    Sys.timezone()
    #> [1] "America/New_York"
    query <- "SELECT '2020-05-04'::TIMESTAMPTZ"
    
    con <- dbConnect(RPostgres::Postgres())
    dbExecute(con, "SET timezone TO 'America/Chicago'")
    #> [1] 0
    dbGetQuery(con, "SHOW timezone")
    #>          TimeZone
    #> 1 America/Chicago
    res <- dbGetQuery(con, query)
    res
    #>           timestamptz
    #> 1 2020-05-03 20:00:00
    
    res[[1,1]]
    #> [1] "2020-05-03 20:00:00 EDT"
    
    dbDisconnect(con)

    Created on 2020-06-06 by the reprex package (v0.3.0))

  2. iangow commented on Jun 6, 2020

    @iangow

    Further digging suggests that RPostgres somehow assumes that the server is set to timezone equal to UTC. I think the only way to get correct behaviour is to use timezone = "UTC" when connecting.

    You can see below that having timezone = "UTC" gets the right values every time.

    library(DBI)
    Sys.timezone()
    #> [1] "America/New_York"
    
    con <- dbConnect(RPostgres::Postgres())
    dbExecute(con, "SET timezone TO 'UTC'")
    #> [1] 0
    dbGetQuery(con, "SHOW timezone")
    #>   TimeZone
    #> 1      UTC
    dbExecute(con, "DROP TABLE IF EXISTS junk")
    #> [1] 0
    query <- "CREATE TEMPORARY TABLE junk AS 
                (SELECT '2020-05-04 America/Chicago'::TIMESTAMPTZ AS ts)"
    res <- dbExecute(con, query)
    dbExecute(con, "SET timezone TO 'America/Chicago'")
    #> [1] 0
    res <- dbGetQuery(con, "SELECT * FROM junk")
    res
    #>                    ts
    #> 1 2020-05-03 20:00:00
    
    dbExecute(con, "SET timezone TO 'UTC'")
    #> [1] 0
    res <- dbGetQuery(con, "SELECT * FROM junk")
    res
    #>                    ts
    #> 1 2020-05-04 01:00:00
    
    dbDisconnect(con)
    con <- dbConnect(RPostgres::Postgres())
    dbExecute(con, "SET timezone TO 'America/Chicago'")
    #> [1] 0
    dbGetQuery(con, "SHOW timezone")
    #>          TimeZone
    #> 1 America/Chicago
    dbExecute(con, "DROP TABLE IF EXISTS junk")
    #> [1] 0
    query <- "CREATE TEMPORARY TABLE junk AS 
                (SELECT '2020-05-04 America/Chicago'::TIMESTAMPTZ AS ts)"
    res <- dbExecute(con, query)
    dbExecute(con, "SET timezone TO 'America/Chicago'")
    #> [1] 0
    res <- dbGetQuery(con, "SELECT * FROM junk")
    res
    #>                    ts
    #> 1 2020-05-03 20:00:00
    
    dbExecute(con, "SET timezone TO 'UTC'")
    #> [1] 0
    res <- dbGetQuery(con, "SELECT * FROM junk")
    res
    #>                    ts
    #> 1 2020-05-04 01:00:00
    
    dbDisconnect(con)
    
    con <- dbConnect(RPostgres::Postgres())
    dbExecute(con, "SET timezone TO 'UTC'")
    #> [1] 0
    dbGetQuery(con, "SHOW timezone")
    #>   TimeZone
    #> 1      UTC
    dbExecute(con, "DROP TABLE IF EXISTS junk")
    #> [1] 0
    query <- "CREATE TEMPORARY TABLE junk AS 
                (SELECT '2020-05-04 America/Chicago'::TIMESTAMPTZ AS ts)"
    res <- dbExecute(con, query)
    dbExecute(con, "SET timezone TO 'America/Chicago'")
    #> [1] 0
    res <- dbGetQuery(con, "SELECT * FROM junk")
    res
    #>                    ts
    #> 1 2020-05-03 20:00:00
    
    dbExecute(con, "SET timezone TO 'America/Chicago'")
    #> [1] 0
    res <- dbGetQuery(con, "SELECT * FROM junk")
    res
    #>                    ts
    #> 1 2020-05-03 20:00:00

    Created on 2020-06-06 by the reprex package (v0.3.0)

  3. dpprdan commented on Aug 19, 2020

    @dpprdan
    ContributorAuthor

    I initially thought that a date string is handled differently than a datetime string when cast to timestamp with timezone
    It seems that I was mistaken, though, and @iangow is correct here:

    The issue appears to be in reading the data into R from PostgreSQL.

    library(DBI)
    Sys.timezone()
    #> [1] "Europe/Berlin"
    
    con <- dbConnect(RPostgres::Postgres(), dbname="postgres", timezone = "America/Chicago")
    
    dbGetQuery(con, "SHOW TIMEZONE;")
    #>          TimeZone
    #> 1 America/Chicago
    
    # cast date string to timestamp
    dbGetQuery(con, "SELECT '2020-05-04'::TIMESTAMPTZ;")[1,1]
    #> [1] "2020-05-04 02:00:00 CEST"
    
    # cast datetime string to timestamp
    dbGetQuery(con, "SELECT '2020-05-04 00:00:00'::TIMESTAMPTZ;")[1,1]
    #> [1] "2020-05-04 02:00:00 CEST"
    
    # cast datetime string with tz to timestamp
    dbGetQuery(con, "SELECT '2020-05-04 00:00:00-05'::TIMESTAMPTZ;")[1,1]
    #> [1] "2020-05-04 02:00:00 CEST"
    

    When casting to text, we get the same output as with psql, like Ian showed.

    # cast date string to timestamp then text
    dbGetQuery(con, "SELECT '2020-05-04'::TIMESTAMPTZ::text;")[1,1]
    #> [1] "2020-05-04 00:00:00-05"
    
    # cast datetime string to timestamp then text
    dbGetQuery(con, "SELECT '2020-05-04 00:00:00'::TIMESTAMPTZ::text;")[1,1]
    #> [1] "2020-05-04 00:00:00-05"
    
    # cast datetime string with tz to timestamp then text
    dbGetQuery(con, "SELECT '2020-05-04 00:00:00-05'::TIMESTAMPTZ::text;")[1,1]
    #> [1] "2020-05-04 00:00:00-05"
    

    When explicitly casting to a time zone in Postgres, we get the correct time, but the time zone is incorrect unexpected, because it is the Sys.timezone(). This is formally correct, because Postgres’ timezone() strips the time zone designation when applied to a timestamp with time zone.

    # cast date string to timestamp
    dbGetQuery(con, "SELECT timezone('America/Chicago', '2020-05-04'::TIMESTAMPTZ);")[1,1]
    #> [1] "2020-05-04 CEST"
    
    # cast datetime string to timestamp
    dbGetQuery(con, "SELECT timezone('America/Chicago', '2020-05-04 00:00:00'::TIMESTAMPTZ);")[1,1]
    #> [1] "2020-05-04 CEST"
    
    # cast datetime string with tz to timestamp
    dbGetQuery(con, "SELECT timezone('America/Chicago', '2020-05-04 00:00:00-05'::TIMESTAMPTZ);")[1,1]
    #> [1] "2020-05-04 CEST"
    
    # clean up
    dbDisconnect(con)
    

    Long story short, this is the same problem as #222, so closing this now.

  4. github-actions commented on Aug 20, 2021

    @github-actions
    Contributor

    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.

  5. locked and limited conversation to collaborators on Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions