Skip to content

Commit

Permalink
[Fix #401 ext] Return localized labels in month
Browse files Browse the repository at this point in the history
  • Loading branch information
alberthkcheng committed Jan 16, 2017
1 parent ac50217 commit 620dd5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
22 changes: 9 additions & 13 deletions R/accessors-month.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,36 +29,31 @@ 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)
}

#' @export
month.Period <- function(x, label = FALSE, abbr = TRUE)
month.Period <- function(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME"))
slot(x, "month")

#' @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"))
Expand Down
4 changes: 3 additions & 1 deletion man/month.Rd

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

0 comments on commit 620dd5b

Please sign in to comment.