Skip to content

Commit 597721d

Browse files
Add ability to parse weeks
1 parent df41ee5 commit 597721d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

R/dst_date_parse.R

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#' Helper function to parse the dates from the statbank.
22
#'
3+
#' @description
4+
#' The weeks are returned to be the first day of the week. It does happen that
5+
#' the first day of week 1 is a day in december of the year before. This is
6+
#' intended and a weird artefact of the system we have.
7+
#'
8+
#'
39
#' @param dst_date A vector of length one or more with date formats like
4-
#' 1982M12D09, 1982M12, 1982Q4 or 1982
10+
#' 1982M12D09, 2004U27, 1982M12, 1982Q4 or 1982
511
#' @returns Returns the input date formatted to be Europe/Copenhagen
612
#' @noRd
713
dst_date_parse <- function(dst_date) {
@@ -22,6 +28,29 @@ dst_date_parse <- function(dst_date) {
2228
),
2329
tz = tz
2430
)
31+
} else if (
32+
# nolint start
33+
all(stringr::str_detect(dst_date, "^[0-9]{4}U(0[1-9]|[1-4][0-9]|5[0-2])$")) &&
34+
all(stringr::str_length(string = dst_date) == 7)
35+
# nolint end
36+
) {
37+
# Weekly format
38+
# Find year and and week
39+
year <- lubridate::ymd(paste0(
40+
stringr::str_sub(dst_date, start = 1L, end = 4L),
41+
"-01-01"))
42+
43+
week <- stringr::str_sub(dst_date, start = -2L)
44+
45+
# Get the first day of the week that is 7 (days) times weeks minus one to
46+
# account for weird date-numbers. Also ensure the week starts on a monday
47+
dst_date <- lubridate::floor_date(
48+
year + lubridate::days(7 * (as.numeric(week) - 1)),
49+
unit = "week",
50+
week_start = 1
51+
)
52+
53+
dst_date <- lubridate::ymd(dst_date, tz = tz)
2554
} else if (
2655
# nolint start
2756
all(stringr::str_detect(

tests/testthat/test-dst_date_parse.R

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ test_that("dst_date_parse gives the correct class.", {
66
expect_equal(class(dst_date_parse(dst_date = "2000M01D01")), exp_dates)
77
expect_equal(class(dst_date_parse(dst_date = c("2000M01D20", "2000M02D21", "2000M03D23", "2000M04D24"))), exp_dates)
88

9+
# Weekly
10+
expect_equal(class(dst_date_parse(dst_date = "2000U01")), exp_dates)
11+
expect_equal(class(dst_date_parse(dst_date = c("2000U01", "2000U03", "2023U47", "2005U11"))), exp_dates)
12+
913
# Monthly
1014
expect_equal(class(dst_date_parse(dst_date = "2000M01")), exp_dates)
1115
expect_equal(class(dst_date_parse(dst_date = c("2000M01", "2000M02", "2000M03", "2000M04", "2000M10", "2000M11"))), exp_dates)
@@ -33,6 +37,10 @@ test_that("Test that dst_date_parse stops when the input is bad.", {
3337
expect_error(dst_date_parse(dst_date = "2000M01D35"))
3438
expect_error(dst_date_parse(dst_date = "2000M10D40"))
3539

40+
# Daily
41+
expect_error(dst_date_parse(dst_date = "2000U57"))
42+
expect_error(dst_date_parse(dst_date = "20000U40"))
43+
3644
# Monthly
3745
expect_error(dst_date_parse(dst_date = "20000M01"))
3846
expect_error(dst_date_parse(dst_date = "2000M101"))

0 commit comments

Comments
 (0)