From 5d640a3333ed925e8edfff2f48afbc9859ca9981 Mon Sep 17 00:00:00 2001 From: Nicolas Casajus Date: Fri, 29 Mar 2024 10:17:53 +0100 Subject: [PATCH] fix: allow month w/ one character (month = 1) --- R/get_calendar.R | 13 ++++++++----- man/get_calendar.Rd | 8 ++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/R/get_calendar.R b/R/get_calendar.R index 150ba79..3f006c9 100644 --- a/R/get_calendar.R +++ b/R/get_calendar.R @@ -3,8 +3,8 @@ #' @param year either an `integer` or a `character` of length 1. Must have 4 #' characters (e.g. '2024' and not '24'). Default is the current year. #' -#' @param month either an `integer` or a `character` of length 1. Must have 2 -#' characters (e.g. '01' and not '1'). Default is the current month. +#' @param month either an `integer` or a `character` of length 1. Must have 1 +#' or 2 characters (e.g. '01' or '1'). Default is the current month. #' #' @return A `data.frame` with the following columns: #' - `date`: the date of the day (`YYYY-MM-DD`), @@ -27,10 +27,10 @@ #' head(get_calendar(month = 12)) #' #' ## Calendar for April (current year) ---- -#' head(get_calendar(month = "04")) +#' head(get_calendar(month = 4)) #' #' ## Calendar for January 1970 ---- -#' head(get_calendar(year = 1970, month = "01")) +#' head(get_calendar(year = 1970, month = 1)) get_calendar <- function(year = format(Sys.Date(), "%Y"), month = format(Sys.Date(), "%m")) { @@ -58,12 +58,15 @@ get_calendar <- function(year = format(Sys.Date(), "%Y"), stop("Argument 'month' must be of length 1", call. = FALSE) } + if (nchar(month) == 1) { + month <- paste0("0", month) + } + if (nchar(month) != 2) { stop("Argument 'month' must be of the form 'MM' (e.g. '01' instead of '1')", call. = FALSE) } - ## Switch to US locale ---- locale <- Sys.getlocale("LC_TIME") diff --git a/man/get_calendar.Rd b/man/get_calendar.Rd index 18c97df..80cf333 100644 --- a/man/get_calendar.Rd +++ b/man/get_calendar.Rd @@ -13,8 +13,8 @@ get_calendar( \item{year}{either an \code{integer} or a \code{character} of length 1. Must have 4 characters (e.g. '2024' and not '24'). Default is the current year.} -\item{month}{either an \code{integer} or a \code{character} of length 1. Must have 2 -characters (e.g. '01' and not '1'). Default is the current month.} +\item{month}{either an \code{integer} or a \code{character} of length 1. Must have 1 +or 2 characters (e.g. '01' or '1'). Default is the current month.} } \value{ A \code{data.frame} with the following columns: @@ -41,8 +41,8 @@ head(get_calendar()) head(get_calendar(month = 12)) ## Calendar for April (current year) ---- -head(get_calendar(month = "04")) +head(get_calendar(month = 4)) ## Calendar for January 1970 ---- -head(get_calendar(year = 1970, month = "01")) +head(get_calendar(year = 1970, month = 1)) }