Skip to content

Instantly share code, notes, and snippets.

@jnolis
Created March 17, 2021 16:12
Show Gist options
  • Save jnolis/e3ba8f786527a4ba17c9a9b7997c55e6 to your computer and use it in GitHub Desktop.
Save jnolis/e3ba8f786527a4ba17c9a9b7997c55e6 to your computer and use it in GitHub Desktop.
Get the sunrise and sunset times for one location on one day
get_sunrise_sunset <- function(lat,long, date, tz){
# tz is required to know when daylight savings happens
response <- httr::GET(glue::glue("https://api.sunrise-sunset.org/json?lat={lat}&lng={long}&date={date}&formatted=0"))
results <- httr::content(response)$results
results[["day_length"]] <- NULL
results <- lapply(results, function(x) as.POSIXct(x,format="%Y-%m-%dT%T",tz="UTC"))
results <- lapply(results, function(x) lubridate::with_tz(x, tzone = tz))
results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment