convert to local time zone using latitude and longitude?

后端 未结 2 784
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 04:25

I have a data set with the following information: latitude, longitude, EST time. For example, for one observation

lat = 13
long = -2
time1 = as.POSIXlt(\"201         


        
2条回答
  •  春和景丽
    2020-12-18 04:43

    lat = 13
    long = -2
    time1 <- as.POSIXct("2014-02-12 17:00:00", tz = "EST")
    # https://developers.google.com/maps/documentation/timezone/
    apiurl <- sprintf("https://maps.googleapis.com/maps/api/timezone/%s?location=%s,%s×tamp=%d&sensor=%s", 
                      "xml", 
                      lat, 
                      long, 
                      as.numeric(time1), 
                      "false")
    library(XML)
    tz <- xmlParse(readLines(apiurl))[["string(//time_zone_id)"]]
    as.POSIXct(format(time1, tz=tz))
    # [1] "2014-02-12 22:00:00 CET"
    

    or, as suggested by @SymbolixAU, use their googleway package:

    res <- googleway::google_timezone(c(lat, long), time1, key = NULL)
    as.POSIXct(format(time1, tz=res$timeZoneId))
    # [1] "2014-02-12 22:00:00 CET"
    

提交回复
热议问题