Timezone for as.POSIXct not working
Asked Answered
C

1

5

I have a dataframe with dates and I am trying to convert it to a POSIXct object but I am unable to specify timezone. Any idea why this is happening?

> str(dates)
'data.frame':   3171 obs. of  3 variables:
$ Date     : Date, format: "2013-05-14" "2013-08-15" "2014-05-30" "2014-09-29" ...
$ BB_Ticker: Factor w/ 1252 levels "A US Equity",..: 1 2 2 2 2 2 2 2 2 2 ...
$ 1Y       : POSIXct, format: "2013-05-13 20:00:00" "2013-08-14 20:00:00" "2014-05-29 20:00:00" "2014-09-28 20:00:00" ..

I tried specifying "America/New_York" as well as "EST5EDT" but it had no effect -

> head(as.POSIXct(dates$Date, tz = "GMT"), 3)
[1] "2013-05-13 20:00:00 EDT" "2013-08-14 20:00:00 EDT" "2014-05-29 20:00:00 EDT"

> head(as.POSIXct(dates$Date, tz = "America/New_York"), 3)
[1] "2013-05-13 20:00:00 EDT" "2013-08-14 20:00:00 EDT" "2014-05-29 20:00:00 EDT"

> head(as.POSIXct(dates$Date, tz = "EST5EDT"), 3)
[1] "2013-05-13 20:00:00 EDT" "2013-08-14 20:00:00 EDT" "2014-05-29 20:00:00 EDT"
Conjure answered 16/5, 2018 at 14:15 Comment(0)
S
6

If you look at the source code of as.POSIXct.Date you see this:

function (x, ...) 
.POSIXct(unclass(x) * 86400)
<bytecode: 0x00000000120de6e0>
<environment: namespace:base>

Note how no timezone is passed on to .POSIXct.

You can use the character method instead:

as.POSIXct(as.character(as.Date("2013-05-14")), tz = "GMT")
[1] "2013-05-14 GMT"
Sinusoidal answered 16/5, 2018 at 14:20 Comment(1)
And @Sinusoidal has been proposing (to the R-devel mailing list, rightly so) to improve the situation, and I think you're +/- right.Demetriusdemeyer

© 2022 - 2024 — McMap. All rights reserved.