Skip to content

Commit

Permalink
Fix suggested CRAN, style, speed, and documentation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
billdenney authored and vspinu committed Jul 27, 2018
1 parent b7c450b commit a62d089
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
33 changes: 21 additions & 12 deletions R/format_ISO8601.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
#' precision for the object is shown.
#' @param ... Additional arguments to methods.
#' @return A character vector of ISO8601-formatted text.
#' @references \url{https://en.wikipedia.org/wiki/ISO_8601}
#' @examples
#' format_ISO8601(as.Date("02-01-2018", format="%m-%d-%Y"))
#' format_ISO8601(as.POSIXct("2018-02-01 03:04:05", tz="EST"), usetz=TRUE)
#' format_ISO8601(as.POSIXct("2018-02-01 03:04:05", tz="EST"), precision="ymdhm")
#' @aliases format_ISO8601,Date-method
#' format_ISO8601,POSIXt-method
#' format_ISO8601,Interval-method
#' format_ISO8601,Duration-method
#' format_ISO8601,Period-method
#' @export
setGeneric(name = "format_ISO8601",
def = function(x, usetz=FALSE, precision=NULL, ...) standardGeneric("format_ISO8601"))
Expand Down Expand Up @@ -94,6 +100,14 @@ setMethod("format_ISO8601", signature="Period",
paste0("P", date_part))
})

ISO8601_precision_map <-
list(y="%Y",
ym="%Y-%m",
ymd="%Y-%m-%d",
ymdh="%Y-%m-%dT%H",
ymdhm="%Y-%m-%dT%H:%M",
ymdhms="%Y-%m-%dT%H:%M:%S")

#' Provide a format for ISO8601 dates and times with the requested precision.
#'
#' @param precision The amount of precision to represent with substrings of
Expand All @@ -107,29 +121,24 @@ setMethod("format_ISO8601", signature="Period",
#' more precise than \code{max_precision}, a warning is given and
#' \code{max_precision} is returned.
format_ISO8601_precision_check <- function(precision, max_precision, usetz=FALSE) {
precision_map <- list(y="%Y",
ym="%Y-%m",
ymd="%Y-%m-%d",
ymdh="%Y-%m-%dT%H",
ymdhm="%Y-%m-%dT%H:%M",
ymdhms="%Y-%m-%dT%H:%M:%S")
if (!(max_precision %in% names(precision_map))) {
if (!(max_precision %in% names(ISO8601_precision_map))) {
stop("Invalid value for max_precision provided: ", max_precision)
}
if (is.null(precision)) {
precision <- max_precision
}
if (!(precision %in% names(precision_map))) {
stop("Invalid value for precision provided: ", precision)
}
if (nchar(precision) > nchar(max_precision)) {
warning("More precision requested (", precision, ") than allowed (", max_precision, ") for this format. Using maximum allowed precision.")
warning("More precision requested (", precision, ") ",
"than allowed (", max_precision, ") for this format. ",
"Using maximum allowed precision.")
precision <- max_precision
}
if (length(precision) != 1) {
stop("precision must be a scalar")
}
ret <- precision_map[[precision]]
if (is.null(ret <- ISO8601_precision_map[[precision]])) {
stop("Invalid value for precision provided: ", precision)
}
if (usetz) {
ret <- paste0(ret, "%z")
}
Expand Down
8 changes: 8 additions & 0 deletions man/format_ISO8601.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/testthat/test-format_ISO8601.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ test_that("Formatting a Duration works", {
expect_equal(format_ISO8601(duration(20, units="minutes")),
paste0("PT", 20*60,"S"),
info="Duration always formats as seconds to ensure precision.")
expect_warning(format_ISO8601(duration(20, units="minutes"), precision="y"))
expect_warning(format_ISO8601(duration(20, units="minutes"), precision="y"),
regexp="precision is not used for Duration objects",
fixed=TRUE)
})

test_that("Formatting a Period works", {
Expand Down

0 comments on commit a62d089

Please sign in to comment.