Reading timestamp data in R from multiple time zones

前端 未结 3 1807

I have a column of time stamps in character format that looks like this:

2015-09-24 06:00:00 UTC

2015-09-24 05:00:00 UTC

dateTimeZone <- c         


        
3条回答
  •  心在旅途
    2020-12-19 06:08

    Another way using lubridate...

    library(stringr)
    library(lubridate)
    
    normalize.timezone <- function(dates, target_tz = local.timezone) {
        tzones <- str_split(dates, ' ')
        tzones <- lapply(tzones, '[', 3)
        tzones <- unlist(tzones)
        dts <- str_replace_all(dates, ' [\\w\\-\\/\\+]+$', '')
        tmp <- lapply(1:length(dates), function(i) {
            with_tz(as.POSIXct(dts[ i ], tz = tzones[ i ]), target_tz)
        })
        final <- unlist(tmp)
        attributes(final) <- attributes(tmp[[ 1 ]])
        final
    }
    
    dates <- c('2019-01-06 23:00:00 MST', 
               '2019-01-22 14:00:00 America/Los_Angeles', 
               '2019-01-05 UTC-4', 
               '2019-01-15 15:00:00 Europe/Moscow')
    (normalize.timezone(dates, 'EST'))
    

提交回复
热议问题