Skip to content

Commit 56ae399

Browse files
author
maechler
committed
cosmetic ... speedup; fix indentation (84928)
git-svn-id: https://svn.r-project.org/R/trunk@87353 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 609d977 commit 56ae399

File tree

1 file changed

+80
-81
lines changed

1 file changed

+80
-81
lines changed

src/library/base/R/dates.R

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ as.Date.character <- function(x, format,
5151
optional = FALSE, ...)
5252
{
5353
charToDate <- function(x) {
54-
is.na(x) <- !nzchar(x) # PR#17909
55-
xx <- x[1L]
54+
is.na(x) <- !nzchar(x) # PR#17909
55+
xx <- x[1L]
5656
if(is.na(xx)) {
5757
j <- 1L
5858
while(is.na(xx) && (j <- j+1L) <= length(x)) xx <- x[j]
5959
if(is.na(xx)) f <- "%Y-%m-%d" # all NAs
6060
}
61-
if(is.na(xx))
61+
if(is.na(xx))
6262
strptime(x, f)
6363
else {
6464
for(ff in tryFormats)
@@ -80,16 +80,16 @@ as.Date.numeric <- function(x, origin, ...)
8080
as.Date.default <- function(x, ...)
8181
{
8282
if(inherits(x, "Date"))
83-
x
83+
x
8484
else if(is.null(x))
8585
.Date(numeric())
8686
else if(is.logical(x) && all(is.na(x)))
87-
.Date(as.numeric(x))
87+
.Date(as.numeric(x))
8888
else
89-
stop(gettextf("do not know how to convert '%s' to class %s",
90-
deparse1(substitute(x)),
91-
dQuote("Date")),
92-
domain = NA)
89+
stop(gettextf("do not know how to convert '%s' to class %s",
90+
deparse1(substitute(x)),
91+
dQuote("Date")),
92+
domain = NA)
9393
}
9494

9595
## ## Moved to package date
@@ -123,13 +123,13 @@ print.Date <- function(x, max = NULL, ...)
123123
{
124124
if(is.null(max)) max <- getOption("max.print", 9999L)
125125
if(max < length(x)) {
126-
print(format(x[seq_len(max)]), max=max+1, ...)
127-
cat(" [ reached 'max' / getOption(\"max.print\") -- omitted",
128-
length(x) - max, 'entries ]\n')
126+
print(format(x[seq_len(max)]), max=max+1, ...)
127+
cat(" [ reached 'max' / getOption(\"max.print\") -- omitted",
128+
length(x) - max, 'entries ]\n')
129129
} else if(length(x))
130-
print(format(x), max = max, ...)
131-
else
132-
cat(class(x)[1L], "of length 0\n")
130+
print(format(x), max = max, ...)
131+
else
132+
cat(class(x)[1L], "of length 0\n")
133133
invisible(x)
134134
}
135135

@@ -203,7 +203,7 @@ Summary.Date <- function (..., na.rm)
203203
ok <- switch(.Generic, max = , min = , range = TRUE, FALSE)
204204
if (!ok) stop(gettextf("%s not defined for \"Date\" objects", .Generic),
205205
domain = NA)
206-
.Date(NextMethod(.Generic), oldClass(list(...)[[1L]]))
206+
.Date(NextMethod(.Generic), oldClass(...elt(1L)))
207207
}
208208

209209
`[.Date` <- function(x, ..., drop = TRUE)
@@ -329,84 +329,84 @@ cut.Date <-
329329
x <- as.Date(x)
330330

331331
if (inherits(breaks, "Date")) {
332-
breaks <- sort(as.Date(breaks))
332+
breaks <- sort(as.Date(breaks))
333333
} else if(is.numeric(breaks) && length(breaks) == 1L) {
334-
## specified number of breaks
334+
## specified number of breaks
335335
} else if(is.character(breaks) && length(breaks) == 1L) {
336-
by2 <- strsplit(breaks, " ", fixed = TRUE)[[1L]]
337-
if(length(by2) > 2L || length(by2) < 1L)
338-
stop("invalid specification of 'breaks'")
339-
valid <-
340-
pmatch(by2[length(by2)],
341-
c("days", "weeks", "months", "years", "quarters"))
342-
if(is.na(valid)) stop("invalid specification of 'breaks'")
343-
start <- as.POSIXlt(min(x, na.rm=TRUE))
344-
if(valid == 1L) incr <- 1L
345-
if(valid == 2L) { # weeks
346-
start$mday <- start$mday - start$wday
347-
if(start.on.monday)
348-
start$mday <- start$mday + ifelse(start$wday > 0L, 1L, -6L)
336+
by2 <- strsplit(breaks, " ", fixed = TRUE)[[1L]]
337+
if(length(by2) > 2L || length(by2) < 1L)
338+
stop("invalid specification of 'breaks'")
339+
valid <-
340+
pmatch(by2[length(by2)],
341+
c("days", "weeks", "months", "years", "quarters"))
342+
if(is.na(valid)) stop("invalid specification of 'breaks'")
343+
start <- as.POSIXlt(min(x, na.rm=TRUE))
344+
if(valid == 1L) incr <- 1L
345+
if(valid == 2L) { # weeks
346+
start$mday <- start$mday - start$wday
347+
if(start.on.monday)
348+
start$mday <- start$mday + ifelse(start$wday > 0L, 1L, -6L)
349349
start$isdst <- -1L
350-
incr <- 7L
351-
}
352-
if(valid == 3L) { # months
353-
start$mday <- 1L
350+
incr <- 7L
351+
}
352+
if(valid == 3L) { # months
353+
start$mday <- 1L
354354
start$isdst <- -1L
355355
maxx <- max(x, na.rm = TRUE)
356-
end <- as.POSIXlt(maxx)
357-
step <- if(length(by2) == 2L) as.integer(by2[1L]) else 1L
358-
end <- as.POSIXlt(end + (31 * step * 86400))
359-
end$mday <- 1L
356+
end <- as.POSIXlt(maxx)
357+
step <- if(length(by2) == 2L) as.integer(by2[1L]) else 1L
358+
end <- as.POSIXlt(end + (31 * step * 86400))
359+
end$mday <- 1L
360360
end$isdst <- -1L
361-
breaks <- as.Date(seq(start, end, breaks))
361+
breaks <- as.Date(seq(start, end, breaks))
362362
## 31 days ahead could give an empty level, so
363-
lb <- length(breaks)
364-
if(maxx < breaks[lb-1]) breaks <- breaks[-lb]
365-
} else if(valid == 4L) { # years
366-
start$mon <- 0L
367-
start$mday <- 1L
363+
lb <- length(breaks)
364+
if(maxx < breaks[lb-1]) breaks <- breaks[-lb]
365+
} else if(valid == 4L) { # years
366+
start$mon <- 0L
367+
start$mday <- 1L
368368
start$isdst <- -1L
369369
maxx <- max(x, na.rm = TRUE)
370-
end <- as.POSIXlt(maxx)
371-
step <- if(length(by2) == 2L) as.integer(by2[1L]) else 1L
372-
end <- as.POSIXlt(end + (366 * step * 86400))
373-
end$mon <- 0L
374-
end$mday <- 1L
370+
end <- as.POSIXlt(maxx)
371+
step <- if(length(by2) == 2L) as.integer(by2[1L]) else 1L
372+
end <- as.POSIXlt(end + (366 * step * 86400))
373+
end$mon <- 0L
374+
end$mday <- 1L
375375
end$isdst <- -1L
376-
breaks <- as.Date(seq(start, end, breaks))
376+
breaks <- as.Date(seq(start, end, breaks))
377377
## 366 days ahead could give an empty level, so
378-
lb <- length(breaks)
379-
if(maxx < breaks[lb-1]) breaks <- breaks[-lb]
380-
} else if(valid == 5L) { # quarters
381-
qtr <- rep(c(0L, 3L, 6L, 9L), each = 3L)
382-
start$mon <- qtr[start$mon + 1L]
383-
start$mday <- 1L
378+
lb <- length(breaks)
379+
if(maxx < breaks[lb-1]) breaks <- breaks[-lb]
380+
} else if(valid == 5L) { # quarters
381+
qtr <- rep(c(0L, 3L, 6L, 9L), each = 3L)
382+
start$mon <- qtr[start$mon + 1L]
383+
start$mday <- 1L
384384
start$isdst <- -1L
385-
maxx <- max(x, na.rm = TRUE)
386-
end <- as.POSIXlt(maxx)
387-
step <- if(length(by2) == 2L) as.integer(by2[1L]) else 1L
388-
end <- as.POSIXlt(end + (93 * step * 86400))
389-
end$mon <- qtr[end$mon + 1L]
390-
end$mday <- 1L
385+
maxx <- max(x, na.rm = TRUE)
386+
end <- as.POSIXlt(maxx)
387+
step <- if(length(by2) == 2L) as.integer(by2[1L]) else 1L
388+
end <- as.POSIXlt(end + (93 * step * 86400))
389+
end$mon <- qtr[end$mon + 1L]
390+
end$mday <- 1L
391391
end$isdst <- -1L
392-
breaks <- as.Date(seq(start, end, paste(step * 3L, "months")))
393-
## 93 days ahead could give an empty level, so
394-
lb <- length(breaks)
395-
if(maxx < breaks[lb-1]) breaks <- breaks[-lb]
396-
} else {
397-
start <- as.Date(start)
398-
if (length(by2) == 2L) incr <- incr * as.integer(by2[1L])
399-
maxx <- max(x, na.rm = TRUE)
400-
breaks <- seq(start, maxx + incr, breaks)
401-
breaks <- breaks[seq_len(1L+max(which(breaks <= maxx)))]
402-
}
392+
breaks <- as.Date(seq(start, end, paste(step * 3L, "months")))
393+
## 93 days ahead could give an empty level, so
394+
lb <- length(breaks)
395+
if(maxx < breaks[lb-1]) breaks <- breaks[-lb]
396+
} else {
397+
start <- as.Date(start)
398+
if (length(by2) == 2L) incr <- incr * as.integer(by2[1L])
399+
maxx <- max(x, na.rm = TRUE)
400+
breaks <- seq(start, maxx + incr, breaks)
401+
breaks <- breaks[seq_len(1L+max(which(breaks <= maxx)))]
402+
}
403403
} else stop("invalid specification of 'breaks'")
404404
res <- cut(unclass(x), unclass(breaks), labels = labels,
405-
right = right, ...)
405+
right = right, ...)
406406
if(is.null(labels)) {
407-
levels(res) <-
408-
as.character(if (is.numeric(breaks)) x[!duplicated(res)]
409-
else breaks[-length(breaks)])
407+
levels(res) <-
408+
as.character(if (is.numeric(breaks)) x[!duplicated(res)]
409+
else breaks[-length(breaks)])
410410
}
411411
res
412412
}
@@ -465,9 +465,8 @@ diff.Date <- function (x, lag = 1L, differences = 1L, ...)
465465
r[-nrow(r):-(nrow(r) - lag + 1L), , drop = FALSE]
466466
else for (i in seq_len(differences))
467467
r <- r[i1] - r[-length(r):-(length(r) - lag + 1L)]
468-
dots <- list(...)
469-
if("units" %in% names(dots) && dots$units != "auto")
470-
units(r) <- match.arg(dots$units, choices = setdiff(eval(formals(difftime)$units), "auto"))
468+
if("units" %in% ...names() && (dunits <- list(...)$units) != "auto")
469+
units(r) <- match.arg(dunits, choices = setdiff(eval(formals(difftime)$units), "auto"))
471470
r
472471
}
473472

0 commit comments

Comments
 (0)