Reading timestamp data in R from multiple time zones

前端 未结 3 1803

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:21

    You can get there by checking each row and processing accordingly, and then putting everything back into a consistent UTC time. (#edited to now include matching the timezone abbreviations to the full timezone specification)

    dates <- c(
      "2015-09-24 06:00:00 UTC",
      "2015-09-24 05:00:00 PDT"
    )
    
    #extract timezone from dates
    datestz <- vapply(strsplit(dates," "), tail, 1, FUN.VALUE="")
    
    ## Make a master list of abbreviation to 
    ## full timezone names. Used an arbitrary summer
    ## and winter date to try to catch daylight savings timezones.
    
    tzabbrev <- vapply(
      OlsonNames(),
      function(x) c(
        format(as.POSIXct("2000-01-01",tz=x),"%Z"),
        format(as.POSIXct("2000-07-01",tz=x),"%Z")
      ),
      FUN.VALUE=character(2)
    )
    tmp <- data.frame(Olson=OlsonNames(), t(tzabbrev), stringsAsFactors=FALSE)
    final <- unique(data.frame(tmp[1], abbrev=unlist(tmp[-1])))
    
    ## Do the matching:
    out <- Map(as.POSIXct, dates, tz=final$Olson[match(datestz,final$abbrev)])
    as.POSIXct(unlist(out), origin="1970-01-01", tz="UTC")
    #  2015-09-24 06:00:00 UTC   2015-09-24 05:00:00 PDT 
    #"2015-09-24 06:00:00 GMT" "2015-09-24 12:00:00 GMT" 
    

提交回复
热议问题