From 530d13875f811a81110c88049f4ab27e5d3df460 Mon Sep 17 00:00:00 2001 From: Nicolas Casajus Date: Sun, 31 Mar 2024 09:29:26 +0200 Subject: [PATCH] feat: get month & weekday names utilities --- NAMESPACE | 2 ++ R/get_month_name.R | 68 +++++++++++++++++++++++++++++++++++++ R/get_weekday_name.R | 74 +++++++++++++++++++++++++++++++++++++++++ man/get_month_name.Rd | 27 +++++++++++++++ man/get_weekday_name.Rd | 32 ++++++++++++++++++ 5 files changed, 203 insertions(+) create mode 100644 R/get_month_name.R create mode 100644 R/get_weekday_name.R create mode 100644 man/get_month_name.Rd create mode 100644 man/get_weekday_name.Rd diff --git a/NAMESPACE b/NAMESPACE index ee06bbb..55da3e6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,8 @@ export(filter_events) export(get_calendar) +export(get_month_name) +export(get_weekday_name) export(number_of_days) export(plot_calendar) importFrom(grDevices,cairo_pdf) diff --git a/R/get_month_name.R b/R/get_month_name.R new file mode 100644 index 0000000..9ddc88e --- /dev/null +++ b/R/get_month_name.R @@ -0,0 +1,68 @@ +#' Get the name of a month +#' +#' @param month an `integer` of length 1. The month to convert in letters. +#' +#' @param lang a `character` of length 1. Used to change the default locale +#' (i.e. the language). Default is `NULL` (i.e. use the current locale). +#' See examples below. Depending on the OS and the locale, the output can be +#' weird. +#' +#' @return A `character` of length 1. +#' +#' @export +#' +#' @examples +#' get_month_name(month = 4) +#' get_month_name(month = 4, lang = "spanish") +#' get_month_name(month = 4, lang = "finnish") + +get_month_name <- function(month, lang = NULL) { + + if (missing(month)) { + stop("Argument 'month' is required", call. = FALSE) + } + + if (!is.numeric(month)) { + stop("Argument 'month' must be a numeric", call. = FALSE) + } + + if (length(month) != 1) { + stop("Argument 'month' must be a numeric of length 1", call. = FALSE) + } + + if (!(month %in% 1:12)) { + stop("Argument 'month' must be between 1 and 12", call. = FALSE) + } + + + ## Switch locale ---- + + if (!is.null(lang)) { + + o_warn <- options()$"warn" + + lc_time <- Sys.getlocale("LC_TIME") + lc_ctype <- Sys.getlocale("LC_CTYPE") + lc_collate <- Sys.getlocale("LC_COLLATE") + + on.exit(options("warn" = o_warn), add = TRUE) + on.exit(Sys.setlocale("LC_TIME", lc_time), add = TRUE) + on.exit(Sys.setlocale("LC_CTYPE", lc_ctype), add = TRUE) + on.exit(Sys.setlocale("LC_COLLATE", lc_collate), add = TRUE) + + options("warn" = -1) + Sys.setlocale("LC_TIME", lang) + Sys.setlocale("LC_CTYPE", lang) + Sys.setlocale("LC_COLLATE", lang) + } + + + ## Create date ---- + + day <- paste(1970, month, 1, sep = "-") + + + ## Get month name ---- + + tools::toTitleCase(format(as.Date(day), "%B")) +} diff --git a/R/get_weekday_name.R b/R/get_weekday_name.R new file mode 100644 index 0000000..a27d04b --- /dev/null +++ b/R/get_weekday_name.R @@ -0,0 +1,74 @@ +#' Get the weekday name of a date +#' +#' @param date either a `character` or a `Date` of length 1. The date to extract +#' the weekday name. See examples below. +#' +#' @param format a `character` of length 1. Used to specify the format of the +#' date. Default is `"%Y-%m-%d"` (i.e. 2024-12-25). See examples below. +#' +#' @param lang a `character` of length 1. Used to change the default locale +#' (i.e. the language). Default is `NULL` (i.e. use the current locale). +#' See examples below. Depending on the OS and the locale, the output can be +#' weird. +#' +#' @return A `character` of length 1. +#' +#' @export +#' +#' @examples +#' get_weekday_name("2024-04-01") +#' get_weekday_name("01/04/2024", format = "%d/%m/%Y") +#' get_weekday_name("2024-04-01", lang = "spanish") +#' get_weekday_name("2024-04-01", lang = "finnish") + +get_weekday_name <- function(date, format = "%Y-%m-%d", lang = NULL) { + + if (missing(date)) { + stop("Argument 'date' is required", call. = FALSE) + } + + if (!is.character(date) && !inherits(date, "Date")) { + stop("Argument 'date' must be either a character or a Date", call. = FALSE) + } + + if (length(date) != 1) { + stop("Argument 'date' must be of length 1", call. = FALSE) + } + + if (!inherits(date, "Date")) { + date <- as.Date(date, format = format) + } + + if (any(is.na(date))) { + stop("Error in converting date. Please use the argument 'format' to ", + "specify the appropriate format. See '?strptime' for further ", + "information", call. = FALSE) + } + + + ## Switch locale ---- + + if (!is.null(lang)) { + + o_warn <- options()$"warn" + + lc_time <- Sys.getlocale("LC_TIME") + lc_ctype <- Sys.getlocale("LC_CTYPE") + lc_collate <- Sys.getlocale("LC_COLLATE") + + on.exit(options("warn" = o_warn), add = TRUE) + on.exit(Sys.setlocale("LC_TIME", lc_time), add = TRUE) + on.exit(Sys.setlocale("LC_CTYPE", lc_ctype), add = TRUE) + on.exit(Sys.setlocale("LC_COLLATE", lc_collate), add = TRUE) + + options("warn" = -1) + Sys.setlocale("LC_TIME", lang) + Sys.setlocale("LC_CTYPE", lang) + Sys.setlocale("LC_COLLATE", lang) + } + + + ## Get weekday name ---- + + tools::toTitleCase(format(as.Date(date), "%A")) +} diff --git a/man/get_month_name.Rd b/man/get_month_name.Rd new file mode 100644 index 0000000..2f1e98e --- /dev/null +++ b/man/get_month_name.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_month_name.R +\name{get_month_name} +\alias{get_month_name} +\title{Get the name of a month} +\usage{ +get_month_name(month, lang = NULL) +} +\arguments{ +\item{month}{an \code{integer} of length 1. The month to convert in letters.} + +\item{lang}{a \code{character} of length 1. Used to change the default locale +(i.e. the language). Default is \code{NULL} (i.e. use the current locale). +See examples below. Depending on the OS and the locale, the output can be +weird.} +} +\value{ +A \code{character} of length 1. +} +\description{ +Get the name of a month +} +\examples{ +get_month_name(month = 4) +get_month_name(month = 4, lang = "spanish") +get_month_name(month = 4, lang = "finnish") +} diff --git a/man/get_weekday_name.Rd b/man/get_weekday_name.Rd new file mode 100644 index 0000000..e5b1bc4 --- /dev/null +++ b/man/get_weekday_name.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_weekday_name.R +\name{get_weekday_name} +\alias{get_weekday_name} +\title{Get the weekday name of a date} +\usage{ +get_weekday_name(date, format = "\%Y-\%m-\%d", lang = NULL) +} +\arguments{ +\item{date}{either a \code{character} or a \code{Date} of length 1. The date to extract +the weekday name. See examples below.} + +\item{format}{a \code{character} of length 1. Used to specify the format of the +date. Default is \code{"\%Y-\%m-\%d"} (i.e. 2024-12-25). See examples below.} + +\item{lang}{a \code{character} of length 1. Used to change the default locale +(i.e. the language). Default is \code{NULL} (i.e. use the current locale). +See examples below. Depending on the OS and the locale, the output can be +weird.} +} +\value{ +A \code{character} of length 1. +} +\description{ +Get the weekday name of a date +} +\examples{ +get_weekday_name("2024-04-01") +get_weekday_name("01/04/2024", format = "\%d/\%m/\%Y") +get_weekday_name("2024-04-01", lang = "spanish") +get_weekday_name("2024-04-01", lang = "finnish") +}