From 1b79cba6f222a22cf86b9ab8b996e33cefbe9778 Mon Sep 17 00:00:00 2001 From: Albert Cheng Date: Mon, 16 Jan 2017 11:11:03 +0800 Subject: [PATCH] [Fix #401 ext] Return localized labels in month --- R/accessors-month.r | 20 ++++++++------------ man/month.Rd | 4 +++- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/R/accessors-month.r b/R/accessors-month.r index 1a746784..37081028 100644 --- a/R/accessors-month.r +++ b/R/accessors-month.r @@ -13,6 +13,7 @@ NULL #' label, such as "January". TRUE will display an abbreviated version of the #' label, such as "Jan". abbr is disregarded if label = FALSE. #' @param value a numeric object +#' @param locale for month, locale to use for month names. Default to current locale. #' @return the months element of x as a number (1-12) or character string. 1 = #' January. #' @keywords utilities manip chron methods @@ -28,25 +29,19 @@ NULL #' month(ymd(080101), label = TRUE, abbr = FALSE) #' month(ymd(080101) + months(0:11), label = TRUE) #' @export -month <- function(x, label = FALSE, abbr = TRUE) +month <- function(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) UseMethod("month") #' @export -month.default <- function(x, label = FALSE, abbr = TRUE) - month(as.POSIXlt(x, tz = tz(x))$mon + 1, label, abbr) +month.default <- function(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) + month(as.POSIXlt(x, tz = tz(x))$mon + 1, label, abbr, locale = locale) #' @export -month.numeric <- function(x, label = FALSE, abbr = TRUE) { +month.numeric <- function(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) { if (!label) return(x) - if (abbr) { - labels <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", - "Oct", "Nov", "Dec") - } else { - labels <- c("January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", - "December") - } + names <- .get_locale_regs(locale)$month_names + labels <- if (abbr) names$abr else names$full ordered(x, levels = 1:12, labels = labels) } @@ -58,6 +53,7 @@ month.Period <- function(x, label = FALSE, abbr = TRUE) #' @rdname month #' @export "month<-" <- function(x, value) { + ## FIXME: how to make this localized and preserve backward compatibility? Guesser? if (!is.numeric(value)) { value <- pmatch(tolower(value), c("january", "february", "march", "june", "july", "august", "september", "october", "november", "december")) diff --git a/man/month.Rd b/man/month.Rd index 90b4a35a..0331258b 100644 --- a/man/month.Rd +++ b/man/month.Rd @@ -5,7 +5,7 @@ \alias{month<-} \title{Get/set months component of a date-time.} \usage{ -month(x, label = FALSE, abbr = TRUE) +month(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) month(x) <- value } @@ -19,6 +19,8 @@ as "January." FALSE will display the month as a number.} label, such as "January". TRUE will display an abbreviated version of the label, such as "Jan". abbr is disregarded if label = FALSE.} +\item{locale}{for month, locale to use for month names. Default to current locale.} + \item{value}{a numeric object} } \value{