Skip to content

Commit 9240241

Browse files
author
maechler
committed
better re-balancing after subassigning POSIXlt
git-svn-id: https://svn.r-project.org/R/trunk@88441 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 8056c90 commit 9240241

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@
234234

235235
\item \code{hist(*, log = "x")} now works without a warning, thanks
236236
to \I{Martin Smith}'s \PR{18921}.
237+
238+
\item Subassigning \code{"POSIXlt"}, i.e., \code{<tdat>[i] <- val}
239+
and \code{<tdat>[[i]] <- val} now rebalance as they should,
240+
thanks to \I{Mikael Jagan}'s \PR{18919}.
237241
}
238242
}
239243
}

src/library/base/R/datetime.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,11 @@ function(x, units = c("secs", "mins", "hours", "days", "months", "years"))
13281328
ici <- is.character(i)
13291329
nms <- names(x$year)
13301330
if(mj) {
1331-
value <- unclass(as.POSIXlt(value))
1331+
tz <- attr(x, "tzone")
1332+
value <- unCfillPOSIXlt(
1333+
if(inherits(value, "POSIXlt") && identical(tz, attr(value, "tzone")))
1334+
value
1335+
else as.POSIXlt(as.POSIXct(value), tz = tz[1L]))
13321336
if(ici) {
13331337
for(n in names(x))
13341338
names(x[[n]]) <- nms
@@ -1562,15 +1566,19 @@ as.list.POSIXlt <- function(x, ...)
15621566
`[[<-.POSIXlt` <- function(x, i, value)
15631567
{
15641568
cl <- oldClass(x)
1565-
class(x) <- NULL
1569+
class(x) <- unCfillPOSIXlt(x)
15661570

15671571
if(!missing(i) && is.character(i)) {
15681572
nms <- names(x$year)
15691573
for(n in names(x))
15701574
names(x[[n]]) <- nms
15711575
}
15721576

1573-
value <- unCfillPOSIXlt(as.POSIXlt(value))
1577+
tz <- attr(x, "tzone")
1578+
value <- unCfillPOSIXlt(
1579+
if(inherits(value, "POSIXlt") && identical(tz, attr(value, "tzone")))
1580+
value
1581+
else as.POSIXlt(as.POSIXct(value), tz = tz[1L]))
15741582
for(n in names(x))
15751583
x[[n]][[i]] <- value[[n]]
15761584

tests/datetime3.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,21 @@ assertErrV(seq(to=to, by=by))
763763
assertErrV(seq(from, to, by=by, length.out=length.out))
764764

765765

766+
## subassignment to POSIXlt must reconcile time zones - PR#18919
767+
tz <- "America/Toronto"
768+
if(!tz %in% OlsonNames()) {
769+
cat(sprintf("%s not in time zone data base\n", tz))
770+
} else withAutoprint({
771+
(x <- x1 <- x2 <- as.POSIXlt(.POSIXct(0, tz = "UTC")))
772+
(y <- as.POSIXlt(.POSIXct(0, tz = tz)))
773+
x1[1L] <- x2[[1L]] <- y
774+
x1; x2
775+
stopifnot(identical(x1, x), identical(x2, x))
776+
})
777+
## x1, x2 were identical but differing from x
778+
779+
780+
766781
## keep at end
767782
rbind(last = proc.time() - .pt,
768783
total = proc.time())

0 commit comments

Comments
 (0)