Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Fix time span calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Sep 26, 2024
1 parent 1c954b5 commit 527349a
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 83 deletions.
4 changes: 1 addition & 3 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ setGeneric(
#' (see [calendar()]).
#' @param decimal A [`logical`] scalar: should decimal years be returned?
#' If `FALSE`, the decimal part is dropped.
#' @param shift A [`logical`] scalar: should years be shifted according to the
#' [calendar epoch][calendar_epoch()]?
#' @param ... Currently not used.
#' @return
#' A [`numeric`] vector of (decimal) years.
Expand Down Expand Up @@ -613,7 +611,7 @@ NULL
#' Durations
#'
#' Get the duration of time series or intervals.
#' @param x A [`TimeSeries-class`] object.
#' @param x A [`TimeSeries-class`] or a [`TimeIntervals-class`] object.
#' @param calendar A [`TimeScale-class`] object specifying the target calendar
#' (see [calendar()]). If `NULL` (the default), *rata die* are returned.
#' @param ... Currently not used.
Expand Down
31 changes: 18 additions & 13 deletions R/calendar-gregorian.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ setMethod(
f = "fixed",
signature = c(year = "numeric", month = "numeric", day = "numeric", calendar = "GregorianCalendar"),
definition = function(year, month, day, calendar) {
## Recycle
n <- length(year)
if (n > 1) {
if (length(month) == 1) month <- rep(month, n)
if (length(day) == 1) day <- rep(day, n)
}

## Switch origin
year <- (year - calendar_epoch(calendar)) * calendar_direction(calendar)

Expand Down Expand Up @@ -68,7 +75,7 @@ setMethod(
setMethod(
f = "as_year",
signature = c(object = "numeric", calendar = "GregorianCalendar"),
definition = function(object, calendar, decimal = TRUE, shift = TRUE, ...) {
definition = function(object, calendar, decimal = TRUE, ...) {
d0 <- object - calendar_fixed(calendar)
n400 <- d0 %/% 146097
d1 <- d0 %% 146097
Expand All @@ -81,21 +88,19 @@ setMethod(
year <- 400 * n400 + 100 * n100 + 4 * n4 + n1
year <- ifelse(n100 == 4 | n1 == 4, year, year + 1)

if (isTRUE(shift)) {
## Shift origin
year <- (year - calendar_epoch(calendar)) * calendar_direction(calendar)
## Shift origin
year <- (year - calendar_epoch(calendar)) * calendar_direction(calendar)

if (isTRUE(decimal)) {
## Year length in days
start <- fixed(year, 01, 01, calendar = calendar)
end <- fixed(year, 12, 31, calendar = calendar)
total <- end - start + 1
if (isTRUE(decimal)) {
## Year length in days
start <- fixed(year, 01, 01, calendar = calendar)
end <- fixed(year, 12, 31, calendar = calendar)
total <- end - start + 1

## Elapsed time
sofar <- object - start
## Elapsed time
sofar <- object - start

year <- year + sofar / total
}
year <- year + sofar / total
}

year
Expand Down
6 changes: 4 additions & 2 deletions R/intervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ setMethod(
msg <- "%s is already expressed in rata die: %s is ignored."
warning(sprintf(msg, sQuote("start"), sQuote("calendar")), call. = FALSE)
} else {
start <- fixed(start, calendar = calendar, scale = scale)
start <- start * scale # Rescale to years
start <- fixed(start, 01, 01, calendar = calendar)
}

## End
if (methods::is(end, "RataDie")) {
msg <- "%s is already expressed in rata die: %s is ignored."
warning(sprintf(msg, sQuote("end"), sQuote("calendar")), call. = FALSE)
} else {
end <- fixed(end, calendar = calendar, scale = scale)
end <- end * scale # Rescale to years
end <- fixed(end, 12, 31, calendar = calendar)
}

names <- names %||% names(start) %||% names(end)
Expand Down
3 changes: 2 additions & 1 deletion R/overlap.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ setMethod(

## Convert to calendar years
if (!is.null(calendar)) {
index <- as_year(index, calendar = calendar, shift = FALSE)
index <- as_year(index, calendar = calendar, decimal = FALSE)
index <- abs(index - calendar_epoch(calendar))
}

## Matrix of results
Expand Down
10 changes: 6 additions & 4 deletions R/span.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ NULL
#' @aliases span,TimeSeries-method
setMethod(
f = "span",
signature = "TimeSeries",
signature = c(x = "TimeSeries"),
definition = function(x, calendar = NULL) {
z <- max(x@.Time) - min(x@.Time)
if (is.null(calendar)) return(unclass(z))
as_year(z, calendar = calendar, shift = FALSE)
y <- as_year(z, calendar = calendar, decimal = FALSE)
abs(y - calendar_epoch(calendar))
}
)

Expand All @@ -20,10 +21,11 @@ setMethod(
#' @aliases span,TimeIntervals-method
setMethod(
f = "span",
signature = "TimeIntervals",
signature = c(x = "TimeIntervals"),
definition = function(x, calendar = NULL) {
z <- x@.End - x@.Start
if (is.null(calendar)) return(unclass(z))
as_year(z, calendar = calendar, shift = FALSE)
y <- as_year(z, calendar = calendar, decimal = FALSE)
abs(y - calendar_epoch(calendar)) - 1 #WHY?
}
)
10 changes: 5 additions & 5 deletions R/time.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ setMethod(
definition = function(x, calendar = NULL) {
z <- min(x@.Time)
if (is.null(calendar)) return(z)
as_year(z, calendar = calendar, decimal = TRUE)
as_year(z, calendar = calendar, decimal = FALSE)
}
)

Expand All @@ -24,7 +24,7 @@ setMethod(
definition = function(x, calendar = NULL) {
z <- x@.Start
if (is.null(calendar)) return(z)
as_year(z, calendar = calendar, decimal = TRUE)
as_year(z, calendar = calendar, decimal = FALSE)
}
)

Expand All @@ -37,7 +37,7 @@ setMethod(
definition = function(x, calendar = NULL) {
z <- max(x@.Time)
if (is.null(calendar)) return(z)
as_year(z, calendar = calendar, decimal = TRUE)
as_year(z, calendar = calendar, decimal = FALSE)
}
)

Expand All @@ -50,7 +50,7 @@ setMethod(
definition = function(x, calendar = NULL) {
z <- x@.End
if (is.null(calendar)) return(z)
as_year(z, calendar = calendar, decimal = TRUE)
as_year(z, calendar = calendar, decimal = FALSE)
}
)

Expand All @@ -63,7 +63,7 @@ setMethod(
definition = function(x, calendar = NULL) {
z <- x@.Time
if (is.null(calendar)) return(z)
as_year(z, calendar = calendar, decimal = TRUE)
as_year(z, calendar = calendar, decimal = FALSE)
}
)

Expand Down
52 changes: 26 additions & 26 deletions inst/tinytest/_tinysnapshot/plot_interval_CE.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 24 additions & 24 deletions inst/tinytest/_tinysnapshot/plot_interval_rd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions inst/tinytest/test_intervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ expect_identical(start(x, calendar = CE()), lower)
expect_identical(end(x, calendar = CE()), upper)

# Duration =====================================================================
expect_identical(span(x, calendar = CE()), upper - lower)
expect_identical(span(x, calendar = CE()), span(x, calendar = BP()))

# Overlap ======================================================================
Expand Down
5 changes: 1 addition & 4 deletions man/as_year.Rd

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

2 changes: 1 addition & 1 deletion man/span.Rd

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

0 comments on commit 527349a

Please sign in to comment.