Pretty Time of Day

Implements an S3 class for storing and formatting time-of-day values, based on the 'difftime' class.


hms Hexagonal logo for the R package ‘hms’, featuring a stylized green clock face showing the time 10:05, with the package name ‘hms’ in white text at the center and the rstudio.com URL at the bottom edge.

Lifecycle:stable rcc Codecov testcoverage CRAN_Status_Badge

Overview

The hms package provides a simple class for storing durations or time-of-day values and displaying them in the hh:mm:ss format. This class is intended to simplify data exchange with databases, spreadsheets, and other data sources:

  • Stores values as a numeric vector that contains the number of seconds since midnight
  • Supports construction from explicit hour, minute, or second values
  • Supports coercion to and from various data types, including POSIXt
  • Can be used as column in a data frame
  • Based on the difftime class
  • Values can exceed the 24-hour boundary or be negative
  • By default, fractional seconds up to a microsecond are displayed, regardless of the value of the "digits.secs" option

Installation

# The easiest way to get hms is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just hms:
install.packages("hms")

# Or the the development version from GitHub:
# install.packages("pak")
pak::pak("tidyverse/hms")

Usage

The following example showcases ways of using the hms class standalone or as a data frame column.

library(hms)

hms(56, 34, 12)
#> 12:34:56
as_hms(Sys.time())
#> 10:27:38.905625
parse_hms("12:34:56")
#> 12:34:56
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

Internal representation

Objects of the hms and its underlying difftime classes are stored as number of seconds since 00:00:00. Use as.numeric() and as_hms() to convert to and from numbers.

times <- parse_hms(c("00:00:00.25", "00:00:01", "00:01:30", "01:00:00"))
times
#> 00:00:00.25
#> 00:00:01.00
#> 00:01:30.00
#> 01:00:00.00
times_num <- as.numeric(times)
times_num
#> [1]    0.25    1.00   90.00 3600.00
as_hms(times_num)
#> 00:00:00.25
#> 00:00:01.00
#> 00:01:30.00
#> 01:00:00.00

Please note that the ‘hms’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("hms")

1.1.4 by Kirill Müller, a year ago


https://hms.tidyverse.org/, https://github.com/tidyverse/hms


Report a bug at https://github.com/tidyverse/hms/issues


Browse source code at https://github.com/cran/hms


Authors: Kirill Müller [aut, cre] (ORCID: , R Consortium [fnd] , Posit Software , PBC [fnd] (ROR: <https://ror.org/03wc8by49>)


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cli, lifecycle, methods, pkgconfig, rlang, vctrs

Suggests crayon, lubridate, pillar, testthat


Imported by ABCDscores, ARUtools, BioIndex, DBItest, LightLogR, NBDCtools, Open.Visualization.Academy, RMariaDB, RPostgres, Rparadox, actibase, activAnalyzer, admiral, admiralneuro, admiralophtha, admiralvaccine, arcgisrouting, artoo, cgmquantify, chms, clockplot, connector.databricks, corella, data.checker, dataquieR, datasetjson, dttr2, eatGADS, edeaR, engager, fitbitViz, haven, hmstimer, iglu, janitor, mctq, melidosData, metaSVR, metro, mineSweepR, mlfit, movementsync, nemsqar, odbc, oncmap, processmapR, progress, quickcheck, readr, rwalkr, sonicscrewdriver, subtools, tibbletime, tidyMC, tidytransit, tidyverse, timetk, transittraj, traumar, tuber, vroom, when.

Suggested by DBI, GTFSwizard, RSQLite, arrow, bigrquery, bigrquerystorage, codebookr, crosstable, datasetviewer, dbx, duckplyr, eplusr, extras, feather, fmtr, frictionless, gasmodel, ggplot2, globaltrends, gtExtras, gtfsrouter, minty, mlr3db, mobdb, mopac, nanoarrow, nanoparquet, pkgmatch, pkgstats, qio, scales, shinyTime, shinydataviewer, stbl, tibble, tsibble.

Enhanced by tind.


See at CRAN