Title: | Pretty Time of Day |
---|---|
Description: | Implements an S3 class for storing and formatting time-of-day values, based on the 'difftime' class. |
Authors: | Kirill Müller [aut, cre] , R Consortium [fnd], RStudio [fnd] |
Maintainer: | Kirill Müller <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.3.9013 |
Built: | 2024-12-09 03:10:28 UTC |
Source: | https://github.com/tidyverse/hms |
Implements an S3 class for storing and formatting time-of-day values, based on the 'difftime' class.
Maintainer: Kirill Müller [email protected] (ORCID)
Other contributors:
R Consortium [funder]
RStudio [funder]
Useful links:
Report bugs at https://github.com/tidyverse/hms/issues
The values are stored as a difftime vector with a custom class, and always with "seconds" as unit for robust coercion to numeric. Supports construction from time values, coercion to and from various data types, and formatting. Can be used as a regular column in a data frame.
hms()
is a high-level constructor that accepts second, minute, hour and day components
as numeric vectors.
new_hms()
is a low-level constructor that only checks that its input has the correct base type, numeric.
is_hms()
checks if an object is of class hms
.
as_hms()
is a generic that supports conversions beyond casting.
The default method forwards to vctrs::vec_cast()
.
hms(seconds = NULL, minutes = NULL, hours = NULL, days = NULL) new_hms(x = numeric()) is_hms(x) as_hms(x, ...) ## S3 method for class 'hms' as.POSIXct(x, ...) ## S3 method for class 'hms' as.POSIXlt(x, ...) ## S3 method for class 'hms' as.character(x, ...) ## S3 method for class 'hms' format(x, ...) ## S3 method for class 'hms' print(x, ...)
hms(seconds = NULL, minutes = NULL, hours = NULL, days = NULL) new_hms(x = numeric()) is_hms(x) as_hms(x, ...) ## S3 method for class 'hms' as.POSIXct(x, ...) ## S3 method for class 'hms' as.POSIXlt(x, ...) ## S3 method for class 'hms' as.character(x, ...) ## S3 method for class 'hms' format(x, ...) ## S3 method for class 'hms' print(x, ...)
seconds , minutes , hours , days
|
Time since midnight. No bounds checking is performed. |
x |
An object. |
... |
additional arguments to be passed to or from methods. |
For hms()
, all arguments must have the same length or be
NULL
. Odd combinations (e.g., passing only seconds
and
hours
but not minutes
) are rejected.
For arguments of type lubridate::POSIXct and POSIXlt, as_hms()
does not perform timezone
conversion.
Use lubridate::with_tz()
and lubridate::force_tz()
as necessary.
hms(56, 34, 12) hms() new_hms(as.numeric(1:3)) # Supports numeric only! try(new_hms(1:3)) as_hms(1) as_hms("12:34:56") as_hms(Sys.time()) as.POSIXct(hms(1)) data.frame(a = hms(1)) d <- data.frame(hours = 1:3) d$hours <- hms(hours = d$hours) d
hms(56, 34, 12) hms() new_hms(as.numeric(1:3)) # Supports numeric only! try(new_hms(1:3)) as_hms(1) as_hms("12:34:56") as_hms(Sys.time()) as.POSIXct(hms(1)) data.frame(a = hms(1)) d <- data.frame(hours = 1:3) d$hours <- hms(hours = d$hours) d
These functions convert character vectors to objects of the hms class.
NA
values are supported.
parse_hms()
accepts values of the form "HH:MM:SS"
, with optional
fractional seconds.
parse_hm()
accepts values of the form "HH:MM"
.
parse_hms(x) parse_hm(x)
parse_hms(x) parse_hm(x)
x |
A character vector |
An object of class hms.
parse_hms("12:34:56") parse_hms("12:34:56.789") parse_hm("12:34")
parse_hms("12:34:56") parse_hms("12:34:56.789") parse_hm("12:34")
Convenience functions to round or truncate to a multiple of seconds.
round_hms(x, secs = NULL, digits = NULL) trunc_hms(x, secs = NULL, digits = NULL)
round_hms(x, secs = NULL, digits = NULL) trunc_hms(x, secs = NULL, digits = NULL)
x |
A vector of class hms |
secs |
Multiple of seconds, a positive numeric. Values less than one are supported |
digits |
Number of digits, a whole number. Negative numbers are supported. |
The input, rounded or truncated to the nearest multiple of secs
(or number of digits
)
round_hms(as_hms("12:34:56"), 5) round_hms(as_hms("12:34:56"), 60) round_hms(as_hms("12:34:56.78"), 0.25) round_hms(as_hms("12:34:56.78"), digits = 1) round_hms(as_hms("12:34:56.78"), digits = -2) trunc_hms(as_hms("12:34:56"), 60)
round_hms(as_hms("12:34:56"), 5) round_hms(as_hms("12:34:56"), 60) round_hms(as_hms("12:34:56.78"), 0.25) round_hms(as_hms("12:34:56.78"), digits = 1) round_hms(as_hms("12:34:56.78"), digits = -2) trunc_hms(as_hms("12:34:56"), 60)
Double dispatch methods to support vctrs::vec_cast()
.
## S3 method for class 'hms' vec_cast(x, to, ...)
## S3 method for class 'hms' vec_cast(x, to, ...)
x |
Vectors to cast. |
to |
Type to cast to. If |
... |
For |
Double dispatch methods to support vctrs::vec_ptype2()
.
## S3 method for class 'hms' vec_ptype2(x, y, ..., x_arg = "", y_arg = "")
## S3 method for class 'hms' vec_ptype2(x, y, ..., x_arg = "", y_arg = "")
x , y
|
Vector types. |
... |
These dots are for future extensions and must be empty. |
x_arg , y_arg
|
Argument names for |