Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow subsecond intervals with date_breaks() #160

Merged
merged 5 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion R/date-time.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ 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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please indent so each argument to structure() goes on its own line?

tzone = attr(date, "tzone", exact = TRUE) %||% "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you need the %||% "" here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this to match what is being called for other units in L32 but no I did not fully think through whether it's necessary in either place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, let's keep it consistent then

}

prec <- parse_unit_spec(time)
Expand Down
9 changes: 8 additions & 1 deletion R/full-seq.r
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ 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
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"))
)
})