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

format.hms() accepting argument format #125

Open
retodomax opened this issue Nov 5, 2024 · 2 comments
Open

format.hms() accepting argument format #125

retodomax opened this issue Nov 5, 2024 · 2 comments

Comments

@retodomax
Copy link

There seems to be a format() S3 method for class 'hms'. However, it just returns the input as character. I would have expected that it takes the argument format and formats the output like an object of class POSIXct

library(hms)
t <- Sys.time()
format(t, "%H")
#> [1] "17"

t2 <- as_hms(t)
format(t2, "%H")
#> [1] "17:12:38.169816"

Created on 2024-11-05 with reprex v2.1.1

@krlmlr
Copy link
Member

krlmlr commented Nov 10, 2024

Thanks. The hms type inherits from difftime, that type doesn't seem to support the format() syntax you're suggesting either:

d <- Sys.time() - Sys.time()
d
#> Time difference of -9.536743e-07 secs
format(d, "%H")
#> Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument

Created on 2024-11-10 with reprex v2.1.1

Here's what I think happens: format() is forwarded to format.default(), and the "%H" is matched to the trim argument.

format.difftime
#> function (x, ...) 
#> {
#>     if (length(x)) 
#>         paste(format(unclass(x), ...), units(x))
#>     else character()
#> }
#> <bytecode: 0x10527d070>
#> <environment: namespace:base>
formals(format.default)
#> $x
#> 
#> 
#> $trim
#> [1] FALSE
#> 
#> $digits
#> NULL
#> 
#> $nsmall
#> [1] 0
#> 
#> $justify
#> c("left", "right", "centre", "none")
#> 
#> $width
#> NULL
#> 
#> $na.encode
#> [1] TRUE
#> 
#> $scientific
#> [1] NA
#> 
#> $big.mark
#> [1] ""
#> 
#> $big.interval
#> [1] 3
#> 
#> $small.mark
#> [1] ""
#> 
#> $small.interval
#> [1] 5
#> 
#> $decimal.mark
#> getOption("OutDec")
#> 
#> $zero.print
#> NULL
#> 
#> $drop0trailing
#> [1] FALSE
#> 
#> $...

Created on 2024-11-10 with reprex v2.1.1

@retodomax
Copy link
Author

retodomax commented Nov 10, 2024

Thanks for the reply!
I guess, ideal would be, if this feature would be implemented in base R but hard to say if they want it there...
If you're interested, I could try to write a method implementing the feature for package hms where in my opinion it would make sense to be able to extract/format specific time components (hours, minutes, ...) as string.

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

No branches or pull requests

2 participants