From fd280b1ee6e5586158881959134d8ed809a9c229 Mon Sep 17 00:00:00 2001 From: mrcaseb <38586519+mrcaseb@users.noreply.github.com> Date: Tue, 19 Sep 2023 04:35:47 +0200 Subject: [PATCH] Latest season with labor day (#221) `most_recent_season()` now internally computes the exact day of the season opener (= Thursday after first Monday of September). --- DESCRIPTION | 2 +- NEWS.md | 1 + R/utils_date.R | 32 +++++++++++++++++++++++++++++--- man/latest_season.Rd | 5 ++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f4644ffd..e4a3847b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: nflreadr Title: Download 'nflverse' Data -Version: 1.4.0.04 +Version: 1.4.0.05 Authors@R: c( person("Tan", "Ho", , "tan@tanho.ca", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0001-8388-5155")), diff --git a/NEWS.md b/NEWS.md index ee262d38..731eae5f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ - `%c%` internal helper now uses `data.table::fifelse()` to avoid falsely converting dates to integers. (#214) - `load_schedules()` cleans the `roof` variable in order to avoid nflverse model issues. (#218) - `join_coalesce()` coerces x/y args to data.frame and will return a data.frame +- `most_recent_season()` now internally computes the exact day of the season opener (= Thursday after first Monday of September). (#221) --- diff --git a/R/utils_date.R b/R/utils_date.R index ece65254..32ffedf5 100644 --- a/R/utils_date.R +++ b/R/utils_date.R @@ -2,8 +2,10 @@ #' #' A helper function to choose the most recent season available for a given dataset #' -#' @param roster a TRUE/FALSE flag: if TRUE, returns the current year if March 15th or later. if FALSE, returns the current year if September 1st or later. Otherwise returns current year minus 1. -#' +#' @param roster Either `TRUE` or `FALSE`. +#' If `TRUE`, will return current year after March 15th, otherwise previous year. +#' If `FALSE`, will return current year on or after Thursday following Labor Day, +#' i.e. Thursday after the first Monday in September. Otherwise previous year. #' #' @rdname latest_season #' @return most recent season (a four digit numeric) @@ -14,8 +16,12 @@ most_recent_season <- function(roster = FALSE) { current_year <- as.integer(format(today, format = "%Y")) current_month <- as.integer(format(today, format = "%m")) current_day <- as.integer(format(today, format = "%d")) + # First Monday of September + labor_day <- compute_labor_day(current_year) + # Thursday following Labor Day is TNF season opener + season_opener <- labor_day + 3 - if ((isFALSE(roster) && current_month >= 9) || + if ((isFALSE(roster) && today >= season_opener) || (isTRUE(roster) && current_month == 3 && current_day >= 15) || (isTRUE(roster) && current_month > 3 )) { @@ -93,3 +99,23 @@ get_current_week <- function(use_date = FALSE) { return(current_week) } } + +#' Compute Date of Labor Day +#' +#' Computes the date of the Labor Day, i.e. the first Monday in September, in a given year. +#' +#' @param year Numeric or Character year. 4 Digits. +#' @noRd +#' @return Object of Class `Date` +#' @examples +#' # 2023 Labor Day was on Sep 4th +#' compute_labor_day(2023) +compute_labor_day <- function(year){ + stopifnot(length(year) == 1) + earliest <- as.Date(paste(year, "09", "01", sep = "-")) + latest <- as.Date(paste(year, "09", "08", sep = "-")) + range <- seq(earliest, latest, by = "day") + numeric_wdays <- as.POSIXlt(range)$wday + labor_day <- range[numeric_wdays == 1][1] + labor_day +} diff --git a/man/latest_season.Rd b/man/latest_season.Rd index 4b06d03a..ffef94ad 100644 --- a/man/latest_season.Rd +++ b/man/latest_season.Rd @@ -13,7 +13,10 @@ get_latest_season(roster = FALSE) get_current_season(roster = FALSE) } \arguments{ -\item{roster}{a TRUE/FALSE flag: if TRUE, returns the current year if March 15th or later. if FALSE, returns the current year if September 1st or later. Otherwise returns current year minus 1.} +\item{roster}{Either \code{TRUE} or \code{FALSE}. +If \code{TRUE}, will return current year after March 15th, otherwise previous year. +If \code{FALSE}, will return current year on or after Thursday following Labor Day, +i.e. Thursday after the first Monday in September. Otherwise previous year.} } \value{ most recent season (a four digit numeric)