Rounding to the nearest unit or multiple of a unit are supported. All meaningful specifications in English language are supported - secs, min, mins, 2 minutes, etc.

Rounding to fractional seconds is supported. Please note that rounding to fractions smaller than 1s can lead to large precision errors due to the floating point representation of the POSIXct objects. See examples.

`round_date()` takes a hms object and rounds it to the nearest value of the specified time unit. For rounding hms which is exactly halfway between two consecutive units, the convention is to round up. Note that this is in line with the behavior of R's [base::round.POSIXt()] function but does not follow the convention of the base [base::round()] function which "rounds to the even digit" per IEC 60559.

`floor_hms()` takes a hms object and rounds it down to the nearest boundary of the specified time unit.

`ceiling_hms()` takes a hms object and rounds it up to the nearest boundary of the specified time unit.

round_hms(x, unit = "second")

floor_hms(x, unit = "seconds")

ceiling_hms(x, unit = "seconds")

Arguments

x

a vector of date-time objects

unit

a character string specifying a time unit or a multiple of a unit to be rounded to. Valid base units are `second`, `minute`, and `hour`. Arbitrary unique English abbreviations as in the [period()] constructor are allowed. Rounding to multiple of units is supported.

See also

[base::round(), lubridate::round_date(), lubridate::floor_date(), lubridate::ceiling_date()]

Examples


## print fractional seconds
options(digits.secs = 6)

x <- lubridate::hms("12:01:59.23")
round_hms(x, ".5s")
#> [1] "12H 1M 59S"
round_hms(x, "sec")
#> [1] "12H 1M 59S"
round_hms(x, "second")
#> [1] "12H 1M 59S"
round_hms(x, "minute")
#> [1] "12H 2M 0S"
round_hms(x, "5 mins")
#> [1] "12H 0M 0S"
round_hms(x, "hour")
#> [1] "12H 0M 0S"
round_hms(x, "2 hours")
#> [1] "12H 0M 0S"

x <- lubridate::hms("12:01:59.23")
floor_hms(x, "12s")
#> [1] "12H 1M 48S"
floor_hms(x, "second")
#> [1] "12H 1M 59S"
floor_hms(x, "minute")
#> [1] "12H 1M 0S"
floor_hms(x, "hour")
#> [1] "12H 0M 0S"

x <- lubridate::hms("12:01:59.23")
ceiling_hms(x, "15 sec")
#> [1] "12H 2M 0S"
ceiling_hms(x, "second")
#> [1] "12H 2M 0S"
ceiling_hms(x, "minute")
#> [1] "12H 2M 0S"
ceiling_hms(x, "5 mins")
#> [1] "12H 5M 0S"
ceiling_hms(x, "hour")
#> [1] "13H 0M 0S"
x <- lubridate::hms("12:01:59.23")
ceiling_hms(x, "10 seconds")
#> [1] "12H 2M 0S"