tidyverse / hms

A simple class for storing time-of-day values
https://hms.tidyverse.org/
Other
138 stars 25 forks source link

Implement comparaison with character and -/+ #79

Closed statquant closed 4 years ago

statquant commented 4 years ago

Hello, that would be great if hms could implement comparaison with character like Date or nanotime, right now it does not throw and the result is wrong

as_hms("16:00:00") < "16:30:00"
[1] FALSE

That would also be great if we could use operators - and +

# class is lost
as_hms("16:00:00") - 1
Time difference of 57599 secs
# class is lost
as_hms("16:00:00") - as_hms("00:00:01")
Time difference of 57599 secs
# I'd expect this result
as_hms(as_hms("16:00:00") - 1)
15:59:59
krlmlr commented 4 years ago

Thanks.

I don't think that comparison with a string is a good idea, can you parse before comparing to make it explicit?

library(hms)
as_hms("16:00:00") < as_hms("16:30:00")
#> [1] TRUE
as_hms("17:00:00") < as_hms("16:30:00")
#> [1] FALSE

Created on 2019-12-31 by the reprex package (v0.3.0)

Unfortunately, the current design makes it very difficult to preserve the "hms" class after arithmetic operations, again you need to coerce with as_hms(). Original issue: #18.

statquant commented 4 years ago

Hello, I can definitely make it explicit but truth is I do not really want to. I guess you're afraid people start silently to use comparison on 1000000s of items, maybe we can check for right end side to be of lenghth 1 ?

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.