-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_current_week.R
43 lines (31 loc) · 1.08 KB
/
get_current_week.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
`%>%`<-magrittr::`%>%`
get_current_week <- function() {
# get season
s <- dplyr::if_else(
lubridate::month(lubridate::today("GMT")) >= 9,
lubridate::year(lubridate::today("GMT")) ,
lubridate::year(lubridate::today("GMT")) - 1
)
# Find the first Monday of September
day1 <- lubridate::as_date(paste(s, "09-01", sep="-"))
week1 <- as.POSIXlt(seq(day1, length.out=7, by="day"))
monday1 <- week1[week1$wday == 1]
# NFL season starts 4 days later
first_game <- (monday1 + lubridate::days(3)) %>% lubridate::as_date()
# current week number of NFL season is 1 + how many weeks have elapsed since first game
# (ie first game is week 1)
current_week <- 1 +
lubridate::days(lubridate::today("America/New_York") - first_game) %>%
.$day %>%
magrittr::divide_by_int(7)
if (current_week <= 1) current_week <- 1
return(current_week)
}
get_current_season <- function() {
s <- dplyr::if_else(
lubridate::month(lubridate::today("GMT")) >= 9,
lubridate::year(lubridate::today("GMT")) ,
lubridate::year(lubridate::today("GMT")) - 1
)
return(s)
}