rstudio / bigD

Flexibly format dates and times to a given locale
https://rstudio.github.io/bigD/
Other
17 stars 2 forks source link

Overflow error with locale #11

Open olivroy opened 2 weeks ago

olivroy commented 2 weeks ago

I did a bit more digging to find out that https://github.com/rstudio/gt/issues/1481 is actually a bigD issue. Maybe related to https://bugs.r-project.org/show_bug.cgi?id=18618

Here is a reprex

library(bigD)
withr::with_locale(
  c("LC_TIME" = "en_CA"),
  fdt(
    input = "2021-01-09 16:32(America/Toronto)",
    format = flex_t12_lst$hms
  )
)
#> [1] "4:32:00 PM"
withr::with_locale(
  c("LC_TIME" = "fr_CA"),
  fdt(
    input = "2021-01-09 16:32(America/Toronto)",
    format = flex_t12_lst$hms
  )
)
#> Error in format.POSIXlt(as.POSIXlt(x, tz), format, usetz, ...): output string exceeded 2048 bytes
# respects locale supplied, so maybe as a workaround,
# gt could set it?
withr::with_locale(
  c("LC_TIME" = "en_CA"),
  fdt(
    input = "2021-01-09 16:32(America/Toronto)",
    format = flex_t12_lst$hms,
    locale = "fr_CA"
  )
)
#> [1] "4 h 32 min 00 s p.m."

Created on 2024-07-04 with reprex v2.1.0

Possibly gt should set the option before attempting to do it.

test case can be

# compare output of this test vs the other one.
test_that("fdt() works with non-standard locale when LC_TIME = 'en_CA' (#11).", {
  withr::local_locale("LC_TIME" = "en_CA")
  expect_no_error(
    res0 <- fdt(
      input = "2021-01-09 16:32(America/Toronto)",
      format = flex_t12_lst$hms,
      locale = "en"
    )
  )
  expect_no_error(
    res1 <- fdt(
      input = "2021-01-09 16:32(America/Toronto)",
      format = flex_t12_lst$hms,
      locale = "en"
    )
  )
})

test_that("fdt() works with non-standard locale when LC_TIME = 'fr_CA' (#11).", {
  withr::local_locale("LC_TIME" = "fr_CA")

  expect_no_error(
    res0 <- fdt(
      input = "2021-01-09 16:32(America/Toronto)",
      format = flex_t12_lst$hms,
    )
  )
  expect_no_error(
    res1 <- fdt(
      input = "2021-01-09 16:32(America/Toronto)",
      format = flex_t12_lst$hms,
      locale = "en"
    )
  )
})
rich-iannone commented 1 week ago

@olivroy I just re-cloned the package, built from main, ran the code examples and didn't experience any errors. Perhaps this issue was fixed since the CRAN release (could be there is now a default locale set by bigD). Could you install the dev version and try again?

olivroy commented 1 week ago

I tried the dev version. This seems to be a Windows only problem?

olivroy commented 1 week ago

Ended up just putting this in my .Renviron, and it solves my issue! No need to bother if no one else complains

LC_TIME = "en-CA"