Skip to content

Commit

Permalink
Restore time-of-day subset begin > end behavior
Browse files Browse the repository at this point in the history
This restores the behavior that existed before the prior commit to
improve performance.

Yes, this actually makes sense and is useful for financial markets.
For example, some futures markets open at 18:00 and close at 16:00 the
next day.

See #193.
  • Loading branch information
joshuaulrich committed Jan 13, 2019
1 parent 17bc247 commit c91b676
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion R/xts.methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
secBegin <- timestringToSeconds(fromTimeString)
secEnd <- timestringToSeconds(toTimeString)

which(secOfDay >= secBegin & secOfDay <= secEnd)
if (secBegin <= secEnd) {
i <- secOfDay >= secBegin & secOfDay <= secEnd
} else {
i <- secOfDay >= secBegin | secOfDay <= secEnd
}
which(i)
}

.subset_xts <- function(x, i, j, ...) {
Expand Down
10 changes: 10 additions & 0 deletions inst/unitTests/runit.subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ test.time_of_day_start_equals_end <- function() {
checkIdentical(.index(x["T01:00/T01:00"]), i1)
}

test.time_of_day_end_before_start <- function() {
# Yes, this actually makes sense and is useful for financial markets
# E.g. some futures markets open at 18:00 and close at 16:00 the next day
i <- 0:47
x <- .xts(i, i * 3600, tz = "UTC")
i1 <- .index(x[-c(18L, 42L)])

checkIdentical(.index(x["T18:00/T16:00"]), i1)
}

# TODO: Add tests for possible edge cases and/or errors
# end time before start time
# start time and/or end time missing "T" prefix
Expand Down

0 comments on commit c91b676

Please sign in to comment.