-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbeop.R
52 lines (51 loc) · 2.44 KB
/
beop.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
#' Find the Beginning or End of Period
#'
#' Each of `bop` and `eop` contains a list of functions, whose names all
#' consist of two letters, the first of which stands for **l**ast, **t**his, **n**ext
#' while the second stands for **y**ear, **q**uarter, **m**onth, **w**eek.
#' For example, `eop$ty()` means "the **e**nding **o**f **p**eriod of **t**his **y**ear"
#' and `bop$lm()` means "the **b**eginning **o**f **p**eriod of **l**ast **m**onth".
#' @details All functions' signatures are the same, with only one argument
#' `x`, which could be a `Date` or values that can be converted to `Date` via [ymd()].
#' @usage NULL
#' @format NULL
#' @examples
#' bop$ty(as.Date("2021-03-02"))
#' ## supports 'YMD' formatted integer or string
#' bop$ty(210302)
#' eop$tm(200201)
#'
#' @name beop
#' @export
eop <- list(
ly = function(x = Sys.Date()) period_begin(x, 'year') - 1,
ty = function(x = Sys.Date()) period_end(x, 'year'),
ny = function(x = Sys.Date()) period_end(period_end(x, 'year') + 1, 'year'),
lq = function(x = Sys.Date()) period_begin(x, 'quarter') - 1,
tq = function(x = Sys.Date()) period_end(x, 'quarter'),
nq = function(x = Sys.Date()) period_end(period_end(x, 'quarter') + 1, 'quarter'),
lm = function(x = Sys.Date()) period_begin(x, 'month') - 1,
tm = function(x = Sys.Date()) period_end(x, 'month'),
nm = function(x = Sys.Date()) period_end(period_end(x, 'month') + 1, 'month'),
lw = function(x = Sys.Date()) period_begin(x, 'week') - 1,
tw = function(x = Sys.Date()) period_end(x, 'week'),
nw = function(x = Sys.Date()) period_end(period_end(x, 'week') + 1, 'week')
)
#' @usage NULL
#' @format NULL
#' @name beop
#' @export
bop <- list(
ly = function(x = Sys.Date()) period_begin(period_begin(x, 'year') - 1, 'year'),
ty = function(x = Sys.Date()) period_begin(x, 'year'),
ny = function(x = Sys.Date()) period_end(x, 'year') + 1,
lq = function(x = Sys.Date()) period_begin(period_begin(x, 'quarter') - 1, 'quarter'),
tq = function(x = Sys.Date()) period_begin(x, 'quarter'),
nq = function(x = Sys.Date()) period_end(x, 'quarter') + 1,
lm = function(x = Sys.Date()) period_begin(period_begin(x, 'month') - 1, 'month'),
tm = function(x = Sys.Date()) period_begin(x, 'month'),
nm = function(x = Sys.Date()) period_end(x, 'month') + 1,
lw = function(x = Sys.Date()) period_begin(period_begin(x, 'week') - 1, 'week'),
tw = function(x = Sys.Date()) period_begin(x, 'week'),
nw = function(x = Sys.Date()) period_end(x, 'week') + 1
)