This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2021-12-01_tom_release_schedule.R
130 lines (99 loc) · 3.55 KB
/
2021-12-01_tom_release_schedule.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Description: Automate release schedule dates for main GENIE by calculating
# the #nd ***day of each month between a specified date range.
# Author: Haley Hunter-Zinck
# Date: 2021-12-01
# pre-setup ---------------------------
library(optparse)
waitifnot <- function(cond, msg) {
if (!cond) {
for (str in msg) {
message(str)
}
message("Press control-C to exit and try again.")
while(T) {}
}
}
# user input ----------------------------
option_list <- list(
make_option(c("-s", "--date_start"), type = "character",
help="start date in YYYY-MM-DD format"),
make_option(c("-e", "--date_end"), type = "character",
help="end date in YYYY-MM-DD format"),
make_option(c("-n", "--number_occurrence"), type = "integer",
help="Number of the occurrence of a day of week in a month"),
make_option(c("-d", "--day_of_week"), type = "character",
help="Day of the week, either full day or 3-letter abbreviation")
)
opt <- parse_args(OptionParser(option_list=option_list))
waitifnot(!is.null(opt$date_start) && !is.null(opt$date_end) && !is.null(opt$number_occurrence) && !is.null(opt$day_of_week),
msg = "Rscript 2021-12-01_tom_release_schedule.R -h")
date_start <- opt$date_start
date_end <- opt$date_end
n_occ <- opt$number_occurrence
dow <- opt$day_of_week
# setup ----------------------------
tic = as.double(Sys.time())
library(glue)
library(dplyr)
library(lubridate)
# functions ----------------------------
#' Get the first date of a month corresponding to the date.
#'
#' @param @date Date as a string in YYYY-MM-DD format
#' @return First day of the month
floor_date <- function(date) {
return(ymd(glue("{year(date)}-{month(date)}-01")))
}
#' Get the last date of a month corresponding to the date.
#'
#' @param @date Date as a string in YYYY-MM-DD format
#' @return Last day of the month
ceiling_date <- function(date) {
y_date <- year(date)
m_date <- month(date)
d_date <- day(date)
next_first <- ""
if (m_date == 12) {
next_first <- ymd(glue("{y_date + 1}-01-01"))
} else {
next_first <- ymd(glue("{y_date}-{m_date + 1}-01"))
}
return(next_first - 1)
}
capitalize <- function(str) {
first <- substr(str, 1, 1)
rest <- substr(str, 2, nchar(str))
return(glue("{toupper(first)}{tolower(rest)}"))
}
format_dow <- function(dow) {
return(substr(capitalize(dow), 1, 3))
}
get_occ_dow <- function(date_start, date_end, n_occ, dow) {
# floor dates to month
floor_start <- floor_date(date_start)
ceiling_end <- ceiling_date(date_end)
# get dow of all dates
seq_date <- floor_start + 1:as.integer(ceiling_end - floor_start)
obj <- data.frame(date = seq_date,
wd = wday(seq_date, label = T, abbr = T) ,
month = month(seq_date))
ordering <- obj %>%
filter(wd == format_dow(dow)) %>%
group_by(month) %>%
group_rows()
idx <- unlist(lapply(ordering, function(x) {return(x[n_occ])}))
targets <- (obj %>%
filter(wd == format_dow(dow)) %>%
select(date))[["date"]][idx]
return(targets[targets >= date_start & targets <= date_end])
}
# main ----------------------------
targets <- get_occ_dow(date_start = date_start,
date_end = date_end,
n_occ = n_occ,
dow = dow)
# close out ----------------------------
str_targets <- paste0("\n", paste0(targets, collapse = "\n"))
print(glue("{dow} number {n_occ} in month(s) between {date_start} and {date_end}: {str_targets}"))
toc = as.double(Sys.time())
print(glue("Runtime: {round(toc - tic)} s"))