Skip to content

Commit

Permalink
Ensure empty constructors return 0-length vectors
Browse files Browse the repository at this point in the history
Fixes #707
  • Loading branch information
hadley authored and vspinu committed Dec 1, 2019
1 parent 5ef92c9 commit a45dcdc
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Version 1.7.4.9000
### NEW FEATURES

* [#713](https://github.com/tidyverse/lubridate/issues/713) `as_datetime()` always returns a `POSIXct()`
* [#707](https://github.com/tidyverse/lubridate/issues/707) Constructors return 0-length inputs when called with no arguments
* [#672](https://github.com/tidyverse/lubridate/issues/672) Eliminate all partial argument matches
* [#695](https://github.com/tidyverse/lubridate/issues/695) Durations can now be compared with numeric vectors.
* [#681](https://github.com/tidyverse/lubridate/issues/681) New constants `NA_Date_` and `NA_POSIXct_` which parallel built-in primitive constants.
Expand Down
4 changes: 3 additions & 1 deletion R/durations.r
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ setMethod("[[<-", signature(x = "Duration"),
#' @export
duration <- function(num = NULL, units = "seconds", ...) {
nums <- list(...)
if (is.character(num)) {
if (is.null(num) && missing(...)) {
.duration_from_num(numeric(), units)
} else if (is.character(num)) {
as.duration(parse_period(num))
} else if (!is.null(num) && length(nums) > 0) {
c(.duration_from_num(num, units), .duration_from_units(nums))
Expand Down
6 changes: 5 additions & 1 deletion R/intervals.r
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,18 @@ unique.Interval <- function(x, ...) {
#'
#' is.interval(period(months= 1, days = 15)) # FALSE
#' is.interval(interval(ymd(20090801), ymd(20090809))) # TRUE
interval <- function(start, end = NULL, tzone = tz(start)) {
interval <- function(start = NULL, end = NULL, tzone = tz(start)) {

if (is.null(tzone)) {
tzone <- tz(end)
if (is.null(tzone))
tzone <- "UTC"
}

if (is.null(start) && is.null(end)) {
return(new("Interval", numeric(), start = POSIXct(), tzone = "UTC"))
}

if (is.character(start) && is.null(end)) {
return(parse_interval(start, tzone))
}
Expand Down
7 changes: 6 additions & 1 deletion R/periods.r
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ setMethod("$<-", signature(x = "Period"), function(x, name, value) {
#' @export
period <- function(num = NULL, units = "second", ...) {
nums <- list(...)
if (is.character(num)) {
if (is.null(num) && missing(...)) {
new("Period", numeric(),
year = numeric(), month = numeric(), day = numeric(),
hour = numeric(), minute = numeric()
)
} else if (is.character(num)) {
parse_period(num)
} else if (!is.null(num) && length(nums) > 0) {
c(.period_from_num(num, units), .period_from_units(nums))
Expand Down
2 changes: 1 addition & 1 deletion man/interval.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-durations.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
context("Durations")

test_that("duration() returns zero-length vector", {
x <- duration()
expect_s4_class(x, "Duration")
expect_length(x, 0)
})

test_that("duration constructor doesn't accept non-numeric or non-character inputs", {
expect_error(duration(interval(ymd("2014-01-01"), ymd("2015-01-01"))))
})
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-intervals.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
context("Intervals")

test_that("interval() returns zero-length vector", {
x <- interval()
expect_s4_class(x, "Interval")
expect_length(x, 0)
})

test_that("is.interval works as expected", {
expect_false(is.interval(234))
expect_false(is.interval(as.POSIXct("2008-08-03 13:01:59", tz = "UTC")))
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-periods.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
context("Periods")

test_that("period() returns zero-length vector", {
x <- period()
expect_s4_class(x, "Period")
expect_length(x, 0)
})

test_that("period constructor doesn't accept non-numeric or non-character inputs", {
expect_error(period(interval(ymd("2014-01-01"), ymd("2015-01-01"))))
})
Expand Down

0 comments on commit a45dcdc

Please sign in to comment.