Randomcloud
Randomcloud

Reputation: 61

Timezone conversion: invalid 'tz' value - R

My goal is to convert "system" time zone to local time zone. I have all dependent timezones saved in a column, but it does not work, see example below (error = invalid 'tz' value) :

   db<-data.table(c('Africa/Windhoek','Europe/Andorra','America/Chicago'), c('2017-12-31 17:04:00','2017-9-1 13:30:12','2017-12-15 21:33:13')) 
   names(db) = c('tz','date')
   db$date<- as.POSIXct(db$date, tz="Europe/London", format= c("%Y-%m-%d %H:%M:%OS"))

   db$original_time<-format(db$date, tz=db$tz) 

I read the second answer at invalid 'tz' value, problems with time zone, (The source of your "invalid 'tz' value" error is because, for whatever reason, R doesn't accept tz = df$var. If you set tz = 'America/New_York' or some other character value, then it will work.) but is this manual input really the only solution to this problem, as I have 247 levels/timezones and I believe there is a smoother way?

Thanks!

Upvotes: 1

Views: 317

Answers (1)

Thomas Jc
Thomas Jc

Reputation: 137

The problem is that format expect a string type but you give it a vector.

Try to do formatting iteratively

library(purrr)
map2(df$date, df$tz, ~ format(.x, tz = .y))

Upvotes: 2

Related Questions