Skip to content

Commit

Permalink
Merge pull request nationalparkservice#111 from nationalparkservice/s…
Browse files Browse the repository at this point in the history
…arah-dev

Fix and test convert_datetime_format
  • Loading branch information
RobLBaker authored Jun 27, 2024
2 parents d79c2a7 + 87f5271 commit 838d490
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
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")
})

0 comments on commit 838d490

Please sign in to comment.