I have a dataframe with UTC with i am trying to convert to local times using the with_tz()
function, however its giving some weird results, for example London is being put forward by 8 hours rather than by just 1 (or 2 for summer). I'll include any code and data below if anyone knows why this is?
library(lubridate)
df
localtime utc timezone
NA 2022-06-02 19:00:00 CET
NA 2022-06-02 14:00:00 Europe/Bucharest
NA 2022-06-02 14:30:00 CET
NA 2022-06-02 16:00:00 CET
NA 2022-06-02 18:00:00 CET
NA 2022-06-02 16:30:00 Europe/London
NA 2022-06-02 21:00:00 US/Central
To get the local time I then use;
df$utc = as.POSIXct(df$utc, format="%Y-%m-%d %H:%M:%OS", tz = "UTC")
df = df %>% rowwise() %>% mutate(localtime = with_tz(utc, timezone))
This returns wrong results which I'm not sure why as can be seen here;
localtime utc timezone
2022-06-03 03:00:00 2022-06-02 19:00:00 CET
2022-06-02 22:00:00 2022-06-02 14:00:00 Europe/Bucharest
2022-06-02 22:30:00 2022-06-02 14:30:00 CET
2022-06-03 00:00:00 2022-06-02 16:00:00 CET
2022-06-03 02:00:00 2022-06-02 18:00:00 CET
2022-06-03 00:30:00 2022-06-02 16:30:00 Europe/London
2022-06-03 05:00:00 2022-06-02 21:00:00 US/Central
US/Central has been put forward when it should be back, London is 8 hours ahead when it should be 1.
dput if anyone needs it
> dput(df)
structure(list(localtime = c(NA, NA, NA, NA, NA, NA, NA), utc = c("2022-06-02 19:00:00",
"2022-06-02 14:00:00", "2022-06-02 14:30:00", "2022-06-02 16:00:00", "2022-06-02 18:00:00",
"2022-06-02 16:30:00", "2022-06-02 21:00:00"), timezone = c("CET",
"Europe/Bucharest", "CET", "CET", "CET", "Europe/London", "US/Central")), row.names = 1:7,
class = "data.frame")