Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timezone for as.POSIXct not working

Tags:

date

r

posixct

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"
like image 628
phil_t Avatar asked May 16 '18 14:05

phil_t


People also ask

What is the difference between POSIXct and POSIXlt and as date?

There are two POSIX date/time classes, which differ in the way that the values are stored internally. The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.

What is the purpose of the POSIXct time format?

POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.

What does POSIXct mean?

The basic POSIX measure of time, calendar time, is the number of seconds since the beginning of 1970, in the UTC timezone (GMT as described by the French).


1 Answers

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"
like image 123
Roland Avatar answered Nov 14 '22 23:11

Roland