I'd like to convert multiple time values with varying time zones, currently represented as milliseconds since 01-01-1970, to a POSIXct format.
I have the following dataset:
times <- c(1427450400291, 1428562800616, 1418651628795, 1418651938990, 1418652348281, 1418652450161)
tzones <- c("America/Los_Angeles", "Africa/Casablanca", "Africa/Casablanca", "Africa/Casablanca", "Africa/Casablanca", "Israel Standard Time")
The problem is that the as.POSIXct
method only accepts one tz value, and not a vector. Therefore, I can't call it directly. I tried using lapply and call it element by element, but it takes a long time (for longer vectors):
get.dates.with.timezones <- function(epoch.vec,tz.vec) {
res <- lapply(seq(epoch.vec),function(x){
as.POSIXct(epoch.vec[x]/1000,origin = "1970-01-01", tz = tz.vec[x])
})
return(do.call(c,res))
}
So for only 1200 values, it takes almost a second.
timesX200 <- rep(times,200)
tzonesX200 <- rep(tzones,200)
system.time( get.dates.with.timezones(timesX200,tzonesX200) )
user system elapsed
0.86800000000005184 0.01999999999999602 0.88899999999921420
I'm a newbie with R, so I wonder if there are ways to improve the performance for this task. is there a vectorized option for this problem? Additionally, it looks like the as.POXIXct()
method itself has some performance issues, as indicated here.
---------- EDIT --------
Apparentely it is impossible to hold a vector of POSIXct with varying time zones. From the POSIXct documentation:
Using c on "POSIXlt" objects converts them to the current time zone, and on "POSIXct" objects drops any "tzone" attributes (even if they are all marked with the same time zone). Source
That's too bad. I wonder if there are any alternatives for dealing with date + time + varying time zone. Would be happy to hear if there is.