Skip to content

Commit

Permalink
[Fix #887] Error on invalid numeric input to month()
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Apr 30, 2020
1 parent dc104b6 commit 133b6f5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Version 1.7.8.9000 (dev)
### BUG FIXES

* [#886](https://github.com/tidyverse/lubridate/issues/886) Fix `with_tz()` for POSIXlt objects

* [#887](https://github.com/tidyverse/lubridate/issues/887) Error on invalid numeric input to `month()`
Version 1.7.8
=============

Expand Down
3 changes: 2 additions & 1 deletion R/accessors-day.r
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ wday.numeric <- function(x, label = FALSE, abbr = TRUE,

start <- as.integer(week_start)

if (start > 7 || start < 1) stop("Invalid 'week_start' argument; must be between 1 and 7")
if (start > 7 || start < 1)
stop("Invalid 'week_start' argument; must be between 1 and 7")

if (start != 7) {
x <- 1 + (x + (6 - start)) %% 7
Expand Down
7 changes: 6 additions & 1 deletion R/accessors-month.r
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ month.default <- function(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale(

#' @export
month.numeric <- function(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) {
if (!label) return(x)
if (!all(x[!is.na(x)] %in% 1:12))
stop("Values are not in 1:12")

if (!label) {
return(x)
}

names <- .get_locale_regs(locale)$month_names
labels <- if (abbr) names$abr else names$full
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-accessors.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ test_that("months accessor extracts correct month", {

})

test_that("month() on numeric input doesn't accept invalid values", {
expect_error(month(0:13))
expect_error(month(c(0:13, NA)))
expect_error(month(0))
expect_error(month(0), label = TRUE)
expect_equal(month(1:12), 1:12)
expect_equal(month(c(1:12, NA)), c(1:12, NA))
expect_equal(month(as.double(1:12)), 1:12)
})

test_that("quarters accessor extracts correct quarter", {
posct <- ymd_hms("2010-11-03 13:45:59")
poslt <- as.POSIXlt(posct)
Expand Down

0 comments on commit 133b6f5

Please sign in to comment.