Nothing
#' @importFrom S7 props props<-
granule_inherit_props <- function(x, parent) {
if (is.null(x@calendar)) {
x@calendar <- parent@calendar
}
naive_props <- is.naive(props <- props(x))
parent_props <- props(parent)
inherit_props <- intersect(names(props)[naive_props], names(parent_props))
props[inherit_props] <- parent_props[inherit_props]
props(x) <- props
x
}
# Inherit naive properties on which every parent shares the same attribute.
granule_inherit_shared_props <- function(x, parents) {
calendars <- lapply(parents, time_calendar)
if (all(vapply(calendars, identical, logical(1L), calendars[[1L]]))) {
x@calendar <- calendars[[1L]]
}
# Build a stand-in parent by filling x's own naive properties with the
# value every parent agrees on, then let granule_inherit_props() perform
# the actual copy (a no-op for properties left untouched below).
shared <- x
shared_props <- props(shared)
for (prop in names(shared_props)[is.naive(shared_props)]) {
values <- lapply(parents, function(parent) props(parent)[[prop]])
is_unset <- vapply(values, function(value) {
# `tz` (and other naive-fillable properties) can carry an explicit NA
# rather than the classed `mt_naive` sentinel (e.g. `datetime(x, tz =
# NA)`) -- both mean "unknown", so both must block reconciliation.
is.null(value) || inherits(value, "mt_naive") || (length(value) == 1L && is.na(value))
}, logical(1L))
if (any(is_unset)) {
next
}
if (prop == "tz") {
# Differing known zones can always be reconciled at a common zone (UTC,
# since every named zone converts to it without loss); only a genuinely
# unknown zone (handled above) leaves the result naive.
shared_props[[prop]] <- if (length(unique(values)) == 1L) values[[1L]] else "UTC"
next
}
if (!all(vapply(values, identical, logical(1L), values[[1L]]))) {
next
}
shared_props[[prop]] <- values[[1L]]
}
props(shared) <- shared_props
granule_inherit_props(x, shared)
}
# Are `x` and `y` the exact same chronon - same class and same property
# values? Used to detect a pure identity conversion in
# chronon_convert_impl(). `identical()` alone is too strict for this: S7's
# `class_numeric` doesn't normalise storage mode, so a numeric property (e.g.
# `n`) can be an integer on one side and a double on the other even when the
# chronons are otherwise the same - `identical()` would then treat two
# genuinely identical chronons as different.
granule_identical <- function(x, y) {
if (!identical(S7_class_id(x), S7_class_id(y))) {
return(FALSE)
}
x_props <- props(x)
y_props <- props(y)
# Calendar membership does not change a unit's size or physical context.
x_props$calendar <- y_props$calendar <- NULL
props_equal(x_props, y_props)
}
# Are the named property lists `x` and `y` the same, treating integer and
# double values as equal? Classes are kept, so a naive property (mt_naive)
# differs from an explicit one with the same value unless both are numeric.
props_equal <- function(x, y) {
if (!identical(names(x), names(y))) {
return(FALSE)
}
for (nm in names(x)) {
a <- x[[nm]]
b <- y[[nm]]
same <- if (is.numeric(a) && is.numeric(b)) {
identical(as.double(vec_data(a)), as.double(vec_data(b)))
} else {
identical(a, b)
}
if (!isTRUE(same)) {
return(FALSE)
}
}
TRUE
}
# Convert a chronon's *default*-naive properties (the classed `mt_naive`
# sentinel, e.g. an unset `tz`) into an explicit NA of the same underlying
# type (e.g. `datetime(x, tz = NA)`'s naive result)
granule_harden_naive <- function(x) {
props <- props(x)
naive_props <- is.naive(props)
props[naive_props] <- lapply(props[naive_props], unclass)
props(x) <- props
x
}
# Normalise a granule's size `n` to a canonical storage type: whole numbers
# (within integer range) are stored as integers, matching the `1L` used by
# unit constructors (e.g. `cal_gregorian$day(1L)`). Without this, a size
# computed with doubles (e.g. `unit@n * as.numeric(duration)`) would give an
# otherwise equal granule that is not `identical()` (e.g. `num 1` vs `int 1`),
# which keeps such values apart when combined in a `mixtime` vector.
granule_n_normalise <- function(n) {
if (
is.double(n) && length(n) == 1L && !is.na(n) &&
n == trunc(n) && abs(n) <= .Machine$integer.max
) {
n <- as.integer(n)
}
n
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.