Skip to content

Commit

Permalink
[Fix #323] Implement coercion methods for Duration class
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed May 13, 2015
1 parent 8bb248e commit e9f1c4c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Collate:
'POSIXt.r'
'util.r'
'timespans.r'
'intervals.r'
'difftimes.r'
'durations.r'
'periods.r'
'accessors-day.r'
Expand All @@ -51,8 +53,6 @@ Collate:
'am-pm.r'
'time-zones.r'
'numeric.r'
'intervals.r'
'difftimes.r'
'coercion.r'
'constants.r'
'data.r'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ exportMethods("minute<-")
exportMethods("month<-")
exportMethods("second<-")
exportMethods("year<-")
exportMethods(Compare)
exportMethods(as.character)
exportMethods(as.difftime)
exportMethods(as.interval)
Expand Down
25 changes: 22 additions & 3 deletions R/durations.r
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#' @include timespans.r
#' @include difftimes.r

check_duration <- function(object){
if (is.numeric(object@.Data))
Expand Down Expand Up @@ -27,9 +28,6 @@ check_duration <- function(object){
#' Durations class objects have one slot: .Data, a numeric object equal to the number
#' of seconds in the duration.
#'
#'
#'
#'
#' @name Duration-class
#' @rdname Duration-class
#' @exportClass Duration
Expand Down Expand Up @@ -119,6 +117,27 @@ setMethod("[[<-", signature(x = "Duration"),
})


#' @export
setMethod("Compare", c(e1 = "Duration", e2 = "ANY"),
function(e1, e2){
stop(sprintf("Incompatible duration classes (%s, %s). Please coerce with `as.duration`.",
class(e1), class(e2)),
call. = FALSE)
})

#' @export
setMethod("Compare", c(e1 = "difftime", e2 = "Duration"),
function(e1, e2){
callGeneric(as.numeric(e1, "secs"),
as.numeric(e2, "secs"))
})

#' @export
setMethod("Compare", c(e1 = "Duration", e2 = "Duration"),
function(e1, e2){
callGeneric(e1@.Data, e2@.Data)
})


#' Create a duration object.
#'
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-durations.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,20 @@ test_that("as.duration handles NA objects", {
na.dur <- dseconds(NA)
expect_equal(as.duration(NA), na.dur)
})


test_that("Comparison operators work duration and difftime objects (#323)", {
t1 <- now()
t2 <- t1 + dhours(1)
t3 <- t1 + dseconds(1)

expect_true((t2 - t1) > dseconds(60))
expect_false((t2 - t1) > dseconds(3600))
expect_true((t2 - t1) < dseconds(3601))

expect_true(dhours(1) > dminutes(59))
expect_true(dhours(1) == dseconds(3600))

expect_error(dhours(1) == 3600)
expect_error(dhours(1) == 1)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-ops-division.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ test_that("division operations work for difftime numerator",{

expect_error(diff/int)
expect_error(diff/days(1))
expect_equal(diff/ ddays(365), 1)
expect_equal(diff/ddays(365), 1)

})

Expand Down

0 comments on commit e9f1c4c

Please sign in to comment.