How to properly handle timezone when passing POSIXct objects between R and Postgres DBMS? -
i struggling understand happens behind scenes when passing posixct
objects between r , postgres using rpostgresql
. in following example, define 2 timestamp fields: 1 with timezone other 1 without. however, appears treated same when passing posixct
objects via dbwritetable
, dbreadtable
.
library(rpostgresql) drv <- dbdriver("postgresql") con <- dbconnect(drv, host = "127.0.0.1", port = "5432", user= "postgres", dbname = "test_db") q <- " create table test_table ( dttm timestamp without time zone, dttmtz timestamp time zone )" dbsendquery(con, q) # using timezone cet dttm <- as.posixct("2016-01-01 10:20:10", tz="cet") df <- data.frame(dttm = dttm, dttmtz = dttm) dbwritetable(con, "test_table", df, overwrite=false, append=t, row.names=0) # using timezone utc dttm <- as.posixct("2016-01-01 14:20:10", tz="utc") df <- data.frame(dttm = dttm, dttmtz = dttm) dbwritetable(con, "test_table", df, overwrite=false, append=t, row.names=0) df2 <- dbreadtable(con, "test_table")
both fields come out same. appears if timezones discarded.
df2$dttm [1] "2016-01-01 10:20:10 cet" "2016-01-01 14:20:10 cet" df2$dttmtz "2016-01-01 10:20:10 cet" "2016-01-01 14:20:10 cet"
questions:
- what goes on behind scenes?
- how can pass posixct's timezone , forth?
i think you've pointed out bug in rpostgresql: not seem getting time zone r posixct objects.
modifying code:
library(rpostgresql) drv <- dbdriver("postgresql") con <- dbconnect(drv, port = "5432", user= "postgres", dbname = "test") # timestamps in 3 different time zones dt1 <- as.posixct("2016-01-01 10:20:10", tz="us/eastern") dt2 <- as.posixct("2016-01-01 10:20:10", tz="utc") dt3 <- as.posixct("2016-01-01 10:20:10", tz="asia/tokyo") df <- data.frame(dt1=dt1, dt2=dt2, dt3=dt3) q <- " create table test_table ( dt1 timestamp time zone, dt2 timestamp time zone, dt3 timestamp time zone, primary key (dt1) )" dbsendquery(con, q) dbwritetable(con, "test_table", df, overwrite=false, append=t, row.names=0) df2 <- dbreadtable(con, "test_table")
note 3 timestamps equal timezones not handled correctly
df2$dt1
"2016-01-01 10:20:10 est"
df2$dt2
"2016-01-01 10:20:10 est"
df2$dt3
"2016-01-01 10:20:10 est"
and same true in postgres - seen in pgadmin here
this suggests postgres not getting timezone r
note if manually change 1 time zone in test_table (e.g., first record in pgadmin)
and fetch
df2 <- dbreadtable(con, "test_table")
then timezone correctly handled
df2$dt1
"2016-01-01 05:20:10 est"
df2$dt2
"2016-01-01 10:20:10 est"
df2$dt3
"2016-01-01 10:20:10 est"
so suggets rpostgresql not correctly passing time zone information postgres rpostgresql correctly getting time zone information postgres.
Comments
Post a Comment