diff --git a/NEWS.md b/NEWS.md index eabccbe..1850af3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,13 @@ # CFtime (development version) -# CFtime 1.3.0 +Changes since release 1.3.0: -Changes since release 1.2.0: +* as.character() and length() methods added that return a vector of timestamps +or the number of offsets in a CFtime instance, respectively. +* Minor code fixes, see GitHub commits. +* Documentation updated, with description of new functions. + +# CFtime 1.3.0 * Two CFtime instances can be added if they have compatible calendars and units. The earlier origin is preserved in the result and offsets from the later instance @@ -20,8 +25,6 @@ timestamps is parsed and found to have different time zones, a warning is genera # CFtime 1.2.0 -Changes since release 1.1.0: - * Datum units "years" and "months" added. While these units are discouraged by the CF Metadata Conventions due to their problematic definition, there are quite a few data sets out there that use these units nonetheless. For this reason, @@ -46,8 +49,6 @@ previous behaviour so the API is not broken. # CFtime 1.1.0 -Changes since release 1.0.0: - * CFtime() can now also be invoked with a vector of character timestamps as offsets, or with a single timestamp to create a complete time series from the datum to the indicated timestamp. diff --git a/R/CFformat.R b/R/CFformat.R index 01a6307..b92997f 100644 --- a/R/CFformat.R +++ b/R/CFformat.R @@ -37,6 +37,8 @@ #' tail(CFtimestamp(cf2 + 1.5)) CFtimestamp <- function(cf, format = NULL, asPOSIX = FALSE) { if (!(methods::is(cf, "CFtime"))) stop("First argument to CFtimestamp must be an instance of the `CFtime` class") + if (asPOSIX && calendar_id(cf@datum) != 1L) stop("Cannot make a POSIX timestamp on a non-standard calendar") + time <- .offsets2time(cf@offsets, cf@datum) if (nrow(time) == 0L) return() @@ -44,7 +46,6 @@ CFtimestamp <- function(cf, format = NULL, asPOSIX = FALSE) { else if (!(format %in% c("date", "time", "timestamp"))) stop("Format specifier not recognized") if (asPOSIX) { - if (calendar_id(cf@datum) != 1L) stop("Cannot make a POSIX timestamp on a non-standard calendar") if (format == "date") ISOdate(time$year, time$month, time$day, 0L) else ISOdatetime(time$year, time$month, time$day, time$hour, time$minute, time$second, "UTC") } else { diff --git a/R/CFtime.R b/R/CFtime.R index 9b34a98..5362d60 100644 --- a/R/CFtime.R +++ b/R/CFtime.R @@ -138,6 +138,21 @@ setMethod("length", "CFtime", function(x) { length(x@offsets) }) +#' Return the timestamps contained in the CFtime instance. +#' +#' @param x The CFtime instance whose timestamps will be returned +#' +#' @return The timestamps in the specified CFtime instance. +#' @export +#' +#' @examples +#' cf <- CFtime("days since 1850-01-01", "julian", 0:364) +#' as.character(cf) +setMethod("as.character", "CFtime", function(x) { + if (length(x@offsets) > 0) + CFtimestamp(x) +}) + setMethod("show", "CFtime", function(object) { noff <- length(object@offsets) if (noff == 0L) {