Skip to content

Different timestamp display in psql and RStudio #205

Closed

Description

@EarlGlynn

psql query and results:

mimic=# SELECT subject_id, dob, cast (dob as date) AS dobDate
mimic-# FROM   patients
mimic-# ORDER BY DOB
mimic-# LIMIT  5;
 subject_id |         dob         |  dobdate
------------+---------------------+------------
      31585 | 1800-07-02 00:00:00 | 1800-07-02
      62884 | 1800-07-16 00:00:00 | 1800-07-16
      31288 | 1800-07-16 00:00:00 | 1800-07-16
       3240 | 1800-07-18 00:00:00 | 1800-07-18
      29851 | 1800-08-19 00:00:00 | 1800-08-19

SQL chunk in RStudio with RPostgres_1.1.1:

SELECT subject_id, dob, cast (dob as date) AS dobDate
FROM   patients
ORDER BY DOB
LIMIT  5
subject_id dob                     dobdate
<int>      <S3:POSIXct>            <date>
31585      1969-12-31 17:59:59     1800-07-02
62884      1969-12-31 17:59:59     1800-07-16
31288      1969-12-31 17:59:59     1800-07-16
3240       1969-12-31 17:59:59     1800-07-18
29851      1969-12-31 17:59:59     1800-08-19

Additional details at RStudio Community.

Activity

  1. EarlGlynn commented on Dec 23, 2018

    @EarlGlynn
    Author

    Using the MIMIC data, the odbc and RPostgreSQL packages work fine to display dates back to 1800, but RPostgres does not.

    RPostgres only shows dates back to 1969-12-31, but maybe that is 1970-01-01 in my timezone (but off by a second).

    See additional details for these drivers with the MIMIC data in separate notebooks here.

    FWIW, the MIMIC-III database is publicly available for medical researchers (after completion of required training).

  2. krlmlr commented on Dec 30, 2018

    @krlmlr
    Member

    Thanks. From the RStudio Community post I'm guessing you're on Windows. On Linux I see:

    library(DBI)
    
    con <- dbConnect(RPostgres::Postgres())
    
    tbl <- dbGetQuery(con, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch")
    tbl
    #>          before_epoch
    #> 1 1960-01-01 12:00:00
    
    dput(tbl$before_epoch)
    #> structure(-315579600, class = c("POSIXct", "POSIXt"))

    Created on 2018-12-30 by the reprex package (v0.2.1.9000)

    But on Windows this is consistent with your observation:

    library(DBI)
    
    con <- dbConnect(RPostgres::Postgres(), ...)
    
    tbl <- dbGetQuery(con, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch")
    tbl
    #>          before_epoch
    #> 1 1970-01-01 00:59:59
    
    dput(tbl$before_epoch)
    #> structure(-1, class = c("POSIXct", "POSIXt"))

    Created on 2018-12-30 by the reprex package (v0.2.1)

  3. krlmlr commented on Dec 17, 2019

    @krlmlr
    Member

    Is this still an issue with RPostgres 1.2.0 (currently in development, about to hit CRAN)?

  4. EarlGlynn commented on Dec 18, 2019

    @EarlGlynn
    Author

    I still see the same problem -- sort of. Using your example from above:

    On Windows 10

    RPostgres_1.1.3 from CRAN

    tbl <- dbGetQuery(MimicDB, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch")
    tbl
    ##          before_epoch
    ## 1 1969-12-31 17:59:59
    
    tbl <- dbGetQuery(MimicDB, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AT TIME ZONE 'utc' AS before_epoch")
    tbl
    ##          before_epoch
    ## 1 1960-01-01 06:00:00
    

    Why the ~10 year difference here?

    Same results using RPostgres_1.2.0 from GitHub

  5. trafficonese commented on Dec 18, 2019

    @trafficonese

    I am getting slightly different results. I tested the 2 queries with RPostgres, RPostgreSQL & pgAdmin4 on Win 10 and on the DB in Linux with psql directly.

    RPostgres with default timezone
    library(RPostgres)
    packageVersion("RPostgres")
    ‘1.2.0’
    pool <- dbConnect(RPostgres::Postgres(),
                      dbname = config$db[["dbname"]],
                      host = config$db[["dbhost"]],
                      port = config$db[["dbport"]],
                      user = config$db[["dbuser"]],
                      password = config$db[["dbpwd"]]
                      # ,timezone = NULL
                      )
    dbGetQuery(pool, "SHOW timezone;")
    UTC
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch")
    1970-01-01 00:59:59
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AT TIME ZONE 'utc' AS before_epoch")
    1960-01-01 13:00:00
    dbDisconnect(pool)
    
    RPostgres with timezone = NULL
    pool <- dbConnect(RPostgres::Postgres(),
                      dbname = config$db[["dbname"]],
                      host = config$db[["dbhost"]],
                      port = config$db[["dbport"]],
                      user = config$db[["dbuser"]],
                      password = config$db[["dbpwd"]],
                      timezone = NULL)
    dbGetQuery(pool, "SHOW timezone;")
    1 localtime
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch")
    1 1970-01-01 00:59:59
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AT TIME ZONE 'utc' AS before_epoch")
    1 1960-01-01 14:00:00
    dbDisconnect(pool)
    
    RPostgreSQL
    library(RPostgreSQL)
    pool <- dbConnect(RPostgreSQL::PostgreSQL(),
                      dbname = config$db[["dbname"]],
                      host = config$db[["dbhost"]],
                      port = config$db[["dbport"]],
                      user = config$db[["dbuser"]],
                      password = config$db[["dbpwd"]])
    dbGetQuery(pool, "SHOW timezone;")
    1 localtime
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch")
    1 1960-01-01 12:00:00
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AT TIME ZONE 'utc' AS before_epoch")
    1 1960-01-01 13:00:00
    dbDisconnect(pool)
    
    pgAdmin 4 (windows 10)
    SHOW timezone;
    "localtime"
    
    SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch;
    "1960-01-01 12:00:00"
    
    SELECT CAST('1960-01-01 12:00:00' AS timestamp) AT TIME ZONE 'utc' AS before_epoch;
    "1960-01-01 13:00:00+01"
    
    Linux + psql
    date +"%Z %z"
      CET +0100
    
    postgres=# show Timezone;
      localtime
    
    postgres=# SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch;
      1960-01-01 12:00:00
    
    postgres=# SELECT CAST('1960-01-01 12:00:00' AS timestamp) AT TIME ZONE 'utc' AS before_epoch;
      1960-01-01 13:00:00+01
    
    Session
    sessioninfo::session_info()
    setting  value                       
    version  R version 3.6.1 (2019-07-05)
    os       Windows 10 x64              
    system   x86_64, mingw32             
    ui       RStudio                     
    language (EN)                        
    collate  German_Germany.1252         
    ctype    German_Germany.1252         
    tz       Europe/Berlin               
    date     2019-12-18   
    
  6. EarlGlynn commented on Jul 17, 2020

    @EarlGlynn
    Author

    Now with RPostgres_1.2.0 I get the same results as I posted above on Dec. 22, 2018. Is there any hope this can be resolved? Is a better solution perhaps to see if odbc works with Postgres?

  7. added this to the 1.2.2 milestone on Dec 27, 2020
  8. krlmlr commented on Jan 3, 2021

    @krlmlr
    Member

    With #276:

    library(RPostgres)
    packageVersion("RPostgres")
    #> [1] '1.2.1.9001'
    
    pool <- dbConnect(RPostgres::Postgres(), timezone = "America/New_York")
    
    dbGetQuery(pool, "SHOW timezone")
    #>           TimeZone
    #> 1 America/New_York
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AS before_epoch")[[1]]
    #> [1] "1960-01-01 12:00:00 EST"
    dbGetQuery(pool, "SELECT CAST('1960-01-01 12:00:00' AS timestamp) AT TIME ZONE 'utc' AS before_epoch")[[1]]
    #> [1] "1960-01-01 07:00:00 EST"
    dbDisconnect(pool)

    Created on 2021-01-03 by the reprex package (v0.3.0)

  9. added a commit that references this issue on Jan 3, 2021
  10. krlmlr commented on Jan 3, 2021

    @krlmlr
    Member

    Should be fixed now. Could you please test the development version in the next few days, and let me know if any problems still arise?

  11. github-actions commented on Jan 4, 2022

    @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.

  12. locked and limited conversation to collaborators on Jan 4, 2022
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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions