From 87f52711bd0f8d4e2e9559f5e509c430d1279c7d Mon Sep 17 00:00:00 2001 From: Wright Date: Thu, 27 Jun 2024 13:21:17 -0600 Subject: [PATCH] Fix and test convert_datetime_format --- R/dates_and_times.R | 6 +++--- tests/testthat/test-dates_and_times.R | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/R/dates_and_times.R b/R/dates_and_times.R index bd17182..f4b4156 100644 --- a/R/dates_and_times.R +++ b/R/dates_and_times.R @@ -64,12 +64,12 @@ convert_datetime_format <- function(eml_format_string, convert_z = FALSE) { stringr::str_replace_all("MMM", "%b") %>% stringr::str_replace_all("MM", "%m") %>% stringr::str_replace_all("DD", "%d") %>% - stringr::str_replace_all("(?% - stringr::str_replace_all("mm", "%M") %>% + stringr::str_replace_all("(?% + stringr::str_replace_all("(?% stringr::str_replace_all("(ss)|(SS)", "%S") %>% stringr::str_replace_all("(?% # Replace M with %m, but leave %M alone stringr::str_replace_all("D", "%d") %>% - stringr::str_replace_all("[+-][Hh]{1,2}:?[Hh]{0,2}(?=$)", "%z") # Replace UTC offset format string (e.g. -hh, -hhhh, -hh:hh) with %z. Note that R seems to only parse UTC offsets when in the format +/-hhhh. + stringr::str_replace_all("[+-][Hh]{1,2}:?[HhMm]{0,2}(?=$)", "%z") # Replace UTC offset format string (e.g. -hh, -hhhh, -hh:hh) with %z. Note that R seems to only parse UTC offsets when in the format +/-hhhh. #stringr::str_replace_all("T", " ") if (convert_z) { diff --git a/tests/testthat/test-dates_and_times.R b/tests/testthat/test-dates_and_times.R index 63432e5..4693fc0 100644 --- a/tests/testthat/test-dates_and_times.R +++ b/tests/testthat/test-dates_and_times.R @@ -9,6 +9,22 @@ test_that("fix_utc_offset fixes offsets", { test_that("convert_datetime_format returns datetimes in r format", { new_datetimes <- convert_datetime_format(c("MM/DD/YYYY", - "YY-MM-DD")) - expect_equal(new_datetimes, c("%m/%d/%Y", "%y-%m-%d")) + "YY-MM-DD", + "YYYY-MM-DDThh:mm:ss", + "YYYY-MM-DDThh:mm:ss-hhmm", + "YYYY-MM-DDThh:mm:ss+hhhh", + "YYYY-MM-DDThh:mm:ss-hh:mm", + "YYYY-MM-DDThh:mm+hh", + "YYYY-MM-DDThh:mm:ssZ")) + fix_z <- convert_datetime_format("YYYY-MM-DDThh:mm:ssZ", convert_z = TRUE) + + expect_equal(new_datetimes, c("%m/%d/%Y", + "%y-%m-%d", + "%Y-%m-%dT%H:%M:%S", + "%Y-%m-%dT%H:%M:%S%z", + "%Y-%m-%dT%H:%M:%S%z", + "%Y-%m-%dT%H:%M:%S%z", + "%Y-%m-%dT%H:%M%z", + "%Y-%m-%dT%H:%M:%SZ")) + expect_equal(fix_z, "%Y-%m-%dT%H:%M:%S%z") }) \ No newline at end of file