Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

hms

A simple class for storing time-of-day values


Description

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 vec_cast().

Usage

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, ...)

Arguments

seconds, minutes, hours, days

Time since midnight. No bounds checking is performed.

x

An object.

...

additional arguments to be passed to or from methods.

Details

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 POSIXct and POSIXlt, as_hms() does not perform timezone conversion. Use lubridate::with_tz() and lubridate::force_tz() as necessary.

Examples

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

Pretty Time of Day

v1.0.0
MIT + file LICENSE
Authors
Kirill Müller [aut, cre], R Consortium [fnd], RStudio [fnd]
Initial release
2021-01-13

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.