Skip to content

Commit

Permalink
Allow subsecond intervals with date_breaks() (#160)
Browse files Browse the repository at this point in the history
* Add subsecond interval support to date_breaks() and full_seq()

Closes #85 

* Fix tz mismatch for sec and min intervals. Fix tests.
  • Loading branch information
Dana Paige Seidel authored Jul 25, 2018
1 parent 5c68e6d commit 37668bf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# scales 0.5.0.9000

* `date_breaks()` now supports subsecond intervals (@dpseidel, #85).

* New function `modulus_trans()` implements the modulus transformation for positive
and negative values (@dpseidel).

Expand Down
6 changes: 5 additions & 1 deletion R/date-time.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ floor_date <- function(date, time) {
as.Date(cut(date, time, right = TRUE, include.lowest = TRUE))
}
}

floor_time <- function(date, time) {
to_time <- function(x) {
force(x)
structure(x, class = c("POSIXt", "POSIXct"))
structure(x,
class = c("POSIXt", "POSIXct"),
tzone = attr(date, "tzone", exact = TRUE) %||% ""
)
}

prec <- parse_unit_spec(time)
Expand Down
10 changes: 9 additions & 1 deletion R/full-seq.r
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@ fullseq.Date <- function(range, size, ...) {
}
#' @export
fullseq.POSIXt <- function(range, size, ...) {
seq(floor_time(range[1], size), ceiling_time(range[2], size), by = size)

# for subsecond interval support
# seq() does not support partial secs in character strings
parsed <- parse_unit_spec(size)
if (parsed$unit == "sec") {
seq(floor_time(range[1], size), ceiling_time(range[2], size), by = parsed$mult)
} else {
seq(floor_time(range[1], size), ceiling_time(range[2], size), by = size)
}
}
2 changes: 1 addition & 1 deletion R/trans-date.r
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ time_breaks <- function(n = 5) {
#'
#' @param width an interval specification, one of "sec", "min", "hour",
#' "day", "week", "month", "year". Can be by an integer and a space, or
#' followed by "s".
#' followed by "s". Fractional seconds are supported.
#' @export
date_breaks <- function(width = "1 month") {
force(width)
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-trans-date.r
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,22 @@ test_that("tz arugment overrules default time zone", {
expect_equal(tz(x), "GMT")
expect_equal(tz2(x), "GMT")
})

test_that("date_breaks() works", {
times <- as.POSIXct(c("2000-01-01 08:29:58", "2000-01-01 08:30:10"), tz = "UTC")

expect_equal(
date_breaks("1 hour")(times),
as.POSIXct(c("2000-01-01 8:00:00 UTC", "2000-01-01 9:00:00 UTC"), tz = "UTC")
)
expect_equal(
date_breaks(".5 secs")(times)[1:2],
as.POSIXct(c("2000-01-01 08:29:58.0 UTC", "2000-01-01 08:29:58.5 UTC"), tz = "UTC")
)

dates <- a_date + 1:30
expect_equal(
date_breaks("1 month")(dates),
as.Date(c("2012-01-01", "2012-02-01"))
)
})

0 comments on commit 37668bf

Please sign in to comment.