Skip to content

Commit

Permalink
Add tz support to time_trans
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jul 5, 2012
1 parent 542b814 commit 274c1f9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* `time_trans` gains an optional argument `tz` to specify the time zone to use
for the times. If not specified, it will be guess from the first input with
a non-null time zone.

* `date_trans` and `time_trans` now check that their inputs are of the correct
type. This prevents ggplot2 scales from silently giving incorrect outputs
when given incorrect inputs.
Expand Down
29 changes: 20 additions & 9 deletions R/trans-date.r
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,36 @@ from_date <- function(x) {

#' Transformation for times (class POSIXt).
#'
#' @param tz Optionally supply the time zone. If \code{NULL}, the default,
#' the time zone will be extracted from first input with a non-null tz.
#' @export
#' @examples
#' hours <- seq(ISOdate(2000,3,20, tz = ""), by = "hour", length.out = 10)
#' t <- time_trans()
#' t$trans(hours)
#' t$inv(t$trans(hours))
#' t$format(t$breaks(range(hours)))
time_trans <- function() {
time_trans <- function(tz = NULL) {

to_time <- function(x) {
force(x)
structure(x, class = c("POSIXt", "POSIXct"), tzone = tz)
}

from_time <- function(x) {
if (!inherits(x, "POSIXct")) {
stop("Invalid input: time_trans works with objects of class ",
"POSIXct only", call. = FALSE)
}
if (is.null(tz)) {
tz <<- attr(as.POSIXlt(x), "tzone")[[1]]
}
structure(as.numeric(x), names = names(x))
}

trans_new("time", "from_time", "to_time", breaks = pretty_breaks())
}

to_time <- function(x) structure(x, class = c("POSIXt", "POSIXct"))
from_time <- function(x) {
if (!inherits(x, "POSIXct")) {
stop("Invalid input: time_trans works with objects of class POSIXct only",
call. = FALSE)
}
structure(as.numeric(x), names = names(x))
}

#' Regularly spaced dates.
#'
Expand Down
30 changes: 30 additions & 0 deletions inst/tests/test-trans-date.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,41 @@ context("Trans - dates and times")
a_time <- ISOdatetime(2012, 1, 1, 11, 30, 0, tz = "UTC")
a_date <- as.Date(a_time)

tz <- function(x) attr(as.POSIXlt(x), "tzone")[1]
tz2 <- function(x) format(x, "%Z")

with_tz <- function(x, value) {
as.POSIXct(format(x, tz = value, usetz = TRUE), tz = value)
}

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

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

})

test_that("time scales learn timezones", {
time <- time_trans()
x <- time$inv(time$trans(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")))

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))

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

})

0 comments on commit 274c1f9

Please sign in to comment.