Skip to content

Commit

Permalink
simplify leap_year() to support objects supported by year()
Browse files Browse the repository at this point in the history
  • Loading branch information
earowang committed Nov 20, 2019
1 parent 3102a6b commit c755112
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
10 changes: 4 additions & 6 deletions R/leap-years.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
#' leap_year(1900) # FALSE
#' leap_year(2000) # TRUE
leap_year <- function(date) {
recognized <- recognize(date)
if (recognized)
year <- year(date)
else if (all(is.numeric(date)))
if (is.numeric(date)) {
year <- date
else
stop("unrecognized date format")
} else {
year <- year(date)
}
(year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0))
}
8 changes: 0 additions & 8 deletions R/util.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ match_lengths <- function(x, y) {
list(x, y)
}

recognize <- function(x) {
recognized <- c("POSIXt", "POSIXlt", "POSIXct", "yearmon", "yearqtr", "Date")

if (all(class(x) %in% recognized))
return(TRUE)
return(FALSE)
}

standardise_date_names <- function(x) {
dates <- c("second", "minute", "hour", "mday", "wday", "yday", "day", "week", "month", "year", "tz")
y <- gsub("(.)s$", "\\1", x)
Expand Down

0 comments on commit c755112

Please sign in to comment.