2

这怎么不是bug?

as.POSIXct(as.Date("2013/01/01"))

结果:

[1] "2012-12-31 19:00:00 EST"
4

1 回答 1

4

它调用as.POSIXct.Date方法,即

function (x, ...) 
.POSIXct(unclass(x) * 86400)

.POSIXct请注意,尽管有这样的参数,但不可能将时区传递给:

function (xx, tz = NULL) 
structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz)

所以会发生这种情况:

structure(unclass(as.Date("2013/01/01")) * 86400, 
          class = c("POSIXct", "POSIXt"), tzone = "EST")
#[1] "2012-12-31 19:00:00 EST"

如果要转换Dates 的解决方法:

structure(unclass(as.Date("2013/01/01")) * 86400, 
          class = c("POSIXct", "POSIXt"), tzone = "GMT")
#"2013-01-01 GMT"

或者修改as.POSIXct.Date

function (x, tz=NULL,...)  .POSIXct(unclass(x) * 86400, tz = tz)
于 2013-07-09T13:15:22.403 回答