-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathexcel-date-functions.R
606 lines (509 loc) · 16.8 KB
/
excel-date-functions.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#' Excel Date and Time Functions
#'
#' @description
#' 50+ date and time functions familiar to users coming from an __Excel Background.__
#' The main benefits are:
#'
#' 1. Integration of the amazing `lubridate` package for handling dates and times
#' 2. Integration of Holidays from `timeDate` and Business Calendars
#' 3. New Date Math and Date Sequence Functions that factor in Business Calendars (e.g. [EOMONTH()], [NET_WORKDAYS()])
#'
#' These functions are designed to help users coming from an __Excel background__.
#' Most functions replicate the behavior of Excel:
#' - Names in most cases match Excel function names
#' - Functionality replicates Excel
#' - By default, missing values are ignored (same as in Excel)
#'
#' @details
#'
#' __Converters__ - Make date and date-time from text (character data)
#' * General String-to-Date Conversion: [AS_DATE()], [AS_DATETIME()]
#' * Format-Specific String-to-Date Conversion: [YMD()] (YYYY-MM-DD), [MDY()] (MM-DD-YYYY), [DMY()] (DD-MM-YYYY)
#' * Hour-Minute-Second Conversion: [YMD_HMS()], [YMD_HM()], and friends.
#'
#' __Extractors__ - Returns information from a time-stamp.
#' * Extractors: [SECOND()], [MINUTE()], [HOUR()], [DAY()], [WEEK()], [MONTH()], [QUARTER()], [YEAR()]
#'
#' __Current Time__ - Returns the current date/date-time based on your locale.
#' * [NOW()], [TODAY()]
#'
#' __Date Math__ - Perform popular Excel date calculations
#' * [EOMONTH()] - End of Month
#' * [NET_WORKDAYS()], [COUNT_DAYS()] - Return number of days between 2 dates factoring in working days and holidays
#' * [YEARFRAC()] - Return the fractional period of the year that has been completed between 2 dates.
#'
#' __Date Sequences__ - Return a vector of dates or a Holiday Table (`tibble`).
#' * [DATE_SEQUENCE()], [WORKDAY_SEQUENCE()], [HOLIDAY_SEQUENCE] - Return a sequence of dates between 2 dates that
#' factor in workdays and `timeDate` holiday calendars for popular business calendars including NYSE and London stock exchange.
#'
#' __Date Collapsers__ - Collapse a date sequence (useful in `dplyr::group_by()` and [pivot_table()])
#' * [FLOOR_DATE()], [FLOOR_DAY()], [FLOOR_WEEK()], [FLOOR_MONTH()], [FLOOR_QUARTER()], [FLOOR_YEAR()]
#' * Similar functions exist for CEILING and ROUND. These are wrappers for `lubridate` functions.
#'
#' @param x A vector of date or date-time objects
#' @param ... Parameters passed to underlying `lubridate` functions.
#' @param label A logical used for [MONTH()] and [WEEKDAY()] Date Extractors to decide whether or not to return names
#' (as ordered factors) or numeric values.
#' @param abbr A logical used for [MONTH()] and [WEEKDAY()]. If `label = TRUE`, used to determine if
#' full names (e.g. Wednesday) or abbreviated names (e.g. Wed) should be returned.
#' @param include_year A logical value used in [QUARTER()]. Determines whether or not to return 2020 Q3 as `3` or `2020.3`.
#' @param fiscal_start A numeric value used in [QUARTER()]. Determines the fiscal-year starting quarter.
#' @param by Used to determine the gap in Date Sequence calculations and value to round to in Date Collapsing operations.
#' Acceptable values are: A character string, containing one of `"day"`, `"week"`, `"month"`, `"quarter"` or `"year"`.
#' @param start_date Used in Date Math and Date Sequence operations. The starting date in the calculation.
#' @param end_date Used in Date Math and Date Sequence operations. The ending date in the calculation.
#' @param remove_weekends A logical value used in Date Sequence and Date Math calculations.
#' Indicates whether or not weekends should be removed from the calculation.
#' @param holidays A vector of dates corresponding to holidays that should be removed from the calculation.
#' @param calendar The calendar to be used in Date Sequence calculations for Holidays from the `timeDate` package.
#' Acceptable values are: `"NYSE"`, `"LONDON"`, `"NERC"`, `"TSX"`, `"ZURICH"`
#' @param pattern Used to filter Holidays (e.g. `pattern = "Easter"`). A "regular expression" filtering pattern.
#' @param year Used in [DATE()]
#' @param month Used in [DATE()]
#' @param day Used in [DATE()]
#' @param months Used to offset months in [EOMONTH()] AND [EDATE()] Date Math calculations
#' @param years A numeric vector of years to return Holidays for in [HOLIDAY_TABLE()]
#'
#'
#'
#' @return
#' - __Converters__ - Date or date-time object the length of x
#' - __Extractors__ - Returns information from a time-stamp.
#' - __Current Time__ - Returns the current date/date-time based on your locale.
#' - __Date Math__ - Numeric values or Date Values depending on the calculation.
#' - __Date Sequences__ - Return a vector of dates or a Holiday Table (`tibble`).
#' - __Date Collapsers__ - Date or date-time object the length of x
#'
#'
#'
#' @examples
#' # Libraries
#' library(lubridate)
#'
#' # --- Basic Usage ----
#'
#' # Converters ---
#' AS_DATE("2011 Jan-01") # General
#' YMD("2011 Jan-01") # Year, Month-Day Format
#' MDY("01-02-20") # Month-Day, Year Format (January 2nd, 2020)
#' DMY("01-02-20") # Day-Month, Year Format (February 1st, 2020)
#'
#' # Extractors ---
#' WEEKDAY("2020-01-01") # Labelled Day
#' WEEKDAY("2020-01-01", label = FALSE) # Numeric Day
#' WEEKDAY("2020-01-01", label = FALSE, week_start = 1) # Start at 1 (Monday) vs 7 (Sunday)
#' MONTH("2020-01-01")
#' QUARTER("2020-01-01")
#' YEAR("2020-01-01")
#'
#' # Current Date-Time ---
#' NOW()
#' TODAY()
#'
#' # Date Math ---
#' EOMONTH("2020-01-01")
#' EOMONTH("2020-01-01", months = 1)
#' NET_WORKDAYS("2020-01-01", "2020-07-01") # 131 Skipping Weekends
#' NET_WORKDAYS("2020-01-01", "2020-07-01",
#' holidays = HOLIDAY_SEQUENCE("2020-01-01", "2020-07-01",
#' calendar = "NYSE")) # 126 Skipping 5 NYSE Holidays
#'
#' # Date Sequences ---
#' DATE_SEQUENCE("2020-01-01", "2020-07-01")
#' WORKDAY_SEQUENCE("2020-01-01", "2020-07-01")
#' HOLIDAY_SEQUENCE("2020-01-01", "2020-07-01", calendar = "NYSE")
#' WORKDAY_SEQUENCE("2020-01-01", "2020-07-01",
#' holidays = HOLIDAY_SEQUENCE("2020-01-01", "2020-07-01",
#' calendar = "NYSE"))
#'
#' # Date Collapsers ---
#' FLOOR_DATE(AS_DATE("2020-01-15"), by = "month")
#' CEILING_DATE(AS_DATE("2020-01-15"), by = "month")
#' CEILING_DATE(AS_DATE("2020-01-15"), by = "month") - ddays(1) # EOMONTH using lubridate
#'
#' # --- Usage with tidyverse ---
#'
#' # Calculate returns by symbol/year/quarter
#' FANG %>%
#' pivot_table(
#' .rows = c(symbol, ~ QUARTER(date)),
#' .columns = ~ YEAR(date),
#' .values = ~ PCT_CHANGE_FIRSTLAST(adjusted)
#' )
#'
#' @name excel_date_functions
NULL
# CONVERTERS ----
#' @rdname excel_date_functions
#' @export
AS_DATE <- function(x, ...) {
lubridate::as_date(x, ...)
}
#' @rdname excel_date_functions
#' @export
AS_DATETIME <- function(x, ...) {
lubridate::as_datetime(x, ...)
}
#' @rdname excel_date_functions
#' @export
DATE <- function(year, month, day) {
lubridate::ymd(stringr::str_c(year, month, day, sep = "-"))
}
#' @rdname excel_date_functions
#' @export
DATEVALUE <- AS_DATE
#' @rdname excel_date_functions
#' @export
YMD <- function(x, ...) {
lubridate::ymd(x, ...)
}
#' @rdname excel_date_functions
#' @export
MDY <- function(x, ...) {
lubridate::mdy(x, ...)
}
#' @rdname excel_date_functions
#' @export
DMY <- function(x, ...) {
lubridate::dmy(x, ...)
}
#' @rdname excel_date_functions
#' @export
YMD_HMS <- function(x, ...) {
lubridate::ymd_hms(x, ...)
}
#' @rdname excel_date_functions
#' @export
MDY_HMS <- function(x, ...) {
lubridate::mdy_hms(x, ...)
}
#' @rdname excel_date_functions
#' @export
DMY_HMS <- function(x, ...) {
lubridate::dmy_hms(x, ...)
}
#' @rdname excel_date_functions
#' @export
YMD_HM <- function(x, ...) {
lubridate::ymd_hm(x, ...)
}
#' @rdname excel_date_functions
#' @export
MDY_HM <- function(x, ...) {
lubridate::mdy_hm(x, ...)
}
#' @rdname excel_date_functions
#' @export
DMY_HM <- function(x, ...) {
lubridate::dmy_hm(x, ...)
}
#' @rdname excel_date_functions
#' @export
YMD_H <- function(x, ...) {
lubridate::ymd_h(x, ...)
}
#' @rdname excel_date_functions
#' @export
MDY_H <- function(x, ...) {
lubridate::mdy_h(x, ...)
}
#' @rdname excel_date_functions
#' @export
DMY_H <- function(x, ...) {
lubridate::dmy_h(x, ...)
}
# EXTRACTORS ----
#' @rdname excel_date_functions
#' @export
WEEKDAY <- function(x, ..., label = FALSE, abbr = TRUE) {
lubridate::wday(x, ..., label = label, abbr = abbr)
}
#' @rdname excel_date_functions
#' @export
WDAY <- WEEKDAY
#' @rdname excel_date_functions
#' @export
DOW <- WEEKDAY
#' @rdname excel_date_functions
#' @export
MONTHDAY <- function(x, ...) {
lubridate::mday(x, ...)
}
#' @rdname excel_date_functions
#' @export
MDAY <- MONTHDAY
#' @rdname excel_date_functions
#' @export
DOM <- MONTHDAY
#' @rdname excel_date_functions
#' @export
QUARTERDAY <- function(x, ...) {
lubridate::qday(x, ...)
}
#' @rdname excel_date_functions
#' @export
QDAY <- QUARTERDAY
#' @rdname excel_date_functions
#' @export
DAY <- function(x, ...) {
lubridate::day(x, ...)
}
#' @rdname excel_date_functions
#' @export
WEEKNUM <- function(x, ...) {
lubridate::week(x, ...)
}
#' @rdname excel_date_functions
#' @export
WEEK <- WEEKNUM
#' @rdname excel_date_functions
#' @export
WEEKNUM_ISO <- function(x, ...) {
lubridate::isoweek(x, ...)
}
#' @rdname excel_date_functions
#' @export
MONTH <- function(x, ..., label = FALSE, abbr = TRUE) {
lubridate::month(x, ..., label = label, abbr = abbr)
}
#' @rdname excel_date_functions
#' @export
QUARTER <- function(x, ..., include_year = FALSE, fiscal_start = 1) {
lubridate::quarter(x, ..., with_year = include_year, fiscal_start = fiscal_start)
}
#' @rdname excel_date_functions
#' @export
YEAR <- function(x, ...) {
lubridate::year(x, ...)
}
#' @rdname excel_date_functions
#' @export
YEAR_ISO <- function(x, ...) {
lubridate::isoyear(x, ...)
}
#' @rdname excel_date_functions
#' @export
DATE_TO_NUMERIC <- function(x, ...) {
as.numeric(as.POSIXct(x, ...))
}
#' @rdname excel_date_functions
#' @export
DATE_TO_DECIMAL <- function(x, ...) {
lubridate::decimal_date(x, ...)
}
#' @rdname excel_date_functions
#' @export
SECOND <- function(x, ...) {
lubridate::second(x, ...)
}
#' @rdname excel_date_functions
#' @export
MINUTE <- function(x, ...) {
lubridate::minute(x, ...)
}
#' @rdname excel_date_functions
#' @export
HOUR <- function(x, ...) {
lubridate::hour(x, ...)
}
# CURRENT DATE-TIME ----
#' @rdname excel_date_functions
#' @export
NOW <- function(...) {
lubridate::now(...)
}
#' @rdname excel_date_functions
#' @export
TODAY <- function(...) {
lubridate::today(...)
}
# DATE MATH ----
#' @rdname excel_date_functions
#' @export
EOMONTH <- function(start_date, months = 0) {
if (rlang::is_missing(start_date)) start_date <- TODAY()
start_date <- lubridate::as_date(start_date)
lubridate::month(start_date) <- lubridate::month(start_date) + months
lubridate::ceiling_date(start_date, unit = "month") - lubridate::ddays(1)
}
#' @rdname excel_date_functions
#' @export
EDATE <- function(start_date, months = 0) {
if (rlang::is_missing(start_date)) start_date <- TODAY()
start_date <- AS_DATE(start_date)
lubridate::month(start_date) <- lubridate::month(start_date) + months
start_date
}
#' @rdname excel_date_functions
#' @export
NET_WORKDAYS <- function(start_date, end_date, remove_weekends = TRUE, holidays = NULL) {
start_date <- AS_DATE(start_date)
end_date <- AS_DATE(end_date)
WORKDAY_SEQUENCE(start_date = start_date, end_date = end_date,
remove_weekends = remove_weekends, holidays = holidays) %>%
COUNT()
}
#' @rdname excel_date_functions
#' @export
COUNT_DAYS <- function(start_date, end_date) {
start_date <- AS_DATE(start_date)
end_date <- AS_DATE(end_date)
DATE_SEQUENCE(start_date = start_date, end_date = end_date, by = "day") %>% COUNT()
}
#' @rdname excel_date_functions
#' @export
YEARFRAC <- function(start_date, end_date) {
start_date <- AS_DATE(start_date)
end_date <- AS_DATE(end_date)
partial_year <- DATE_SEQUENCE(start_date = start_date, end_date = end_date, by = "day") %>% COUNT()
full_year <- DATE_SEQUENCE(start_date = start_date,
end_date = lubridate::ceiling_date(end_date, unit = "year") - lubridate::ddays(1),
by = "day") %>%
COUNT()
(partial_year - 1) / full_year
}
# DATE SEQUENCES AND HOLIDAYS ----
#' @rdname excel_date_functions
#' @export
DATE_SEQUENCE <- function(start_date, end_date, by = "day") {
seq.Date(from = AS_DATE(start_date),
to = AS_DATE(end_date),
by = by)
}
#' @rdname excel_date_functions
#' @export
WORKDAY_SEQUENCE <- function(start_date, end_date, remove_weekends = TRUE, holidays = NULL) {
day_sequence <- DATE_SEQUENCE(start_date, end_date, by = "day")
ret_tbl <- tibble::tibble(day_sequence = day_sequence) %>%
dplyr::mutate(weekday = WEEKDAY(day_sequence, label = TRUE))
if (remove_weekends) {
ret_tbl <- ret_tbl %>%
dplyr::filter(!(weekday == "Sat" | weekday == "Sun"))
}
if (!is.null(holidays)) {
if (!is.Date(holidays)) stop("WORKDAY_SEQUENCE(): holidays must be a date sequence (vector of dates).", call. = FALSE)
ret_tbl <- ret_tbl %>%
dplyr::filter(!(day_sequence %in% holidays))
}
ret_tbl %>% dplyr::pull(day_sequence)
}
#' @rdname excel_date_functions
#' @export
HOLIDAY_SEQUENCE <- function(start_date, end_date,
calendar = c("NYSE", "LONDON", "NERC", "TSX", "ZURICH")) {
fun <- switch(
tolower(calendar[1]),
"nyse" = timeDate::holidayNYSE,
"london" = timeDate::holidayLONDON,
"nerc" = timeDate::holidayNERC,
"tsx" = timeDate::holidayTSX,
"zurich" = timeDate::holidayZURICH
)
date_seq <- DATE_SEQUENCE(start_date, end_date)
years <- date_seq %>% YEAR() %>% unique()
holidays <- fun(year = years) %>% AS_DATE()
return(holidays[holidays %in% date_seq])
}
#' @rdname excel_date_functions
#' @export
HOLIDAY_TABLE <- function(years, pattern = ".") {
if (rlang::is_missing(years)) years = YEAR(TODAY())
tibble::tibble(holidays = timeDate::listHolidays(pattern = pattern)) %>%
dplyr::mutate(date = purrr::map(holidays, .f = function(holiday) {
timeDate::holiday(years, Holiday = holiday) %>% AS_DATE()
})
) %>%
tidyr::unnest(date) %>%
dplyr::mutate(year = YEAR(date))
}
# DATE COLLAPSERS ----
#' @rdname excel_date_functions
#' @export
FLOOR_DATE <- function(x, ..., by = "day") {
lubridate::floor_date(x, ..., unit = by)
}
#' @rdname excel_date_functions
#' @export
FLOOR_DAY <- function(x, ...) {
lubridate::floor_date(x, ..., unit = "day")
}
#' @rdname excel_date_functions
#' @export
FLOOR_WEEK<- function(x, ...) {
lubridate::floor_date(x, ..., unit = "week")
}
#' @rdname excel_date_functions
#' @export
FLOOR_MONTH <- function(x, ...) {
lubridate::floor_date(x, ..., unit = "month")
}
#' @rdname excel_date_functions
#' @export
FLOOR_QUARTER <- function(x, ...) {
lubridate::floor_date(x, ..., unit = "quarter")
}
#' @rdname excel_date_functions
#' @export
FLOOR_YEAR <- function(x, ...) {
lubridate::floor_date(x, ..., unit = "year")
}
#' @rdname excel_date_functions
#' @export
CEILING_DATE <- function(x, ..., by = "day") {
lubridate::ceiling_date(x, ..., unit = by)
}
#' @rdname excel_date_functions
#' @export
CEILING_DAY <- function(x, ...) {
lubridate::ceiling_date(x, ..., unit = "day")
}
#' @rdname excel_date_functions
#' @export
CEILING_WEEK <- function(x, ...) {
lubridate::ceiling_date(x, ..., unit = "week")
}
#' @rdname excel_date_functions
#' @export
CEILING_MONTH <- function(x, ...) {
lubridate::ceiling_date(x, ..., unit = "month")
}
#' @rdname excel_date_functions
#' @export
CEILING_QUARTER <- function(x, ...) {
lubridate::ceiling_date(x, ..., unit = "quarter")
}
#' @rdname excel_date_functions
#' @export
CEILING_YEAR <- function(x, ...) {
lubridate::ceiling_date(x, ..., unit = "year")
}
#' @rdname excel_date_functions
#' @export
ROUND_DATE <- function(x, ..., by = "day") {
lubridate::round_date(x, ..., unit = by)
}
#' @rdname excel_date_functions
#' @export
ROUND_DAY <- function(x, ...) {
lubridate::round_date(x, ..., unit = "day")
}
#' @rdname excel_date_functions
#' @export
ROUND_WEEK <- function(x, ...) {
lubridate::round_date(x, ..., unit = "week")
}
#' @rdname excel_date_functions
#' @export
ROUND_MONTH <- function(x, ...) {
lubridate::round_date(x, ..., unit = "month")
}
#' @rdname excel_date_functions
#' @export
ROUND_QUARTER <- function(x, ...) {
lubridate::round_date(x, ..., unit = "quarter")
}
#' @rdname excel_date_functions
#' @export
ROUND_YEAR <- function(x, ...) {
lubridate::round_date(x, ..., unit = "year")
}