Skip to content

Commit

Permalink
Use correct types for date-time domains (#336)
Browse files Browse the repository at this point in the history
And fix partial match warnings

Fixes #298
  • Loading branch information
hadley authored Mar 24, 2022
1 parent ed3dee0 commit de10911
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# scales (development version)

* `time_trans()` and `date_trans()` have `domains` of the correct type so that
they can be transformed without error (#298).

* `label_date()` and `label_time()` gain a `locale` argument that allows you
to set the locale used to generate day and month names (#309).

Expand Down
14 changes: 12 additions & 2 deletions R/trans-date.r
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
#' t$inverse(t$transform(years))
#' t$format(t$breaks(range(years)))
date_trans <- function() {
trans_new("date", "from_date", "to_date", breaks = breaks_pretty())
trans_new("date",
transform = "from_date",
inverse = "to_date",
breaks = breaks_pretty(),
domain = to_date(c(-Inf, Inf))
)
}

to_date <- function(x) structure(x, class = "Date")
Expand Down Expand Up @@ -51,7 +56,12 @@ time_trans <- function(tz = NULL) {
structure(as.numeric(x), names = names(x))
}

trans_new("time", "from_time", "to_time", breaks = breaks_pretty())
trans_new("time",
transform = "from_time",
inverse = "to_time",
breaks = breaks_pretty(),
domain = to_time(c(-Inf, Inf))
)
}

#' Transformation for times (class hms)
Expand Down
18 changes: 13 additions & 5 deletions tests/testthat/test-trans-date.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ with_tz <- function(x, value) {

test_that("date/time scales raise error on incorrect inputs", {
time <- time_trans()
expect_error(time$trans(a_date), "Invalid input")
expect_error(time$transform(a_date), "Invalid input")

date <- date_trans()
expect_error(date$trans(a_time), "Invalid input")
expect_error(date$transform(a_time), "Invalid input")
})

test_that("time scales learn timezones", {
skip_if_not(getRversion() > "3.3.3")
time <- time_trans()
x <- time$inv(time$trans(a_time))
x <- time$inverse(time$transform(a_time))

expect_equal(tz(x), "UTC")
expect_equal(tz2(x), "UTC")

time <- time_trans()
x <- time$inv(time$trans(with_tz(a_time, "GMT")))
x <- time$inverse(time$transform(with_tz(a_time, "GMT")))

expect_equal(tz(x), "GMT")
expect_equal(tz2(x), "GMT")
})

test_that("tz arugment overrules default time zone", {
time <- time_trans("GMT")
x <- time$inv(time$trans(a_time))
x <- time$inverse(time$transform(a_time))

expect_equal(tz(x), "GMT")
expect_equal(tz2(x), "GMT")
Expand All @@ -57,3 +57,11 @@ test_that("date_breaks() works", {
as.Date(c("2012-01-01", "2012-02-01"))
)
})

test_that("can invert domain", {
t <- date_trans()
expect_equal(t$transform(t$domain), c(-Inf, Inf))

t <- time_trans()
expect_equal(t$transform(t$domain), c(-Inf, Inf))
})

0 comments on commit de10911

Please sign in to comment.