sqldf:将时间戳从本地时间更改为GMT / UTC

时间:2012-03-01 19:00:20

标签: r sqldf

在我的机器上,sqldf将POSIXct从本地时间(我在CST,GMT -0600)更改为GMT / UTC。这是预期的行为吗?我可以阻止R或sqldf这样做吗?这是我的代码:

> library('sqldf')
> 
> before <- data.frame(ct_sys = Sys.time())
> before
               ct_sys
1 2012-03-01 12:57:58
> after <- sqldf('select * from before')
> after
               ct_sys
1 2012-03-01 18:57:58
> 

1 个答案:

答案 0 :(得分:3)

这似乎是sqldf中的时区错误。它现在应该在sqldf版本0.4-6.2中修复(尚未在CRAN上,但你可以尝试这样):

library(sqldf)
source("http://sqldf.googlecode.com/svn/trunk/R/sqldf.R")
before <- data.frame(ct_sys = Sys.time()); before
after <- sqldf('select * from before'); after

即使没有这样做sqldf("select * from before", method = "raw")也会有效,虽然它会返回一个数字列(你可以转换为POSIXct),也可能会影响其他列。以下是使用method = "raw"的示例。请注意,在这两种情况下,我们都会获得1330661786.181

> library(sqldf)
>
> before <- data.frame(ct_sys = Sys.time()); dput(before)
structure(list(ct_sys = structure(1330661786.181, class = c("POSIXct", 
"POSIXt"))), .Names = "ct_sys", row.names = c(NA, -1L), class = "data.frame")
>
> after <- sqldf('select * from before', method = "raw"); dput(after)
structure(list(ct_sys = 1330661786.181), .Names = "ct_sys", row.names = 1L, class = "data.frame")

编辑:添加了使用method = "raw"

的示例