lundi 1 juin 2020

R: if_else and timezone forcing

I have a weird problem (maybe I'm missing something?), when trying to force timezone within if_else (as ifelse does not seem to handle POSIXct well). It seems to force only when TRUE, but convert when FALSE. Why? How to fix it?

library(lubridate)
library(dplyr)
some_date = ymd_hm("2020-06-01 17:45", tz = "America/New_York")

if_else(TRUE, force_tz(some_date, tz = "GMT"), force_tz(some_date, tz = "Singapore"))
[1] "2020-06-01 17:45:00 GMT"

if_else(FALSE, force_tz(some_date, tz = "GMT"), force_tz(some_date, tz = "Singapore"))
[1] "2020-06-01 09:45:00 GMT"

I would expect the same outcome as running force_tz alone:

# if TRUE
force_tz(some_date, tz = "GMT")
[1] "2020-06-01 17:45:00 GMT"

# if FALSE
force_tz(some_date, tz = "Singapore")
[1] "2020-06-01 17:45:00 +08"

Thanks!

Aucun commentaire:

Enregistrer un commentaire