r-lib / clock

A Date-Time Library for R
https://clock.r-lib.org
Other
102 stars 5 forks source link

Implement `date_spanning_seq()` and friends #341

Closed DavisVaughan closed 1 year ago

DavisVaughan commented 1 year ago

Closes #279

I've chosen not to give this a by argument because it doesn't matter for the 99% case, but if we ever did want to go back and add that in we'd need to check that the result of the seq() call was a regular sequence that fully included to. We'd also need to check that by isn't negative. This helper could help with the first part:

check_regular_seq <- function(out, from, to, by, ..., call = caller_env()) {
  check_dots_empty0(...)

  size <- vec_size(out)

  # If `out` is empty, `x` was empty.
  # Otherwise, `by` is required to break `x` into a regular sequence,
  # meaning `to` must be included as the last value of the sequence.
  if (size == 0L || vec_equal(to, out[[size]])) {
    return(invisible(NULL))
  }

  message <- c(
    "{.arg by} must create a regular sequence along the limits of {.arg x}.",
    i = "{.arg by} is {by}.",
    i = "{.arg x} has a lower limit of {from}.",
    i = "{.arg x} has an upper limit of {to}."
  )

  cli::cli_abort(message, call = call)
}