Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/ et al. drop class "hms" #119

Open
AshesITR opened this issue Aug 18, 2023 · 6 comments · May be fixed by #120
Open

/ et al. drop class "hms" #119

AshesITR opened this issue Aug 18, 2023 · 6 comments · May be fixed by #120

Comments

@AshesITR
Copy link

Most computations with hms drop the class to difftime causing worse UX.

hms::hms(10.5) / 2.0
#> Time difference of 5.25 secs
`/.hms` <- function(a, b) {
  res <- as.difftime(unclass(a), units = attr(a, "units")) / b
  hms::as_hms(res)
}
hms::hms(10.5) / 2.0
#> 00:00:05.25
sum(hms::hms(c(10L, 10L)))
#> Time difference of 20 secs

Created on 2023-08-18 with reprex v2.0.2

@AshesITR
Copy link
Author

Here is a more general fix, covering base operations and summaries (like sum, min, max, range), and mean()
I can work on a PR if the change is desired.

Ops.hms <- function(e1, e2) {
  res <- NextMethod(.Generic)
  if (inherits(res, "difftime")) {
    hms::as_hms(res)
  } else {
    res
  }
}

Summary.hms <- function(..., na.rm) {
  res <- NextMethod(.Generic)
  hms::as_hms(res)
}

mean.hms <- function(x, ...) {
  hms::as_hms(NextMethod(.Generic))
}

@krlmlr
Copy link
Member

krlmlr commented Aug 20, 2023

Thanks. Is this related to #18?

@AshesITR
Copy link
Author

Trying to understand the neccessary test cases:
Relevant classes appear to be all of: Date, POSIXct and POSIXlt (possibly with timezones and times around DST), difftime (base), hms.

Once with hms on the LHS and once with hms on the RHS (with expect_error() where appropriate).

Is that assessment correct?

@krlmlr
Copy link
Member

krlmlr commented Aug 21, 2023

Multiplying with and dividing by numeric or integer? Otherwise, this looks fairly complete.

@AshesITR AshesITR linked a pull request Aug 21, 2023 that will close this issue
@olsgaard
Copy link

olsgaard commented Jan 7, 2025

I'm having a similar problem with max(my_hms_time) returning a difftime instead of a hms, e.g.:

> max(as_hms(10))
Time difference of 10 secs
> class(max(as_hms(10)))
[1] "difftime"
> class(as_hms(10))
[1] "hms"      "difftime"

Would #120 fix this and what would it take to review it again?

@krlmlr
Copy link
Member

krlmlr commented Jan 7, 2025

In #120, we need the existing tests to work. Could require changes in base R. I retriggered the test runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants