Skip to content

Commit

Permalink
Latest season with labor day (#221)
Browse files Browse the repository at this point in the history
`most_recent_season()` now internally computes the exact day of the
season opener (= Thursday after first Monday of September).
  • Loading branch information
mrcaseb authored Sep 19, 2023
1 parent bc9ab2f commit fd280b1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-8388-5155")),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---

Expand Down
32 changes: 29 additions & 3 deletions R/utils_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 )) {

Expand Down Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion man/latest_season.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd280b1

Please sign in to comment.