-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix timezone issues in scale_*_datetime (#1767)
Fixes #1718 * Store and access timezone when making labels and breaks * Add default timezone argument
- Loading branch information
Showing
4 changed files
with
80 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
context("scale_date") | ||
|
||
base_time <- function(tz = "") { | ||
as.POSIXct(strptime("2015-06-01", "%Y-%m-%d", tz = tz)) | ||
} | ||
|
||
df <- data.frame( | ||
time1 = base_time("") + 0:6 * 3600, | ||
time2 = base_time("UTC") + 0:6 * 3600, | ||
time3 = base_time("Australia/Lord_Howe") + (0:6 + 13) * 3600, # has half hour offset | ||
y = seq_along(base_time) | ||
) | ||
|
||
test_that("inherits timezone from data", { | ||
# Local time | ||
p <- ggplot(df, aes(y = y)) + geom_point(aes(time1)) | ||
sc <- layer_scales(p)$x | ||
expect_equal(sc$timezone, NULL) | ||
expect_equal(sc$get_labels()[1], "00:00") | ||
|
||
# UTC | ||
p <- ggplot(df, aes(y = y)) + geom_point(aes(time2)) | ||
sc <- layer_scales(p)$x | ||
expect_equal(sc$timezone, "UTC") | ||
expect_equal(sc$get_labels()[1], "00:00") | ||
}) | ||
|
||
|
||
test_that("first timezone wins", { | ||
p <- ggplot(df, aes(y = y)) + | ||
geom_point(aes(time2)) + | ||
geom_point(aes(time3), colour = "red") + | ||
scale_x_datetime(date_breaks = "hour", date_labels = "%H:%M") | ||
sc <- layer_scales(p)$x | ||
expect_equal(sc$timezone, "UTC") | ||
}) | ||
|
||
test_that("not cached across calls", { | ||
scale_x <- scale_x_datetime(date_breaks = "hour", date_labels = "%H:%M") | ||
|
||
p1 <- ggplot(df, aes(y = y)) + geom_point(aes(time2)) + scale_x | ||
p2 <- ggplot(df, aes(y = y)) + geom_point(aes(time3)) + scale_x | ||
|
||
expect_equal(layer_scales(p1)$x$timezone, "UTC") | ||
expect_equal(layer_scales(p2)$x$timezone, "Australia/Lord_Howe") | ||
}) |