i have strings representing time offsets, have + , - signs, such as:
string1 <- c("+00:30", "-07:00")
how can convert these as.difftime format? know if there no signs, have written as.difftime(string1, format = "%h:%m") , work. however, not know how handle signs. want use resulted difftime converting utc time lst.
i appreciate time , in advance.
it possible using as.difftime , format = "%z" there couple of issues. first, colon isn't valid part of time zone specifier. second, output seems relative current timezone. here's function gets round those:
tz_to_difftime <- function(x, 0 = "+0000") { out <- as.difftime(c(zero, gsub("[: ]", "", x)), format = "%z") out[1] - out[-1] } tz_to_difftime("+05:30") note work differences of 14 hours in current r (less on older versions).
here's alternative using strsplit:
tz_to_difftime2 <- function(x) { as.difftime(sapply(strsplit(x, ":"), function(x) as.numeric(x) %*% c(1, 1/60)), units = "hours") } and if things more pipey (using magtittr)
tz_to_difftime3 <- . %>% strsplit(":") %>% sapply( . %>% as.numeric %>% multiply_by_matrix(c(1, 1/60)) ) %>% as.difftime(unit = "hours") these last 2 have advantage they'll take number of hours.
Comments
Post a Comment