Skip to content

Commit 87f5271

Browse files
committed
Fix and test convert_datetime_format
1 parent 0c79fe8 commit 87f5271

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

R/dates_and_times.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ convert_datetime_format <- function(eml_format_string, convert_z = FALSE) {
6464
stringr::str_replace_all("MMM", "%b") %>%
6565
stringr::str_replace_all("MM", "%m") %>%
6666
stringr::str_replace_all("DD", "%d") %>%
67-
stringr::str_replace_all("(?<![+-])((hh)|(HH))", "%H") %>%
68-
stringr::str_replace_all("mm", "%M") %>%
67+
stringr::str_replace_all("(?<![Hh+-])((hh)|(HH))", "%H") %>%
68+
stringr::str_replace_all("(?<!([+-]hh:{0,1}))mm", "%M") %>%
6969
stringr::str_replace_all("(ss)|(SS)", "%S") %>%
7070
stringr::str_replace_all("(?<!%)M", "%m") %>% # Replace M with %m, but leave %M alone
7171
stringr::str_replace_all("D", "%d") %>%
72-
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.
72+
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.
7373
#stringr::str_replace_all("T", " ")
7474

7575
if (convert_z) {

tests/testthat/test-dates_and_times.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ test_that("fix_utc_offset fixes offsets", {
99

1010
test_that("convert_datetime_format returns datetimes in r format", {
1111
new_datetimes <- convert_datetime_format(c("MM/DD/YYYY",
12-
"YY-MM-DD"))
13-
expect_equal(new_datetimes, c("%m/%d/%Y", "%y-%m-%d"))
12+
"YY-MM-DD",
13+
"YYYY-MM-DDThh:mm:ss",
14+
"YYYY-MM-DDThh:mm:ss-hhmm",
15+
"YYYY-MM-DDThh:mm:ss+hhhh",
16+
"YYYY-MM-DDThh:mm:ss-hh:mm",
17+
"YYYY-MM-DDThh:mm+hh",
18+
"YYYY-MM-DDThh:mm:ssZ"))
19+
fix_z <- convert_datetime_format("YYYY-MM-DDThh:mm:ssZ", convert_z = TRUE)
20+
21+
expect_equal(new_datetimes, c("%m/%d/%Y",
22+
"%y-%m-%d",
23+
"%Y-%m-%dT%H:%M:%S",
24+
"%Y-%m-%dT%H:%M:%S%z",
25+
"%Y-%m-%dT%H:%M:%S%z",
26+
"%Y-%m-%dT%H:%M:%S%z",
27+
"%Y-%m-%dT%H:%M%z",
28+
"%Y-%m-%dT%H:%M:%SZ"))
29+
expect_equal(fix_z, "%Y-%m-%dT%H:%M:%S%z")
1430
})

0 commit comments

Comments
 (0)