Skip to content

Commit

Permalink
[Fix #475] Bypass character->POSIX conversion on Date <> character co…
Browse files Browse the repository at this point in the history
…mparison
  • Loading branch information
vspinu committed Sep 23, 2016
1 parent faf3367 commit d561bf1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 21 additions & 11 deletions R/ops-compare.r
Expand Up @@ -15,28 +15,38 @@

.date_to_posix <- function(date, tz){
utc <- .POSIXct(unclass(date) * 86400, tz = "UTC")
## fixme: force_tz is heavy; could be done much faster by pre-computing the
## offset of tz.
## fixme: force_tz is extremely heavy; consider throwing a big warning instead
if (tz == "UTC") utc
else force_tz(utc, tz)
}

comp_posix_date <- function(e1, e2){
if (nargs() == 1)
stop(gettextf("unary '%s' not defined for \"%s\" objects", .Generic, class(e1)), domain = NA)
if (inherits(e1, "POSIXlt") || is.character(e1))

if (inherits(e1, "POSIXlt"))
e1 <- as.POSIXct(e1)
if (inherits(e2, "POSIXlt") || is.character(e2))
if (inherits(e2, "POSIXlt"))
e2 <- as.POSIXct(e2)

if(is.POSIXct(e1)){
if (is.Date(e2))
e2 <- .date_to_posix(e2, tz(e1))
if (is.character(e1)){
e1 <-
if (is.Date(e2)) as.Date(e1)
else as.POSIXct(e1)
}

if (is.character(e2)){
e2 <-
if (is.Date(e1)) as.Date(e2)
else as.POSIXct(e2)
}

if(is.POSIXct(e1) && is.Date(e2)){
e2 <- .date_to_posix(e2, tz(e1))
check_tzones(e1, e2)
} else if (is.Date(e1) && is.POSIXct(e2)) {
e1 <- .date_to_posix(e1, tz = tz(e2))
check_tzones(e1, e2)
} else if (is.Date(e1)) {
if (is.POSIXt(e2)){
e1 <- .date_to_posix(e1, tz = tz(e2))
}
}
NextMethod(.Generic)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-ops-compare.R
Expand Up @@ -5,6 +5,8 @@ test_that("Comparison operators work with POSIX and Date objects", {
expect_true(ymd_hms("2016-01-03 00:00:01", tz = "") > "2016-01-03 00:00:00")
expect_true(ymd_hms("2016-01-03 00:00:00", tz = "") == "2016-01-03")
expect_true(ymd("2016-01-03") == "2016-01-03")
expect_true(ymd("2016-01-03") > "2016-01-02")
expect_true(ymd("2016-01-03") < "2016-01-04")
expect_true(ymd("2016-01-03") == ymd_hms("2016-01-03 00:00:00"))
expect_true(ymd("2016-01-03") < ymd_hms("2016-01-03 00:00:01"))
expect_true(ymd("2016-01-03") != ymd_hms("2016-01-03 00:00:01"))
Expand Down

0 comments on commit d561bf1

Please sign in to comment.