Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better way to format dates? #12

Open
christophsax opened this issue Apr 7, 2021 · 2 comments
Open

A better way to format dates? #12

christophsax opened this issue Apr 7, 2021 · 2 comments

Comments

@christophsax
Copy link
Member

We are looking for a language dependent date formatting that does not depend on the system. Current cd_format_date.R works differently on every system. Can we find a solution that does not depend on the system?

@krlmlr
@valeguggia

@christophsax
Copy link
Member Author

@christophsax
Copy link
Member Author

This is what we did in admindown:

#' Language Specific Date Formatting
#'
#' Changes the format of the date
#' @param date Date Creation date read from the metadata, default is the system date
#' @param lang Character. Language read from the metadata
#'
#' @export
#' @examples
#' cd_format_date(Sys.Date())
cd_format_date <- function(date = default(rmarkdown::metadata$date, Sys.Date()),
                           lang = default(rmarkdown::metadata$lang, "de-DE")) {
  lang <- clean_lang(lang)
  date <- as.Date(date)

  month_names <-
    list(
      en = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"),
      de = c("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"),
      it = c("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"),
      fr = c("janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre")
    )

  skeleton <-
    c(
      en = "{month_name} {day}, {year}",
      de = "{day}. {month_name} {year}",
      it = "{day} {month_name} {year}",
      fr = "{day} {month_name} {year}"
    )

  day <- as.POSIXlt(date)$mday
  month_name <- month_names[[lang]][as.POSIXlt(date)$mon + 1L]
  year <- as.POSIXlt(date)$year + 1900L

  glue::glue(skeleton[lang])
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant