Skip to content

Commit c8d58f9

Browse files
author
maechler
committed
proper summary(<difftime>), printing by new format(*, with.units=FALSE); unique(); diff(); also fixing PR#18844
git-svn-id: https://svn.r-project.org/R/trunk@87666 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 2be09e1 commit c8d58f9

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

doc/NEWS.Rd

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@
182182
rounding. The current default \code{zdigits = 4L} is somewhat
183183
experimental. Specifying both \code{digits = *, zdigits = *} allows
184184
behaviour independent of the global \code{digits} option.
185+
186+
\item The \code{format()} method for \code{"difftime"} objects gets a
187+
new back compatible option \code{with.units}.
188+
189+
\item A \code{summary()} method for \code{"difftime"} objects which
190+
prints nicely, similar to those for \code{"Date"} and \code{"POSIXct"}.
185191
}
186192
}
187193
@@ -192,7 +198,7 @@
192198
flag \option{--with-C23}.
193199
194200
It is intended that this will become the default before release.
195-
201+
196202
\item The minimum \command{autoconf} requirement for a maintainer
197203
build has been increased to \command{autoconf}\sspace{}2.72.
198204
@@ -356,7 +362,7 @@
356362
\code{Rf_*()} as has been documented in
357363
\sQuote{Writing R Extensions} for a while, fixing \PR{18800}
358364
thanks to \I{Mikael Jagan} and \I{Suharto Anggono}.
359-
365+
360366
\item \code{R_GetCurrentSrcref(skip)} now skips calls rather
361367
than \code{srcref}s, consistent with counting items in the
362368
\code{traceback()} display. If \code{skip == NA_INTEGER},
@@ -519,13 +525,13 @@
519525
\item \code{summary(<stl>)} (which prints directly) finally gets the
520526
same \code{digits} default as the formatting printing of default
521527
\code{summary()} method results, and it is documented explicitly.
522-
528+
523529
\item \code{options(show.error.locations = TRUE)} once
524530
again shows the most recent known location when an
525531
error is reported. Setting its value to \code{"bottom"}
526532
is no longer supported. Numerical values are converted
527533
to logical.
528-
534+
529535
\item C API function \code{R_GetCurrentSrcref(skip)} now
530536
returns \code{srcref} entries correctly. (Note that there
531537
is also a change to the interpretation of \code{skip};

src/library/base/R/datetime.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ summary.POSIXct <- function(object, digits = 15L, ...)
465465
}
466466
.POSIXct(x,
467467
tz = attr(object, "tzone"),
468-
cl = c("summaryDefault", "table", oldClass(object)))
468+
cl = c("summaryDefault", oldClass(object)))
469469
}
470470

471471
summary.POSIXlt <- function(object, digits = 15, ...)
@@ -769,10 +769,10 @@ as.double.difftime <- function(x, units = "auto", ...)
769769

770770
as.data.frame.difftime <- as.data.frame.vector
771771

772-
format.difftime <- function(x,...)
772+
format.difftime <- function(x,..., with.units = TRUE)
773773
{
774774
y <- if(length(x))
775-
paste(format(unclass(x),...), units(x))
775+
paste0(format(unclass(x),...), if(with.units) paste0(" ",units(x)))
776776
else
777777
character()
778778
names(y) <- names(x)
@@ -786,7 +786,7 @@ print.difftime <- function(x, digits = getOption("digits"), ...)
786786
else if(is.array(x) || length(x) > 1L) {
787787
cat("Time differences in ", attr(x, "units"), "\n", sep = "")
788788
y <- unclass(x); attr(y, "units") <- NULL
789-
print(y, digits=digits, ...)
789+
print(y, digits=digits, ...)
790790
}
791791
else
792792
cat("Time difference of ", format(unclass(x), digits = digits), " ",
@@ -952,8 +952,9 @@ function(object, digits = getOption("digits"), ...)
952952
x <- x[-m]
953953
attr(x, "NAs") <- NAs
954954
}
955-
.difftime(x, attr(object, "units"), oldClass(object))
956-
}
955+
.difftime(x, attr(object, "units"),
956+
c("summaryDefault", oldClass(object)))
957+
}
957958

958959
## ----- convenience functions -----
959960

src/library/base/R/summary.R

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ format.summaryDefault <- function(x, digits = max(3L, getOption("digits") - 3L),
7676
}
7777
class(xx) <- class(x)[-1]
7878
m <- match("NA's", names(x), 0)
79-
if(inherits(x, "Date") || inherits(x, "POSIXct")) {
80-
if(length(a <- attr(x, "NAs")))
81-
c(format(xx, digits=digits, ...), "NA's" = as.character(a))
82-
else format(xx, digits=digits)
79+
if((iD <- inherits(x, "Date")) | (iP <- inherits(x, "POSIXct")) || inherits(x, "difftime")) {
80+
c(format(xx, digits = if(iP) 0L else digits),
81+
"NA's" = if(length(a <- attr(x, "NAs"))) as.character(a))
8382
} else if(m && !is.character(x))
8483
c(format(xx[-m], digits=digits, ...), "NA's" = as.character(xx[m]))
8584
else format(xx, digits=digits, ...)
@@ -93,14 +92,20 @@ print.summaryDefault <- function(x, digits = max(3L, getOption("digits") - 3L),
9392
xx[finite] <- zapsmall(x[finite], digits = digits + zdigits)
9493
}
9594
class(xx) <- class(x)[-1] # for format
96-
m <- match("NA's", names(xx), 0)
97-
if(inherits(x, "Date") || inherits(x, "POSIXct")) {
98-
xx <- if(length(a <- attr(x, "NAs")))
99-
c(format(xx, digits=digits), "NA's" = as.character(a))
100-
else format(xx, digits=digits)
101-
print(xx, digits=digits, ...)
95+
if((iD <- inherits(x, "Date")) | (iP <- inherits(x, "POSIXct")) || inherits(x, "difftime")) {
96+
no.q <- is.na(match("quote", ...names())) # no 'quote = *' in `...`
97+
if(no.q) quote <- TRUE
98+
if(iP)
99+
digits <- 0L
100+
else if(!iD) { # have difftime
101+
cat("Time differences in ", attr(x, "units"), "\n", sep = "")
102+
if(no.q) quote <- FALSE
103+
}
104+
xx <- c(format(xx, digits = digits, with.units = FALSE),
105+
"NA's" = if(length(a <- attr(x, "NAs"))) as.character(a))
106+
print(xx, quote = quote, ...)
102107
return(invisible(x))
103-
} else if(m && !is.character(x))
108+
} else if((m <- match("NA's", names(xx), 0L)) && !is.character(x))
104109
xx <- c(format(xx[-m], digits=digits), "NA's" = as.character(xx[m]))
105110
print.table(xx, digits=digits, ...)
106111
invisible(x)

src/library/base/man/difftime.Rd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/base/man/difftime.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2024 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{difftime}
@@ -41,7 +41,7 @@ difftime(time1, time2, tz,
4141

4242
as.difftime(tim, format = "\%X", units = "auto", tz = "UTC")
4343

44-
\method{format}{difftime}(x, ...)
44+
\method{format}{difftime}(x, ..., with.units = TRUE)
4545
\method{units}{difftime}(x)
4646
\method{units}{difftime}(x) <- value
4747
\method{as.double}{difftime}(x, units = "auto", ...)
@@ -63,6 +63,9 @@ as.difftime(tim, format = "\%X", units = "auto", tz = "UTC")
6363
\code{\link{strptime}}. The default is a locale-specific time format.}
6464
\item{x}{an object inheriting from class \code{"difftime"}.}
6565
\item{\dots}{arguments to be passed to or from other methods.}
66+
\item{with.units}{(for the \code{format()} method:) logical indicating
67+
the units should be part, e.g., "3.5 hours"; if false, the units are
68+
suppressed.}
6669
}
6770
\details{
6871
Function \code{difftime} calculates a difference of two date/time

tests/reg-tests-1e.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,24 @@ stopifnot(is.table(sdf), is.matrix(sdf), identical(dim(sdf), c(6L, 2L)))
17821782
## failed for a few days only
17831783

17841784

1785+
## summary(<difftime>) and its print()ing
1786+
xt <- .POSIXct(1737745992 + 2/7 + 10000 * (0:7))
1787+
(dt <- diff(xt)) # |--> diff.POSIXt() -- perfect
1788+
(sdt <- summary(dt))
1789+
stopifnot(exprs = {
1790+
inherits(dt, "difftime")
1791+
inherits(sdt, "difftime")
1792+
inherits(diff(sdt), "difftime")
1793+
diff(sdt) == 0
1794+
inherits(sdt, "summaryDefault")
1795+
identical(capture.output(sdt), c(
1796+
"Time differences in hours",
1797+
" Min. 1st Qu. Median Mean 3rd Qu. Max. ",
1798+
strrep(' 2.778 ', 6)))
1799+
})
1800+
## summary(<difftime>) was not useful in R < 4.5.0
1801+
1802+
17851803

17861804
## keep at end
17871805
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)