2

How is this not a bug?

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

Result:

[1] "2012-12-31 19:00:00 EST"
zkurtz
  • 3,230
  • 7
  • 28
  • 64
  • 1
    Why not just `as.POSIXct("2013/01/01")`? Also, that error doesn't reproduce for me. – Thomas Jul 09 '13 at 13:03
  • Great point, I think I can drop as.Date() -- I was just borrowing someone else's code. I wonder if the bug could be a function of my OS - Ubuntu 12.10. – zkurtz Jul 09 '13 at 13:07

1 Answers1

4

It calls the as.POSIXct.Date method, which is

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

Note that there is no possibility to pass a time zone to .POSIXct although it has such a parameter:

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

So this happens:

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

Workaround if you want to convert Dates:

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

Or modify as.POSIXct.Date to

function (x, tz=NULL,...)  .POSIXct(unclass(x) * 86400, tz = tz)
Roland
  • 127,288
  • 10
  • 191
  • 288