R/datetime_as_seasonweek.R

Defines functions datetime_as_seasonweek

Documented in datetime_as_seasonweek

#' Calculate the isoweek from a given datetime
#'
#' \lifecycle{stable}
#'
#' @details Return the isoweek of a given datetime.
#'
#' The isoweek is useful as a grouping variable for weekly summaries.
#'
#' @param datetime (dttm) A datetime
#' @return The ISO week as int, e.g. 26
#' @importFrom lubridate days isoweek
#' @export
#' @family helpers
#' @examples
#' # These datetimes are turtle season 2017:
#' datetime_as_seasonweek(httpdate_as_gmt08("2017-07-02T15:59:59Z")) # 0
#' datetime_as_seasonweek(httpdate_as_gmt08("2017-07-02T16:00:00Z")) # 1
#' datetime_as_seasonweek(httpdate_as_gmt08("2017-08-30T06:38:43Z")) # 9
#' datetime_as_seasonweek(httpdate_as_gmt08("2017-11-01T22:00:00Z")) # 18
#' datetime_as_seasonweek(httpdate_as_gmt08("2018-11-01T22:00:00Z")) # 18
datetime_as_seasonweek <- function(datetime) {
  (lubridate::isoweek(datetime) + 26) %% 52
}

# usethis::use_test("datetime_as_seasonweek")
parksandwildlife/wastdr documentation built on Nov. 17, 2022, 4:52 p.m.