calendar icon indicating copy to clipboard operation
calendar copied to clipboard

ic_datetime and vectors of datetime

Open r2evans opened this issue 4 years ago • 3 comments

Given a vector of ICS entries, ic_datetime does not check for length greater than 1. If an ICS file has more than one event:

calendar::ic_read("~/twoevents.ics")
# Warning in if (!is.na(x) & !x == "NA" & !grepl("^\\d{8}T\\d{6}Z?$", x)) { :
#   the condition has length > 1 and only the first element will be used
# Warning in if (!is.na(x) & !x == "NA" & !grepl("^\\d{8}T\\d{6}Z?$", x)) { :
#   the condition has length > 1 and only the first element will be used
# # A tibble: 2 x 8
#   DTSTART             DTEND               SUMMARY CATEGORIES LOCATION UID   `X-MICROSOFT-CD~
#   <dttm>              <dttm>              <chr>   <chr>      <chr>    <chr> <chr>           
# ...

(It doesn't matter what my ICS file contains, just that it contains more than one.)

This is traced back to https://github.com/ATFutures/calendar/blob/master/R/ic_date.R#L13

ic_datetime <- function(x) {

  # TODO (LH): regex check x timestamp
  if(!is.na(x) & !x == "NA" & !grepl("^\\d{8}T\\d{6}Z?$", x)) {
    # stop("time should be in this format: 20180809T160000Z")
    warning("Non-standard time string: should be in this format: 20180809T160000Z")
    x = ""
  }

  plain <- gsub("[TZtz]", "", x)
  datetime <- as.POSIXct(plain, format = "%Y%m%d%H%M%S")
  datetime
}

which is called by ic_dataframe here:

  if(any(datetime_cols)) {
    x_df[datetime_cols] <- lapply(x_df[, datetime_cols], ic_datetime)
  }

I suggest that ic_datetime should be vectorized (vice trying to change ic_dataframe).

r2evans avatar Mar 10 '20 15:03 r2evans