Skip to content

Commit

Permalink
[Fix #1003] Correctly handle r and R formats in localse which have no…
Browse files Browse the repository at this point in the history
… p format
  • Loading branch information
vspinu committed Oct 25, 2022
1 parent fcea952 commit afb6e47
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Version 1.8.0.9000

### BUG FIXES

* [#1003](https://github.com/tidyverse/lubridate/issues/1003) Correctly handle r and R formats in locales which have no p format
* [#1074](https://github.com/tidyverse/lubridate/issues/1074) Fix concatination of named Period, Interval and Duration vectors.
* [#1044](https://github.com/tidyverse/lubridate/issues/1044) POSIXlt results returned by `fast_strptime()` and `parse_date_time2()` now have a recycled `isdst` field.
* [#1069](https://github.com/tidyverse/lubridate/issues/1069) Internal code handling the addition of period months and years no longer generates partially recycled POSIXlt objects.
Expand Down
10 changes: 5 additions & 5 deletions R/guess.r
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ guess_formats <- function(x, orders, locale = Sys.getlocale("LC_TIME"),
}

## We split into characterst first and then paste together formats that start
## with O. If perl style lookahead would have worked we wouldn't need this,
## but it doesn't.
## with O.
osplits <- strsplit(orders, "", fixed = TRUE)
osplits <- lapply(
osplits,
Expand Down Expand Up @@ -382,8 +381,9 @@ guess_formats <- function(x, orders, locale = Sys.getlocale("LC_TIME"),

p <- unique(mat[, "p"])
p <- p[nzchar(p)]
no_p <- length(p) == 0L
alpha["p"] <-
if (length(p) == 0L) {
if (no_p) {
""
} else {
sprintf("(?<p>%s)(?![[:alpha:]])", paste(p, collapse = "|"))
Expand Down Expand Up @@ -423,8 +423,8 @@ guess_formats <- function(x, orders, locale = Sys.getlocale("LC_TIME"),
if (length(p) == 0L) {
num <- c(num,
T = sprintf("(%s\\D+%s\\D+%s)", num[["H"]], num[["M"]], num[["S"]]),
R = sprintf("(%s\\D+%s)", num[["H"]], num[["M"]]),
r = sprintf("(%s\\D+)", num[["H"]])
R = sprintf(if (no_p) "(%s%s)" else "(%s\\D+%s)", num[["H"]], num[["M"]]),
r = sprintf(if (no_p) "(%s)" else "(%s\\D+)", num[["H"]])
)
} else {
num <- c(num,
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-parsers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,16 @@ test_that("parsing with j format works correctly", {
expect_equal(yday(parse_date_time(c(1, 150, 330), "j")), c(1, 150, 330))
})

test_that("parsing with r and R formats works in non-english locale", {
skip_on_cran()
suppressWarnings(testthat::skip_if(Sys.setlocale("LC_TIME", "fr_FR.utf8") == ""))
on.exit(Sys.setlocale("LC_TIME", "C"))
expect_equal(ymd_h("2021-10-26 0"), ymd("2021-10-26", tz = "UTC"))
expect_equal(parse_date_time("2021-10-26 0", "Ymdr"), ymd("2021-10-26", tz = "UTC"))
expect_equal(parse_date_time("2021-10-26 0001", "YmdR"), ymd_hms("2021-10-26 0:1:0"))
})


## library(microbenchmark)
## library(lubridate)

Expand Down

0 comments on commit afb6e47

Please sign in to comment.