A simple class for storing time-of-day values
R Shell Makefile
Latest commit 8f4eaa3 Jul 25, 2017 @krlmlr krlmlr Merge tag 'v0.3.0.9003'
- Fix and enhance examples in `?hms`.
- `hms()` creates a zero-length object of class `hms` that prints as `"hms()"`.
- `hms(integer())` and `as.hms(integer())` both work and are identical to `hms()`.
- `as.hms.POSIXt()` now defaults to the current time zone, the previous default was `"UTC"` and can be restored by calling `pkgconfig::set_config("hms::default_tz", "UTC")`.
- `as.hms.POSIXt()` gains `tz` argument, default `"UTC"` (#28).

README.md

hms Travis-CI Build Status AppVeyor Build Status Coverage Status CRAN_Status_Badge

A simple class for storing durations or time-of-day values and displaying them in the hh:mm:ss format. Intended to simplify data exchange with databases, spreadsheets, and other data sources.

The values are stored as a numeric vector that contains the number of seconds since midnight. Supports construction from time values, coercion to and from various data types, and formatting, based on the difftime class. Can be used in a data frame.

Compared to POSIXct, no date is stored, although the values can exceed the 24-hour boundary or be negative. By default, fractional seconds up to a microsecond are displayed.

library(hms)
hms(56, 34, 12)
#> 12:34:56
as.hms(1)
#> 00:00:01
as.hms("12:34:56")
#> 12:34:56
as.hms(Sys.time())
#> 14:41:28.004544
as.POSIXct(hms(1))
#> [1] "1970-01-01 00:00:01 UTC"

data.frame(hours = 1:3, hms = hms(hours = 1:3))
#>   hours      hms
#> 1     1 01:00:00
#> 2     2 02:00:00
#> 3     3 03:00:00

Install the package from GitHub:

# install.packages("devtools")
devtools::install_github("tidyverse/hms")