Skip to content

Commit 60c64de

Browse files
authored
reinstate np loop but use truncation
1 parent 9c68536 commit 60c64de

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/library/base/R/datetime.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,19 @@ format.POSIXlt <- function(x, format = "", usetz = FALSE,
384384
if(!inherits(x, "POSIXlt")) stop("wrong class")
385385
if(any(f0 <- format == "" | grepl("%OS$", format))) {
386386
## need list [ method here.
387-
times <- unlist(unclass(x)[1L:3L])[f0]
387+
times <- unlist(unclass(x)[1L:3L])[f0]
388+
secs <- x$sec[f0]; secs <- secs[is.finite(secs)]
388389
np <- if(is.null(digits)) 0L else min(6L, digits)
389-
format[f0] <-
390-
if(all(times[is.finite(times)] == 0)) "%Y-%m-%d"
391-
else if(np == 0L) "%Y-%m-%d %H:%M:%S"
392-
else paste0("%Y-%m-%d %H:%M:%OS", np)
390+
if(np >= 1L) # no unnecessary trailing '0' :
391+
for (i in seq_len(np)- 1L)
392+
if(all( abs(secs - trunc(secs*10^i)/10^i) < 1e-6 )) {
393+
np <- i
394+
break
395+
}
396+
format[f0] <-
397+
if(all(times[is.finite(times)] == 0)) "%Y-%m-%d"
398+
else if(np == 0L) "%Y-%m-%d %H:%M:%S"
399+
else paste0("%Y-%m-%d %H:%M:%OS", np)
393400
}
394401
.Internal(format.POSIXlt(x, format, usetz))
395402
}

0 commit comments

Comments
 (0)