|
1 | 1 | #' Convenience functions for dealing with financial years |
2 | 2 | #' |
| 3 | +#' @description From grattan v1.7.1.4, these are reexports from the \code{\link[fy]{fy-package}}. |
| 4 | +#' |
3 | 5 | #' @name is.fy |
4 | 6 | #' @aliases fy.year yr2fy fy2yr fy2date date2fy |
5 | 7 | #' @param yr_ending An integer representing a year. |
|
23 | 25 | #' |
24 | 26 | #' \code{date2fy} converts a date to the corresponding financial year. |
25 | 27 | #' |
| 28 | +#' @importFrom fy validate_fys_permitted |
26 | 29 | #' |
27 | 30 | #' @examples |
28 | 31 | #' is.fy("2012-13") |
|
33 | 36 | #' @export is.fy fy.year yr2fy fy2yr fy2date date2fy |
34 | 37 | NULL |
35 | 38 |
|
| 39 | +is.fy <- fy::is_fy |
36 | 40 |
|
37 | | - |
38 | | -is.fy <- function(fy.yr){ |
39 | | - out <- logical(length(fy.yr)) |
40 | | - potential_fys <- grepl("^([12][0-9]{3})[-\\s]?[0-9]{2}$", fy.yr, perl = TRUE) |
41 | | - out[potential_fys] <- |
42 | | - {as.integer(sub("^([12][0-9]{3})[-\\s]?[0-9]{2}$", "\\1", fy.yr[potential_fys], perl = TRUE)) + 1L} %% 100L == as.integer(sub("^[12][0-9]{3}[-\\s]?([0-9]{2})$", "\\1", fy.yr[potential_fys], perl = TRUE)) |
43 | | - out |
44 | | -} |
45 | | - |
46 | | -is_fy2 <- function(x) { |
47 | | - a <- c("1897-98", "1898-99", "1899-00", "1900-01", "1901-02", "1902-03", |
48 | | - "1903-04", "1904-05", "1905-06", "1906-07", "1907-08", "1908-09", |
49 | | - "1909-10", "1910-11", "1911-12", "1912-13", "1913-14", "1914-15", |
50 | | - "1915-16", "1916-17", "1917-18", "1918-19", "1919-20", "1920-21", |
51 | | - "1921-22", "1922-23", "1923-24", "1924-25", "1925-26", "1926-27", |
52 | | - "1927-28", "1928-29", "1929-30", "1930-31", "1931-32", "1932-33", |
53 | | - "1933-34", "1934-35", "1935-36", "1936-37", "1937-38", "1938-39", |
54 | | - "1939-40", "1940-41", "1941-42", "1942-43", "1943-44", "1944-45", |
55 | | - "1945-46", "1946-47", "1947-48", "1948-49", "1949-50", "1950-51", |
56 | | - "1951-52", "1952-53", "1953-54", "1954-55", "1955-56", "1956-57", |
57 | | - "1957-58", "1958-59", "1959-60", "1960-61", "1961-62", "1962-63", |
58 | | - "1963-64", "1964-65", "1965-66", "1966-67", "1967-68", "1968-69", |
59 | | - "1969-70", "1970-71", "1971-72", "1972-73", "1973-74", "1974-75", |
60 | | - "1975-76", "1976-77", "1977-78", "1978-79", "1979-80", "1980-81", |
61 | | - "1981-82", "1982-83", "1983-84", "1984-85", "1985-86", "1986-87", |
62 | | - "1987-88", "1988-89", "1989-90", "1990-91", "1991-92", "1992-93", |
63 | | - "1993-94", "1994-95", "1995-96", "1996-97", "1997-98", "1998-99", |
64 | | - "1999-00", "2000-01", "2001-02", "2002-03", "2003-04", "2004-05", |
65 | | - "2005-06", "2006-07", "2007-08", "2008-09", "2009-10", "2010-11", |
66 | | - "2011-12", "2012-13", "2013-14", "2014-15", "2015-16", "2016-17", |
67 | | - "2017-18", "2018-19", "2019-20", "2020-21", "2021-22", "2022-23", |
68 | | - "2023-24", "2024-25", "2025-26", "2026-27", "2027-28", "2028-29", |
69 | | - "2029-30", "2030-31", "2031-32", "2032-33", "2033-34", "2034-35", |
70 | | - "2035-36", "2036-37", "2037-38", "2038-39", "2039-40", "2040-41", |
71 | | - "2041-42", "2042-43", "2043-44", "2044-45", "2045-46", "2046-47", |
72 | | - "2047-48", "2048-49", "2049-50", "2050-51", "2051-52", "2052-53") |
73 | | - x %fin% a |
74 | | -} |
75 | | - |
76 | | -all_fy <- function(x, permitted = NULL) { |
77 | | - is.character(x) && length(x) && { |
78 | | - |
79 | | - |
80 | | - a <- if (is.null(permitted)) { |
81 | | - c("1897-98", "1898-99", "1899-00", "1900-01", "1901-02", "1902-03", |
82 | | - "1903-04", "1904-05", "1905-06", "1906-07", "1907-08", "1908-09", |
83 | | - "1909-10", "1910-11", "1911-12", "1912-13", "1913-14", "1914-15", |
84 | | - "1915-16", "1916-17", "1917-18", "1918-19", "1919-20", "1920-21", |
85 | | - "1921-22", "1922-23", "1923-24", "1924-25", "1925-26", "1926-27", |
86 | | - "1927-28", "1928-29", "1929-30", "1930-31", "1931-32", "1932-33", |
87 | | - "1933-34", "1934-35", "1935-36", "1936-37", "1937-38", "1938-39", |
88 | | - "1939-40", "1940-41", "1941-42", "1942-43", "1943-44", "1944-45", |
89 | | - "1945-46", "1946-47", "1947-48", "1948-49", "1949-50", "1950-51", |
90 | | - "1951-52", "1952-53", "1953-54", "1954-55", "1955-56", "1956-57", |
91 | | - "1957-58", "1958-59", "1959-60", "1960-61", "1961-62", "1962-63", |
92 | | - "1963-64", "1964-65", "1965-66", "1966-67", "1967-68", "1968-69", |
93 | | - "1969-70", "1970-71", "1971-72", "1972-73", "1973-74", "1974-75", |
94 | | - "1975-76", "1976-77", "1977-78", "1978-79", "1979-80", "1980-81", |
95 | | - "1981-82", "1982-83", "1983-84", "1984-85", "1985-86", "1986-87", |
96 | | - "1987-88", "1988-89", "1989-90", "1990-91", "1991-92", "1992-93", |
97 | | - "1993-94", "1994-95", "1995-96", "1996-97", "1997-98", "1998-99", |
98 | | - "1999-00", "2000-01", "2001-02", "2002-03", "2003-04", "2004-05", |
99 | | - "2005-06", "2006-07", "2007-08", "2008-09", "2009-10", "2010-11", |
100 | | - "2011-12", "2012-13", "2013-14", "2014-15", "2015-16", "2016-17", |
101 | | - "2017-18", "2018-19", "2019-20", "2020-21", "2021-22", "2022-23", |
102 | | - "2023-24", "2024-25", "2025-26", "2026-27", "2027-28", "2028-29", |
103 | | - "2029-30", "2030-31", "2031-32", "2032-33", "2033-34", "2034-35", |
104 | | - "2035-36", "2036-37", "2037-38", "2038-39", "2039-40", "2040-41", |
105 | | - "2041-42", "2042-43", "2043-44", "2044-45", "2045-46", "2046-47", |
106 | | - "2047-48", "2048-49", "2049-50", "2050-51", "2051-52", "2052-53") |
107 | | - } else { |
108 | | - permitted |
109 | | - } |
110 | | - if (anyNA(fmatch(x, a))) { |
111 | | - a <- NULL |
112 | | - FALSE |
113 | | - } else { |
114 | | - a <- NULL |
115 | | - TRUE |
116 | | - } |
117 | | - } |
118 | | -} |
119 | | - |
120 | | -range_fy2yr <- function(x) { |
121 | | - if (length(x) == 1L) { |
122 | | - y <- fmatch(x, fys1901) + 1900L |
123 | | - return(rep(y, times = 2L)) |
124 | | - } |
125 | | - if (!is.null(g_min_yr <- attr(x, "grattan_min_yr")) && |
126 | | - !is.null(g_max_yr <- attr(x, "grattan_max_yr"))) { |
127 | | - return(c(g_min_yr, g_max_yr)) |
128 | | - } |
129 | | - y <- fmatch(x, fys1901) + 1900L |
130 | | - miny <- min(y, na.rm = TRUE) |
131 | | - maxy <- max(y, na.rm = TRUE) |
132 | | - setattr(x, "grattan_min_yr", miny) |
133 | | - setattr(x, "grattan_max_yr", maxy) |
134 | | - c(miny, maxy) |
135 | | -} |
136 | | - |
137 | | -min_fy2yr <- function(x) { |
138 | | - range_fy2yr(x)[1L] |
139 | | -} |
140 | | - |
141 | | -max_fy2yr <- function(x) { |
142 | | - range_fy2yr(x)[2L] |
143 | | -} |
144 | | - |
145 | | -fy.year <- function(yr_ending){ |
| 41 | +fy.year <- function(yr_ending) { |
146 | 42 | paste0(as.integer(yr_ending) - 1, "-", substr(yr_ending, 3, 4)) |
147 | 43 | } |
148 | 44 |
|
149 | | -yr2fy <- function(yr_ending, assume1901_2100 = .getOption("grattan.assume1901_2100", TRUE)) { |
150 | | - if (assume1901_2100 || |
151 | | - AND(min(yr_ending) > 1900L, |
152 | | - max(yr_ending) < 2100L)) { |
153 | | - fys1901[yr_ending - 1900L] |
154 | | - } else { |
155 | | - .yr2fy(yr_ending) |
156 | | - } |
157 | | -} |
158 | | - |
159 | | -.yr2fy <- function(yr_ending) { |
160 | | - if (length(yr_ending) > 10e3L) { |
161 | | - # Apparently quicker for > 1000 |
162 | | - accel_repetitive_input(yr_ending, .yr2fy) |
163 | | - } else { |
164 | | - sprintf("%d-%02d", as.integer(yr_ending) - 1L, as.integer(yr_ending) %% 100L) |
165 | | - } |
166 | | -} |
167 | | - |
168 | | -fy2yr <- function(fy.yr){ |
169 | | - if (!all(is.fy(fy.yr))){ |
170 | | - stop("fy.yr contains non-FYs") |
171 | | - } else { |
172 | | - 1L + as.integer(gsub("^.*([12][0-9]{3}).?[0-9]{2}.*$", "\\1", fy.yr)) |
173 | | - } |
174 | | -} |
175 | | - |
176 | | - |
177 | | - |
178 | | -fy2date <- function(x){ |
179 | | - if (!all(is.fy(x))){ |
180 | | - stop("fy.yr contains non-FYs") |
181 | | - } else { |
182 | | - date <- paste0(as.numeric(gsub("^([1-9][0-9]{3}).*", "\\1", x)) + 1, "-06-30") |
183 | | - as.Date(date) |
184 | | - } |
185 | | -} |
186 | | - |
| 45 | +yr2fy <- fy::yr2fy |
| 46 | +fy2yr <- fy::fy2yr |
| 47 | +fy2date <- fy::fy2date |
| 48 | +date2fy <- fy::date2fy |
| 49 | +qtr2fy <- fy::qtr2fy |
187 | 50 |
|
188 | | -date2fy <- function(date) { |
189 | | - if_else(month(date) < 7L, |
190 | | - yr2fy(year(date)), |
191 | | - yr2fy(year(date) + 1L)) |
192 | | -} |
| 51 | +max_fy2yr <- function(x) fy2yr(max(x)) |
| 52 | +min_fy2yr <- function(x) fy2yr(min(x)) |
193 | 53 |
|
194 | | -qtr2fy <- function(yq) { |
195 | | - if (inherits(yq, "yearqtr")) { |
196 | | - yqn <- as.numeric(yq) |
197 | | - o <- |
198 | | - yr2fy(if_else(yqn %% 1 >= 0.5, |
199 | | - yqn + 1, |
200 | | - yqn)) |
201 | | - o |
202 | | - } else if (is.character(yq)) { |
203 | | - # Rely on the first element to determine the |
204 | | - # format |
205 | | - first_yq <- yq[1L] |
206 | | - if (is.na(first_yq)) { |
207 | | - yq_is_na <- is.na(yq) |
208 | | - first_yq <- first(yq[which.min(yq_is_na)]) |
209 | | - } |
210 | | - |
211 | | - y <- q <- NULL |
212 | | - cm <- CJ(y = 1901:2099, q = 1:4) |
213 | | - cm[, "YQ" := sprintf("%d%sQ%d", y, substr(first_yq, 5L, 5L), q)] |
214 | | - cm[, "fy_year" := yr2fy(y + q %in% 3:4)] |
215 | | - cmyq <- .subset2(cm, "YQ") |
216 | | - o <- .subset2(cm, "fy_year")[fmatch(yq, cmyq)] |
| 54 | +all_fy <- function(x, permitted = NULL) { |
| 55 | + if (is.null(permitted)) { |
| 56 | + all(fy::is_fy(x), na.rm = TRUE) |
217 | 57 | } else { |
218 | | - stop("Unknown class for `yq`.") |
| 58 | + !anyNA(fmatch(x, permitted)) |
219 | 59 | } |
220 | | - o |
221 | 60 | } |
222 | 61 |
|
223 | | - |
224 | | - |
225 | | - |
| 62 | +is_fy2 <- fy::is_fy |
226 | 63 |
|
227 | 64 |
|
0 commit comments