|
8 | 8 | #' @param lat Latitude in decimal degrees.
|
9 | 9 | #' @param elev Sun elevation in degrees.
|
10 | 10 | #' @param tz output time zone. Ignored if \code{date} has an associated time zone already
|
| 11 | +#' @param force_tz whether to convert input time to timezone \code{tz}. Default \code{FALSE}. |
11 | 12 | #'
|
12 |
| -#' @return The moment of sunrise or sunset in the same time zone as specified |
13 |
| -#' (by \code{date} or \code{tz}) or in UTC if not specified. |
| 13 | +#' @return The moment of sunrise or sunset for the date set by \code{date}and time zone as specified |
| 14 | +#' (by \code{date} and \code{tz}) or in UTC if not specified. |
14 | 15 | #'
|
15 | 16 | #' @details The angular diameter of the sun is about 0.536 degrees,
|
16 | 17 | #' therefore the moment of sunrise/sunset corresponds to half that elevation
|
|
21 | 22 | #' Approximate astronomical formula are used, therefore the moment of
|
22 | 23 | #' sunrise / sunset may be off by a few minutes
|
23 | 24 | #'
|
| 25 | +#' Sunrise and sunset is given for the day specified by \code{date}, with sunset always |
| 26 | +#' later than sunrise given the same \code{date}. |
| 27 | +#' |
| 28 | +#' If \code{force_tz} is \code{TRUE}, the input date is converted to the timezone |
| 29 | +#' set by \code{tz} prior to calculating the sunrise/sunset time, resulting in a shift by +/-1 day |
| 30 | +#' |
24 | 31 | #' @examples
|
25 | 32 | #' # sunrise in the Netherlands
|
26 | 33 | #' sunrise("2016-01-01", 5, 53)
|
|
36 | 43 | #' @rdname sunrise_sunset
|
37 | 44 | #'
|
38 | 45 | #' @export
|
39 |
| -sunrise <- function(date, lon, lat, elev = -0.268, tz = "UTC") { |
| 46 | +sunrise <- function(date, lon, lat, elev = -0.268, tz = "UTC", force_tz = FALSE) { |
40 | 47 | locations <- data.frame(lon = lon, lat = lat)
|
41 | 48 | locations <- SpatialPoints(locations, proj4string = CRS("+proj=longlat +datum=WGS84"))
|
42 |
| - datetime <- as.POSIXct(date, tz = tz) |
| 49 | + datetime <- as.POSIXct(date, tz = tz) # tz ignored if already set |
| 50 | + if(force_tz) datetime <- as_datetime(datetime, tz=tz) |
43 | 51 | suntimes <- crepuscule(locations, datetime, solarDep = -elev, direction = "dawn", POSIXct.out = TRUE)
|
44 | 52 | suntimes$time
|
45 | 53 | }
|
46 | 54 |
|
47 | 55 | #' @rdname sunrise_sunset
|
48 | 56 | #'
|
49 | 57 | #' @export
|
50 |
| -sunset <- function(date, lon, lat, elev = -0.268, tz = "UTC") { |
| 58 | +sunset <- function(date, lon, lat, elev = -0.268, tz = "UTC", force_tz = FALSE) { |
51 | 59 | locations <- data.frame(lon = lon, lat = lat)
|
52 | 60 | locations <- SpatialPoints(locations, proj4string = CRS("+proj=longlat +datum=WGS84"))
|
53 |
| - datetime <- as.POSIXct(date, tz = tz) |
| 61 | + datetime <- as.POSIXct(date, tz = tz) # tz ignored if already set |
| 62 | + if(force_tz) datetime <- as_datetime(datetime, tz=tz) |
54 | 63 | suntimes <- crepuscule(locations, datetime, solarDep = -elev, direction = "dusk", POSIXct.out = TRUE)
|
55 | 64 | suntimes$time
|
56 | 65 | }
|
0 commit comments