Skip to content

Commit

Permalink
Update index if tclass set to non-TZ from TZ class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshuaulrich committed Aug 4, 2023
1 parent ba9dc66 commit 6fcf2de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions R/tclass.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions inst/tinytest/test-tclass.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

0 comments on commit 6fcf2de

Please sign in to comment.