Skip to content

Commit

Permalink
Transform round_time() to a S3 generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvartan committed May 24, 2021
1 parent 5f7d970 commit dd91589
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ S3method(convert,data.frame)
S3method(convert,difftime)
S3method(convert,hms)
S3method(convert,numeric)
S3method(round_time,default)
S3method(round_time,difftime)
S3method(round_time,hms)
export(assign_date)
export(chronotype)
export(convert)
Expand Down
47 changes: 32 additions & 15 deletions R/round_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
#' round_time(lubridate::microseconds(123456789))
#' #> [1] "123S" # Expected
#'
#' as.difftime(12345.6789, units = "hours")
#' #> Time difference of 12345.68 hours # Expected
#' round_time(as.difftime(12345.6789, units = "hours"))
#' #> Time difference of 12346 hours # Expected
#' as.difftime(12345.6789, units = "secs")
#' #> Time difference of 12345.68 secs # Expected
#' round_time(as.difftime(12345.6789, units = "secs"))
#' #> Time difference of 12346 secs # Expected
#'
#' hms::as_hms(12345.6789)
#' #> 03:25:45.6789 # Expected
Expand All @@ -69,17 +69,34 @@ round_time <- function(x) {
classes <- c("Duration", "Period", "difftime", "hms", "POSIXct", "POSIXlt")
checkmate::assert_multi_class(x, classes)

class <- class(x)[1]
if (class == "difftime") units <- units(x)
if (class %in% c("POSIXct", "POSIXlt")) tz <- attributes(x)$tzone[1]
UseMethod("round_time")
}

#' @rdname round_time
#' @export
round_time.default <- function(x) {
round(x)
}

#' @rdname round_time
#' @export
round_time.difftime <- function(x) {
units <- units(x)
units(x) <- "secs"

x <- x %>% as.numeric() %>%
round() %>%
as.difftime(units = "secs")

x <- round(as.numeric(x))
units(x) <- units

if (class == "difftime") {
as.difftime(x, units = units)
} else if (class %in% c("POSIXct", "POSIXlt")) {
convert(x, class, tz = tz, quiet = TRUE)
} else {
convert(x, class, quiet = TRUE)
}
x
}

#' @rdname round_time
#' @export
round_time.hms <- function(x) {
x %>% as.numeric() %>%
round() %>%
hms::as_hms()
}
17 changes: 13 additions & 4 deletions man/round_time.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/test-round_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ test_that("round_time() | scalar test", {
expect_equal(object, expected)

object <- round_time(lubridate::microseconds(123456789))
expected <- lubridate::as.period(hms::hms(123))
expected <- round(lubridate::microseconds(123456789))
expect_equal(object, expected)

object <- round_time(as.difftime(12345.6789, units = "hours"))
expected <- as.difftime(12346, units = "hours")
object <- round_time(as.difftime(12345.6789, units = "secs"))
expected <- as.difftime(12346, units = "secs")
expect_equal(object, expected)

object <- round_time(hms::as_hms(12345.6789))
Expand Down

0 comments on commit dd91589

Please sign in to comment.