Skip to content

Commit

Permalink
Handle ISO8601 string outside valid index range
Browse files Browse the repository at this point in the history
Passing an invalid date/time string to .parseISO8601() when both
'start' and 'end' arguments are also specified caused the function
return the first and last index values.

Now the function returns a first and last value equal to NA_real_,
with a warning. .parseISO8601() is primarily used in `[.xts` where
the returned NA values will cause the subset call to return no
instead of all rows.

Fixes #96.
  • Loading branch information
joshuaulrich committed Jul 8, 2018
1 parent c4f92b1 commit 7d487bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/parse8601.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
if(is.na(e))
e <- as.POSIXlt(do.call(lastof, parse.side(intervals[2])))
}
if(is.na(s) && is.na(e) && !nzchar(DURATION)) {
warning("cannot determine first and last time from ", x)
return(list(first.time=NA_real_,last.time=NA_real_))
}
if(!missing(start)) {
start <- as.numeric(start)
#s <- as.POSIXlt(structure(max(start, as.numeric(s), na.rm=TRUE),
Expand Down
7 changes: 7 additions & 0 deletions inst/unitTests/runit.subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,10 @@ test.i_POSIXlt <- function() {
checkIdentical(y, x[i, ])
}
}

# invalid date/time
test.i_invalid_date_string <- function() {
x <- xts(1:10, as.Date("2015-02-20")+0:9)
y <- x["2012-02-30"]
checkIdentical(y, x[NA,])
}

0 comments on commit 7d487bd

Please sign in to comment.