Skip to content

Commit 4968019

Browse files
committed
add force_tz option
1 parent 76b754c commit 4968019

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

R/sunrise_sunset.R

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#' @param lat Latitude in decimal degrees.
99
#' @param elev Sun elevation in degrees.
1010
#' @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}.
1112
#'
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.
1415
#'
1516
#' @details The angular diameter of the sun is about 0.536 degrees,
1617
#' therefore the moment of sunrise/sunset corresponds to half that elevation
@@ -21,6 +22,12 @@
2122
#' Approximate astronomical formula are used, therefore the moment of
2223
#' sunrise / sunset may be off by a few minutes
2324
#'
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+
#'
2431
#' @examples
2532
#' # sunrise in the Netherlands
2633
#' sunrise("2016-01-01", 5, 53)
@@ -36,21 +43,23 @@ NULL
3643
#' @rdname sunrise_sunset
3744
#'
3845
#' @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) {
4047
locations <- data.frame(lon = lon, lat = lat)
4148
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)
4351
suntimes <- crepuscule(locations, datetime, solarDep = -elev, direction = "dawn", POSIXct.out = TRUE)
4452
suntimes$time
4553
}
4654

4755
#' @rdname sunrise_sunset
4856
#'
4957
#' @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) {
5159
locations <- data.frame(lon = lon, lat = lat)
5260
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)
5463
suntimes <- crepuscule(locations, datetime, solarDep = -elev, direction = "dusk", POSIXct.out = TRUE)
5564
suntimes$time
5665
}

man/sunrise_sunset.Rd

+12-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)