Skip to content

Commit

Permalink
as.character() function added to return vector of timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
pvanlaake committed Apr 3, 2024
1 parent 26db569 commit 5bd9a8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
13 changes: 7 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion R/CFformat.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
#' 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()

if (is.null(format)) format <- ifelse(unit(cf@datum) < 4L || .has_time(time), "timestamp", "date")
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 {
Expand Down
15 changes: 15 additions & 0 deletions R/CFtime.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5bd9a8d

Please sign in to comment.