From 6fcf2de84b018b3e6c5ac84448262490b2f9fede Mon Sep 17 00:00:00 2001 From: Joshua Ulrich Date: Fri, 4 Aug 2023 11:55:21 -0500 Subject: [PATCH] Update index if tclass set to non-TZ from TZ class Updating the tclass from a class with a timezone to one without a timezone did not actually update the index. This meant that converting a POSIXct index to a Date index wouldn't actually change the index to a POSIXct value at midnight. Thanks to Daniel Palomar for the report. Reverse dependency checks passed after making this change. Fixes #311. --- R/tclass.R | 6 ++++++ inst/tinytest/test-tclass.R | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/R/tclass.R b/R/tclass.R index 004a41ce..2c7132d6 100644 --- a/R/tclass.R +++ b/R/tclass.R @@ -124,6 +124,12 @@ function(x, value) { } attr(attr(x,'index'), 'tclass') <- value + x_has_tz <- !isClassWithoutTZ(x) + if(x_has_tz && isClassWithoutTZ(value)) { + # update index values to midnight UTC (this also changes the tzone) + index(x) <- index(x) + } + # Remove class attrs (object created before 0.10-3) attr(x, ".indexCLASS") <- NULL attr(x, "tclass") <- NULL diff --git a/inst/tinytest/test-tclass.R b/inst/tinytest/test-tclass.R index 92b97262..868be118 100644 --- a/inst/tinytest/test-tclass.R +++ b/inst/tinytest/test-tclass.R @@ -65,3 +65,9 @@ info_msg <- "tclass() on object with no tclass/.indexCLASS returns POSIXct" x <- structure(1:5, .Dim = c(5L, 1L), index = 1:5, class = c("xts", "zoo")) expect_warning(xtc <- tclass(x), "index does not have a 'tclass' attribute") expect_identical(c("POSIXct", "POSIXt"), xtc) + +info_msg <- "tclass<-() updates index" +x <- xts(1, .POSIXct(14400, tz = "Europe/Berlin")) +tclass(x) <- "Date" +expect_identical(as.numeric(.index(x)), 0, info = paste(info_msg, "values")) +expect_identical(tzone(x), "UTC", info = paste(info_msg, "timezone"))