I have a POSIXct
object and would like to change it's tz
attribute WITHOUT R to interpret it (interpret it would mean to change how the datetime is displayed on the screen).
Some background: I am using the fasttime
package from S.Urbanek, which take strings and cast it to POSIXct
very quickly. Problem is that the string should represent a datetime in "GMT" and it's not the case of my data.
I end up with a POSIXct
object with tz=GMT
, in reality it is tz=GMT+1
, if I change the timezone with
attr(datetime, "tzone") <- "Europe/Paris";
datetime <- .POSIXct(datetime,tz="Europe/Paris");
then it will be "displayed" as GMT+2
(the underlying value never change).
EDIT: Here is an example
datetime=as.POSIXct("2011-01-01 12:32:23.234",tz="GMT")
attributes(datetime)
#$tzone
#[1] "GMT"
datetime
#[1] "2011-01-01 12:32:23.233 GMT"
How can I change this attribute without R to interpret it aka how can I change tzone and still have datetime displayed as "2011-01-01 12:32:23.233"
?
EDIT/SOLUTION, @GSee's solution is reasonably fast, lubridate::force_tz very slow
datetime=rep(as.POSIXct("2011-01-01 12:32:23.234",tz="GMT"),1e5)
f <- function(x,tz) return(as.POSIXct(as.numeric(x), origin="1970-01-01", tz=tz))
> system.time(datetime2 <- f(datetime,"Europe/Paris"))
user system elapsed
0.01 0.00 0.02
> system.time(datetime3 <- force_tz(datetime,"Europe/Paris"))
user system elapsed
5.94 0.02 5.98
identical(datetime2,datetime3)
[1] TRUE
lubridate::force_tz
– Hephzipaf
doesn't correspond to @GSee's latest answer, because the origins won't necessarily be the same. When I dof(datetime[1], tz="Europe/Paris")
with yourf
I get2011-01-01 13:32:23 CET
. So inf
I think you should haveorigin = as.POSIXct("1970-01-01", tz=tz)
. Also,force_tz
appears to be much faster now. – Omen