Skip to content

Commit

Permalink
Add locale argument to date/time formatters (#330)
Browse files Browse the repository at this point in the history
Fixes #309
  • Loading branch information
hadley authored Mar 24, 2022
1 parent 795b966 commit ed3dee0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Suggests:
dichromat,
ggplot2,
hms (>= 0.5.0),
stringi,
testthat (>= 3.0.0)
Config/Needs/website: tidyverse/tidytemplate
Encoding: UTF-8
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# scales (development version)

* `label_date()` and `label_time()` gain a `locale` argument that allows you
to set the locale used to generate day and month names (#309).

* `hue_pal()` respects `h.start` once again (#288).

* `col_quantile()` no longer errors if data is sufficiently skewed that we
Expand Down
29 changes: 23 additions & 6 deletions R/label-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#' For `date_short()` a character vector of length 4 giving the format
#' components to use for year, month, day, and hour respectively.
#' @param tz a time zone name, see [timezones()]. Defaults
#' to UTC
#' to UTC
#' @param locale Locale to use when for day and month names. The default
#' uses the current locale. Setting this argument requires stringi, and you
#' can see a complete list of supported locales with
#' [stringi::stri_locale_list()].
#' @export
#' @examples
#' date_range <- function(start, days) {
Expand All @@ -26,6 +30,8 @@
#' two_months <- date_range("2020-05-01", 60)
#' demo_datetime(two_months)
#' demo_datetime(two_months, labels = date_format("%m/%d"))
#' demo_datetime(two_months, labels = date_format("%e %b", locale = "fr"))
#' demo_datetime(two_months, labels = date_format("%e %B", locale = "es"))
#' # ggplot2 provides a short-hand:
#' demo_datetime(two_months, date_labels = "%m/%d")
#'
Expand All @@ -35,9 +41,11 @@
#' one_year <- date_range("2020-05-01", 365)
#' demo_datetime(one_year, date_breaks = "month")
#' demo_datetime(one_year, date_breaks = "month", labels = label_date_short())
label_date <- function(format = "%Y-%m-%d", tz = "UTC") {
force_all(format, tz)
function(x) format(x, format, tz = tz) # format handles NAs correctly when dealing with dates
label_date <- function(format = "%Y-%m-%d", tz = "UTC", locale = NULL) {
force_all(format, tz, locale)
function(x) {
format_dt(x, format = format, tz = tz, locale = locale)
}
}

#' @export
Expand Down Expand Up @@ -93,11 +101,11 @@ append_if <- function(x, cond, value) {

#' @export
#' @rdname label_date
label_time <- function(format = "%H:%M:%S", tz = "UTC") {
label_time <- function(format = "%H:%M:%S", tz = "UTC", locale = NULL) {
force_all(format, tz)
function(x) {
if (inherits(x, "POSIXt")) {
format(x, format = format, tz = tz) # format handles NAs correctly for times
format_dt(x, format = format, tz = tz, locale = locale)
} else if (inherits(x, "difftime")) {
format(as.POSIXct(x), format = format, tz = tz)
} else {
Expand All @@ -110,6 +118,15 @@ label_time <- function(format = "%H:%M:%S", tz = "UTC") {
}
}

format_dt <- function(x, format, tz = "UTC", locale = NULL) {
if (is.null(locale)) {
format(x, format = format, tz = tz)
} else {
check_installed("stringi")
format <- stringi::stri_datetime_fstr(format)
stringi::stri_datetime_format(x, format, tz = tz, locale = locale)
}
}

#' Superseded interface to `label_date()`/`label_time()`
#'
Expand Down
9 changes: 7 additions & 2 deletions man/date_format.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions man/label_date.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test-label-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ test_that("time_format works correctly", {
expect_equal(time_format()(na_time), NA_character_)
})

test_that("can set locale", {
x <- ISOdate(2012, 1, 1, 11, tz = "UTC")
expect_equal(date_format("%B", locale = "fr")(x), "janvier")
expect_equal(time_format("%B", locale = "fr")(x), "janvier")
})

test_that("date_short doesn't change unexpectedly", {
expect_snapshot({
dformat <- label_date_short()
Expand Down

0 comments on commit ed3dee0

Please sign in to comment.