Skip to content

Commit 36b2f1f

Browse files
author
maechler
committed
print.POSIX*t(.., digits = *) passed to format...()
git-svn-id: https://svn.r-project.org/R/trunk@87363 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 75f3824 commit 36b2f1f

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

doc/NEWS.Rd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@
121121
\item The \code{sunspot.month} data have been updated to Oct 2024;
122122
because of recalibration also historical numbers are changed, and we
123123
keep the previous data as \code{sunspot.m2014} for reproducibility.
124+
125+
\item The \code{print()} method for date-time objects (\code{POSIX.t})
126+
gets an optional \code{digits} argument for \emph{fractional}
127+
seconds, passed to improved \code{format.POSIXlt()}; consequently,
128+
\code{print(<date.time>, digits = n)} allows to print fractions of
129+
seconds.
124130
}
125131
}
126132

@@ -369,11 +375,11 @@
369375

370376
\item \code{debug()} and \code{debugonce(fun)} now also accept a
371377
string \code{fun} when it names an S4 generic, fixing \PR{18822}
372-
thanks to \I{Michael Jagan}.
378+
thanks to \I{Mikael Jagan}.
373379

374380
\item \code{debugonce(<S4-simple-body>, signature=*)} now works
375381
correctly when \dQuote{called twice}, fixing \PR{18824} thanks to
376-
\I{Michael Jagan}.
382+
\I{Mikael Jagan}.
377383

378384
\item \code{format(dtime, digits=* / format=*)} is more consistent
379385
when the \code{POSIXt} date-time object \code{dtime} has fractional

src/library/base/R/datetime.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,13 @@ format.POSIXct <- function(x, format = "", tz = "", usetz = FALSE, ...)
426426

427427
## keep in sync with print.Date() in ./dates.R
428428
print.POSIXct <-
429-
print.POSIXlt <- function(x, tz = "", usetz = TRUE, max = NULL, ...)
429+
print.POSIXlt <- function(x, tz = "", usetz = TRUE, max = NULL,
430+
digits = getOption("digits.secs"), ...)
430431
{
431432
if(is.null(max)) max <- getOption("max.print", 9999L)
432433
FORM <- if(missing(tz))
433-
function(z) format(z, usetz = usetz)
434-
else function(z) format(z, tz = tz, usetz = usetz)
434+
function(z) format(z, usetz=usetz, digits=digits)
435+
else function(z) format(z, tz=tz, usetz=usetz, digits=digits)
435436
if(max < length(x)) {
436437
print(FORM(x[seq_len(max)]), max=max+1, ...)
437438
cat(" [ reached 'max' / getOption(\"max.print\") -- omitted",

src/library/base/man/DateTimeClasses.Rd

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
representing calendar dates and times.
6161
}
6262
\usage{
63-
\method{print}{POSIXct}(x, tz = "", usetz = TRUE, max = NULL, \dots)
63+
\method{print}{POSIXct}(x, tz = "", usetz = TRUE, max = NULL,
64+
digits = getOption("digits.prec"), \dots)
6465

6566
\method{summary}{POSIXct}(object, digits = 15, \dots)
6667

@@ -76,7 +77,9 @@
7677
\item{max}{numeric or \code{NULL}, specifying the maximal number of
7778
entries to be printed. By default, when \code{NULL},
7879
\code{\link{getOption}("max.print")} used.}
79-
\item{digits}{number of significant digits for the computations:
80+
\item{digits}{number of digits to format fractional seconds in the case
81+
of \code{print()};
82+
number of \emph{significant} digits for the computations:
8083
should be high enough to represent the least important time unit
8184
exactly.}
8285
\item{\dots}{further arguments to be passed from or to other methods.}
@@ -337,16 +340,14 @@
337340
must not be changed, as several C-based conversion methods rely on the
338341
order for efficiency.
339342
}
340-
\examples{
341-
\dontdiff{
343+
\examples{\dontdiff{
342344
(z <- Sys.time()) # the current date, as class "POSIXct"
343345

344346
Sys.time() - 3600 # an hour ago
345347

346348
as.POSIXlt(Sys.time(), "GMT") # the current time in GMT
347-
format(.leap.seconds) # the leap seconds in your time zone
349+
format(.leap.seconds) # the leap seconds in your time zone}
348350
print(.leap.seconds, tz = "America/Los_Angeles") # and in Seattle's
349-
}
350351

351352
## look at *internal* representation of "POSIXlt" :
352353
leapS <- as.POSIXlt(.leap.seconds)

tests/datetime3.R

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ stopifnot(identical(fx, format(x.))) # *are* the same (for a while now)
622622
fD.OS <- function(d) format(x, format = paste0("%Y-%m-%d %H:%M:%OS", if(d=="_") "" else d))
623623
f.OSss <- vapply(c("_",0:6), fD.OS, character(length(x)))
624624
t(f.OSss) |> print(width=111, quote=FALSE) # shows 'trunc()' instead of 'round()'
625-
stopifnot(identical(f.OSss[,"_"], f.OSss[,"6"])) # by option digits.secs
625+
stopifnot(identical(f.OSss[,"_"], f.OSss[,"6"])) # by option digits.secs
626626
(secDig <- sub(".*:59", '', f.OSss)) ## [,"1"] is *.0 *.0 *.2 *.2 - "bad" from using trunc() by design
627627
## ___________ ___ __ __ "factory fresh" default
628628
options(digits.secs = NULL, scipen = 0, digits = 7)
@@ -638,6 +638,23 @@ stopifnot(exprs = {
638638
options(op)
639639
## Number of digits used differed in several cases in R <= 4.4.z
640640

641+
## print(*, digits = n) now works, too -- compatibly with format()
642+
print(x, digits = 6) # shows full digits
643+
xx <- as.POSIXct("2009-08-03 12:01:59") + 0:8/8 # rare exact dbl prec
644+
j <- 2L*(1:4) -1L # 1,3,5,7
645+
(fxx2 <- format(xx, digits = 2)[j])
646+
print(xx, digits = 5, usetz=FALSE)# needs only 3 digits
647+
pxx <- capture.output(split = TRUE,
648+
print(xx[j], digits = 5, width=140)) # 2 digits suffice
649+
stopifnot(exprs = {
650+
identical(format(x, digits = 6) |> paste(collapse=' '), sub('^\\[1\\] ', '', gsub('"', '', capture.output(
651+
print (x, digits = 6, usetz = FALSE, width = 120)))))
652+
is.integer(nf2 <- nchar(fxx2))
653+
substr (fxx2, nf2-1L, nf2) == c("00", "25", "50", "75")
654+
identical(fxx2, sub(" [A-Z].*$", '',
655+
strsplit(split = "@",
656+
gsub('" "', '@', sub(".$", '', sub('^\\[1\\] "', '', pxx))))[[1L]]))
657+
})
641658

642659

643660
## keep at end

0 commit comments

Comments
 (0)