Browse files

Add tz support to time_trans

  • Loading branch information...
1 parent 542b814 commit 274c1f9abd9a3c7726edd569c893b289e3886276 @hadley committed Jul 5, 2012
Showing with 54 additions and 9 deletions.
  1. +4 −0 NEWS
  2. +20 −9 R/trans-date.r
  3. +30 −0 inst/tests/test-trans-date.r
View
4 NEWS
@@ -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.
View
29 R/trans-date.r
@@ -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.
#'
View
30 inst/tests/test-trans-date.r
@@ -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.