Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and test convert_datetime_format #111

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions R/dates_and_times.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("(?<![+-])((hh)|(HH))", "%H") %>%
stringr::str_replace_all("mm", "%M") %>%
stringr::str_replace_all("(?<![Hh+-])((hh)|(HH))", "%H") %>%
stringr::str_replace_all("(?<!([+-]hh:{0,1}))mm", "%M") %>%
stringr::str_replace_all("(ss)|(SS)", "%S") %>%
stringr::str_replace_all("(?<!%)M", "%m") %>% # 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) {
Expand Down
20 changes: 18 additions & 2 deletions tests/testthat/test-dates_and_times.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Loading