Skip to content

Commit

Permalink
[Fix #441] Register S3 methods for POSIXt <> Date comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Aug 3, 2016
1 parent 2c47263 commit 14ac8a7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ Collate:
'leap-years.r'
'ops-addition.r'
'ops-%m+%.r'
'ops-compare.r'
'ops-division.r'
'ops-integer-division.r'
'ops-modulo.r'
'ops-multiplication.r'
'ops-subtraction.r'
'ops.r'
'parse.r'
'pretty.r'
'round.r'
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Version 1.5.6.9000 (development)

### NEW FEATURES

* [#441](https://github.com/hadley/lubridate/issues/441) Comparison between POSIXt and Date objects is now possible.
* [#437](https://github.com/hadley/lubridate/issues/437) New function `as_datetime` to coerce to POSIXct object. A counterpart of `as_date`.
* [#412](https://github.com/hadley/lubridate/issues/412) New function `make_date` to produce Date objects. A counterpart of `make_datetime`.
* [#268](https://github.com/hadley/lubridate/issues/268) `round_date`, `ceiling_date`, and `floor_date` now accept "quarter", "bimonth", and "halfyear" as `unit` options.
Expand All @@ -15,6 +16,7 @@ Version 1.5.6.9000 (development)
* [#403](https://github.com/hadley/lubridate/issues/403) Update on `Date` objects now return `POSIXct` instead of `POSIXlt`.
* [#411](https://github.com/hadley/lubridate/issues/411) format `mdy` or `myd` beginning with `"January"` or `"Jan"` now parsing correctly
* `here` and `olson_time_zones` were deprecated in favor of `new` and base `OlsonNames` respectively.
* Internally, S4 Compare and Ops generics were cleaned and simplified.

### BUG FIXES
* [#418](https://github.com/hadley/lubridate/issues/418) C level parsing functions understand 24:00:00 in datetime strings.
Expand Down
43 changes: 43 additions & 0 deletions R/ops-compare.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

## #' @export
## setMethod("<", signature(e1 = "POSIXct", e2 = "Date"),
## function(e1, e2) {
## cat("here")
## callNextMethod(e1, .POSIXct(e2, tz = attr(e1, "tzone")))
## })

## #' @export
## setMethod("<", signature(e1 = "Date", e2 = "POSIXct"),
## function(e1, e2) {
## cat("here")
## callNextMethod(.POSIXct(e1, tz = attr(e2, "tzone")), e2)
## })

comp_posix_date <- function(e1, e2){
if (nargs() == 1)
stop(gettextf("unary '%s' not defined for \"%s\" objects", .Generic, class(e1)), domain = NA)
if(is.POSIXt(e1) || is.character(e1)){
if (inherits(e1, "POSIXlt") || is.character(e1))
e1 <- as.POSIXct(e1)
if (inherits(e2, "POSIXlt") || is.character(e2))
e2 <- as.POSIXct(e2)
if (is.Date(e2))
e2 <- .POSIXct(unclass(e2) * 86400, tz = attr(e1, "tzone"))
check_tzones(e1, e2)
} else if (is.Date(e1) || is.character(e1)) {
if (is.character(e2))
e2 <- as.Date(e2)
if (is.POSIXt(e2)){
e1 <- .POSIXct(unclass(e1) * 86400, tz = attr(e2, "tzone"))
}
}
NextMethod(.Generic)
}

## Nothing else seem to work, only this sneaky method.
evalqOnLoad({
for(op in c("<", ">", "==", ">=", "<=", "!=")){
registerS3method(op, "Date", comp_posix_date)
registerS3method(op, "POSIXct", comp_posix_date)
}
})
4 changes: 1 addition & 3 deletions man/round_date.Rd

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

0 comments on commit 14ac8a7

Please sign in to comment.