Skip to content

Commit

Permalink
Basics
Browse files Browse the repository at this point in the history
- Use _vctrs_ (#61).
- New `new_hms()`, currently doesn't use the class defined by vctrs.
  • Loading branch information
krlmlr committed Jan 15, 2019
1 parent 16ff76e commit 515c0d9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Description: Implements an S3 class for storing and formatting time-of-day
Imports:
methods,
pkgconfig,
rlang
rlang,
vctrs
Suggests:
crayon,
lubridate,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export(as.hms)
export(hms)
export(is.hms)
export(is_hms)
export(new_hms)
export(parse_hm)
export(parse_hms)
export(round_hms)
export(trunc_hms)
import(rlang)
import(vctrs)
importFrom(methods,setOldClass)
importFrom(pkgconfig,get_config)
22 changes: 21 additions & 1 deletion R/hms.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#' @import vctrs
#' @import rlang
NULL

#' @importFrom methods setOldClass
setOldClass(c("hms", "difftime"))
#setOldClass(c("hms", "vctrs_vctr", "difftime"))

#' A simple class for storing time-of-day values
#'
Expand All @@ -14,6 +18,8 @@ setOldClass(c("hms", "difftime"))
#' @examples
#' hms(56, 34, 12)
#' hms()
#' new_hms(1:3)
#'
#' as.hms(1)
#' as.hms("12:34:56")
#' as.hms(Sys.time())
Expand All @@ -40,7 +46,21 @@ hms <- function(seconds = NULL, minutes = NULL, hours = NULL, days = NULL) {
secs <- reduce(arg_secs[!map_lgl(args, is.null)], `+`)
if (is.null(secs)) secs <- numeric()

as.hms(as.difftime(secs, units = "secs"))
new_hms(secs)
}

#' @rdname hms
#' @export
new_hms <- function(x = numeric()) {
vec_assert(x, numeric())

out <- new_vctr(x, units = "secs", class = c("hms", "difftime"))

# Move difftime class to the beginning, so that vctrs methods override
# base methods
class(out) <- c("hms", "difftime")
#class(out) <- c(setdiff(class(out), "difftime"), "difftime")
out
}

#' @rdname hms
Expand Down
5 changes: 5 additions & 0 deletions man/hms.Rd

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

0 comments on commit 515c0d9

Please sign in to comment.