Skip to content

Commit

Permalink
Update roxygen2 and register internal S3 methods as suggested (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan authored Dec 3, 2024
1 parent b591ca3 commit dfdfd32
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ URL: https://github.com/business-science/tibbletime
BugReports: https://github.com/business-science/tibbletime/issues
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Depends:
R (>= 3.4.0)
Expand Down
42 changes: 42 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,26 @@ S3method(as_period,tbl_time)
S3method(as_tbl_time,default)
S3method(as_tbl_time,tbl_df)
S3method(as_tibble,tbl_time)
S3method(assert_period_matches_index_class,Date)
S3method(assert_period_matches_index_class,POSIXct)
S3method(assert_period_matches_index_class,default)
S3method(assert_period_matches_index_class,hms)
S3method(assert_period_matches_index_class,yearmon)
S3method(assert_period_matches_index_class,yearqtr)
S3method(ceiling_index,default)
S3method(ceiling_index,hms)
S3method(ceiling_index,yearmon)
S3method(ceiling_index,yearqtr)
S3method(coerce_start_date,Date)
S3method(coerce_start_date,POSIXct)
S3method(coerce_start_date,hms)
S3method(coerce_start_date,yearmon)
S3method(coerce_start_date,yearqtr)
S3method(dispatch_to_datetime,Date)
S3method(dispatch_to_datetime,default)
S3method(dispatch_to_datetime,hms)
S3method(dispatch_to_datetime,yearmon)
S3method(dispatch_to_datetime,yearqtr)
S3method(distinct,tbl_time)
S3method(filter,tbl_time)
S3method(filter_time,default)
Expand All @@ -25,14 +41,34 @@ S3method(full_join,tbl_time)
S3method(group_by,tbl_time)
S3method(inner_join,tbl_time)
S3method(left_join,tbl_time)
S3method(list_to_datetime,Date)
S3method(list_to_datetime,POSIXct)
S3method(list_to_datetime,hms)
S3method(list_to_datetime,yearmon)
S3method(list_to_datetime,yearqtr)
S3method(lookup_defaults,Date)
S3method(lookup_defaults,POSIXct)
S3method(lookup_defaults,hms)
S3method(lookup_defaults,yearmon)
S3method(lookup_defaults,yearqtr)
S3method(lookup_seq_fun,Date)
S3method(lookup_seq_fun,POSIXct)
S3method(lookup_seq_fun,hms)
S3method(lookup_seq_fun,yearmon)
S3method(lookup_seq_fun,yearqtr)
S3method(mutate,tbl_time)
S3method(mutate_,tbl_time)
S3method(parse_period,character)
S3method(parse_period,default)
S3method(push_datetime,default)
S3method(push_datetime,hms)
S3method(reconstruct,tbl_time)
S3method(right_join,tbl_time)
S3method(select,tbl_time)
S3method(semi_join,tbl_time)
S3method(seq,hms)
S3method(seq,yearmon)
S3method(seq,yearqtr)
S3method(slice,tbl_time)
S3method(slice_,tbl_time)
S3method(split_to_list,Date)
Expand All @@ -46,6 +82,12 @@ S3method(summarise,tbl_time)
S3method(summarise_,tbl_time)
S3method(summarize_,tbl_time)
S3method(tbl_sum,tbl_time)
S3method(to_posixct_numeric,Date)
S3method(to_posixct_numeric,POSIXct)
S3method(to_posixct_numeric,default)
S3method(to_posixct_numeric,hms)
S3method(to_posixct_numeric,yearmon)
S3method(to_posixct_numeric,yearqtr)
S3method(transmute,tbl_time)
S3method(ungroup,tbl_time)
export("%>%")
Expand Down
28 changes: 28 additions & 0 deletions R/index-based-generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lookup_seq_fun <- function(x) {
UseMethod("lookup_seq_fun")
}

#' @export
lookup_seq_fun.POSIXct <- function(x) {

# For POSIXct object, DST can cause problems with
Expand All @@ -31,18 +32,22 @@ lookup_seq_fun.POSIXct <- function(x) {
}
}

#' @export
lookup_seq_fun.Date <- function(x) {
seq.Date
}

#' @export
lookup_seq_fun.yearmon <- function(x) {
seq.yearmon
}

#' @export
lookup_seq_fun.yearqtr <- function(x) {
seq.yearqtr
}

#' @export
lookup_seq_fun.hms <- function(x) {
seq.hms
}
Expand All @@ -55,6 +60,7 @@ push_datetime <- function(x, push) {
UseMethod("push_datetime")
}

#' @export
push_datetime.default <- function(x, push) {

x_num <- to_posixct_numeric(x)
Expand All @@ -69,6 +75,7 @@ push_datetime.default <- function(x, push) {
)
}

#' @export
push_datetime.hms <- function(x, push) {
hms::as_hms(push_datetime.default(x, push))
}
Expand All @@ -84,35 +91,41 @@ assert_period_matches_index_class <- function(x, period) {
UseMethod("assert_period_matches_index_class")
}

#' @export
assert_period_matches_index_class.default <- function(x, period) {
glue_stop("Class '{class(x)}' is not a known index class.")
}

#' @export
assert_period_matches_index_class.POSIXct <- function(x, period) {
return()
}

#' @export
assert_period_matches_index_class.Date <- function(x, period) {
assertthat::assert_that(
period %in% c("year", "quarter", "month", "week", "day"),
msg = "Only year, quarter, month, week, and day periods are allowed for an index of class Date"
)
}

#' @export
assert_period_matches_index_class.yearmon <- function(x, period) {
assertthat::assert_that(
period %in% c("year", "quarter", "month"),
msg = "Only year, quarter, and month periods are allowed for an index of class yearmon"
)
}

#' @export
assert_period_matches_index_class.yearqtr <- function(x, period) {
assertthat::assert_that(
period %in% c("year", "quarter"),
msg = "Only year and quarter periods are allowed for an index of class yearqtr"
)
}

#' @export
assert_period_matches_index_class.hms <- function(x, period) {
assertthat::assert_that(
period %in% c("hour", "min", "sec"),
Expand Down Expand Up @@ -203,30 +216,35 @@ lookup_defaults <- function(index, side = "lhs") {
UseMethod("lookup_defaults")
}

#' @export
lookup_defaults.POSIXct <- function(index, side = "lhs") {
switch(side,
"lhs" = list(y = 1970, m = 01, d = 01, h = 00, M = 00, s = 00),
"rhs" = list(y = 1970, m = 12, d = 00, h = 23, M = 59, s = 59))
}

#' @export
lookup_defaults.Date <- function(index, side = "lhs") {
switch(side,
"lhs" = list(y = 1970, m = 01, d = 01),
"rhs" = list(y = 1970, m = 12, d = 00))
}

#' @export
lookup_defaults.yearmon <- function(index, side = "lhs") {
switch(side,
"lhs" = list(y = 1970, m = 01),
"rhs" = list(y = 1970, m = 12))
}

#' @export
lookup_defaults.yearqtr <- function(index, side = "lhs") {
switch(side,
"lhs" = list(y = 1970, q = 01),
"rhs" = list(y = 1970, q = 04))
}

#' @export
lookup_defaults.hms <- function(index, side = "lhs") {
switch(side,
"lhs" = list(h = 00, M = 00, s = 00),
Expand All @@ -241,25 +259,30 @@ list_to_datetime <- function(index, tf_side, ...) {
UseMethod("list_to_datetime")
}

#' @export
list_to_datetime.POSIXct <- function(index, tf_side, tz, ...) {
lubridate::make_datetime(tf_side$y, tf_side$m, tf_side$d,
tf_side$h, tf_side$M, tf_side$s, tz = tz)
}

#' @export
list_to_datetime.Date <- function(index, tf_side, ...) {
lubridate::make_date(tf_side$y, tf_side$m, tf_side$d)
}

#' @export
list_to_datetime.yearmon <- function(index, tf_side, ...) {
tf_side$d <- 1
zoo::as.yearmon(list_to_datetime.Date(index, tf_side))
}

#' @export
list_to_datetime.yearqtr <- function(index, tf_side, ...) {
yearqtr_string <- paste0(tf_side$y, "-", tf_side$q)
zoo::as.yearqtr(yearqtr_string)
}

#' @export
list_to_datetime.hms <- function(index, tf_side, ...) {
hms::hms(seconds = tf_side$s, minutes = tf_side$M, hours = tf_side$h)
}
Expand All @@ -274,23 +297,28 @@ coerce_start_date <- function(x, start_date) {
UseMethod("coerce_start_date")
}

#' @export
coerce_start_date.POSIXct <- function(x, start_date) {
tz <- get_index_col_time_zone(x)
as.POSIXct(start_date, tz = tz)
}

#' @export
coerce_start_date.Date <- function(x, start_date) {
as.Date(start_date)
}

#' @export
coerce_start_date.yearmon <- function(x, start_date) {
zoo::as.yearmon(start_date)
}

#' @export
coerce_start_date.yearqtr <- function(x, start_date) {
zoo::as.yearqtr(start_date)
}

#' @export
coerce_start_date.hms <- function(x, start_date) {
hms::as_hms(start_date)
}
3 changes: 3 additions & 0 deletions R/seq.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# unique Dates -> yearmon are created. E.g., from/to will only end up being 1st of
# the month and `by` will be year + month / year + quarter

#' @export
seq.yearmon <- function(from, to, by, ...) {
.seq <- seq.Date(
zoo::as.Date(from, tz = get_default_time_zone()),
Expand All @@ -11,6 +12,7 @@ seq.yearmon <- function(from, to, by, ...) {
zoo::as.yearmon(.seq)
}

#' @export
seq.yearqtr <- function(from, to, by, ...) {
.seq <- seq.Date(
zoo::as.Date(from, tz = get_default_time_zone()),
Expand All @@ -20,6 +22,7 @@ seq.yearqtr <- function(from, to, by, ...) {
zoo::as.yearqtr(.seq)
}

#' @export
seq.hms <- function(from, to, by, ...) {
.seq <- seq.POSIXt(
as.POSIXct(from),
Expand Down
11 changes: 11 additions & 0 deletions R/to_posixct_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@ to_posixct_numeric <- function(index) {
UseMethod("to_posixct_numeric")
}

#' @export
to_posixct_numeric.default <- function(index) {
as.numeric(index)
}

#' @export
to_posixct_numeric.Date <- function(index) {
secs_in_day <- 86400
as.numeric(.POSIXct(unclass(index) * secs_in_day, tz = get_default_time_zone()))
}

#' @export
to_posixct_numeric.POSIXct <- function(index) {
as.numeric(index)
}

#' @export
to_posixct_numeric.yearmon <- function(index) {
to_posixct_numeric(
yearmon_to_POSIXct(index)
)
}

# Same as yearmon, represented as a numeric internally, same as yearmon
#' @export
to_posixct_numeric.yearqtr <- to_posixct_numeric.yearmon

#' @export
to_posixct_numeric.hms <- function(index) {
# No need to convert to POSIXct then numeric, this is just number of
# seconds since epoch
Expand Down Expand Up @@ -66,24 +72,29 @@ dispatch_to_datetime <- function(dummy, x, ...) {
UseMethod("dispatch_to_datetime")
}

#' @export
dispatch_to_datetime.default <- function(dummy, x, ..., tz = NULL) {
tz <- tz %||% get_default_time_zone()
as.POSIXct(x, tz = tz, origin = "1970-01-01", ...)
}

#' @export
dispatch_to_datetime.Date <- function(dummy, x, ..., tz = NULL) {
tz <- tz %||% get_default_time_zone()
as.Date(dispatch_to_datetime.default(dummy, x, tz = tz), tz = tz)
}

#' @export
dispatch_to_datetime.yearmon <- function(dummy, x, ..., tz = NULL) {
zoo::as.yearmon(dispatch_to_datetime.default(dummy, x, tz = tz))
}

#' @export
dispatch_to_datetime.yearqtr <- function(dummy, x, ..., tz = NULL) {
zoo::as.yearqtr(dispatch_to_datetime.default(dummy, x, tz = tz))
}

#' @export
dispatch_to_datetime.hms <- function(dummy, x, ..., tz = NULL) {
datetime <- dispatch_to_datetime.default(dummy, x, tz = tz)
hms::as_hms(datetime)
Expand Down
9 changes: 6 additions & 3 deletions man/ceiling_index.Rd

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

9 changes: 6 additions & 3 deletions man/floor_index.Rd

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

2 changes: 1 addition & 1 deletion man/tibbletime.Rd

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

0 comments on commit dfdfd32

Please sign in to comment.