Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert to local time zone using latitude and longitude?

Tags:

timezone

r

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("2014-02-12 17:00:00", tz = "EST")

I want to create a new variable timeL that is the local time. Any suggestions of how to do this with R?

Thanks!

like image 817
Ignacio Avatar asked May 01 '14 18:05

Ignacio


People also ask

How do you calculate time zone using latitude and longitude?

Here's how longitude converts into your personal time zone: 15 degrees of longitude = 1 hour difference; 1 degree longitude = 4 minutes difference. 15 minutes of longitude = 1 minute difference; 1 minute of longitude = 4 seconds difference.

What is the formula for time zones?

To find the time zone in hours of a particular location, you can take the longitude -- in degrees -- and divide it by 15. So, for example, 75° E would be 75/15 which equals 5. That translates to the time zone being 5 hours ahead of UTC or GMT time, which can also be labeled as UTC+5.

How do you find UTC from longitude?

Take the number of minutes difference between your local noon and UTC noon and divide it by 4. That'll tell you roughly the longitude of your location. To continue the previous example, you have a 300 minutes' difference between local time and UTC time. Since 300 divided by 4 is 75, your longitude is 75º west.

Do time zones match lines of longitude?

In fact, because time zones do not follow neatly along lines of longitude, there are many places where three or more time zones meet.


2 Answers

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&timestamp=%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"
like image 157
lukeA Avatar answered Nov 15 '22 06:11

lukeA


An alternate approach is to intersect the target point coordinates with a shapefile of global time zones and subsequently convert the EST timestamps to the local time at each spatial point. For example, boundaries of the world's time zones are available from Timezone Boundary Builder (direct download).

library(sf)

## example data
dat = data.frame(lon = -2
                 , lat = 13
                 , time1 = as.POSIXlt("2014-02-12 17:00:00", tz = "EST"))

## convert to 'sf' object
sdf = st_as_sf(dat, coords = c("lon", "lat"), crs = 4326)

## import timezones (after extraction) and intersect with spatial points
tzs = st_read("timezones.geojson/combined.json", quiet = TRUE)
sdf = st_join(sdf, tzs)

## convert timestamps to local time
sdf$timeL = as.POSIXlt(sdf$time1, tz = as.character(sdf$tzid))
sdf$timeL
# [1] "2014-02-12 22:00:00 GMT"

Note that the corresponding time zone Africa/Ouagadougou is GMT.

like image 20
fdetsch Avatar answered Nov 15 '22 07:11

fdetsch