Skip to contents

[Maturing]

round_time() takes a Duration, difftime, hms, POSIXct, or POSIXlt object and round it at the seconds level.

Usage

round_time(x)

# S3 method for Duration
round_time(x)

# S3 method for difftime
round_time(x)

# S3 method for hms
round_time(x)

# S3 method for POSIXct
round_time(x)

# S3 method for POSIXlt
round_time(x)

Arguments

x

An object belonging to one of the following classes: Duration, difftime, hms, POSIXct, or POSIXlt.

Value

An object of the same class of x rounded at the seconds level.

Details

Round standard

round_time() uses base::round() for rounding. That is to say that round_time() uses the same IEC 60559 standard ("go to the even digit") for rounding off a 5. Therefore, round(0.5) is equal to 0 and round(-1.5) is equal to -2. See ?round to learn more.

Period objects

Period objects are special type of objects developed by the lubridate team that represents "human units", ignoring possible timeline irregularities. That is to say that 1 day as Period can have different time spans, when looking to a timeline after a irregularity event.

Since the time span of a Period object can fluctuate, round_time() don't accept this kind of object. You can transform it to a Duration object and still use the function, but beware that this can produce errors.

Learn more about Period objects in the Dates and times chapter of Wickham & Grolemund book (n.d.).

References

Wickham, H., & Grolemund, G. (n.d.). R for data science. Sebastopol, CA: O'Reilly Media. https://r4ds.had.co.nz

See also

Other date-time rounding functions: round_hms() trunc_hms() round_date().

Other utility functions: assign_date(), cycle_time(), pretty_mctq(), qplot_walk(), random_mctq(), raw_data(), shorter_interval(), sum_time()

Examples

## Scalar example

lubridate::dmilliseconds(123456789)
#> [1] "123456.789s (~1.43 days)"
#> [1] "123456.789s (~1.43 days)" # Expected
round_time(lubridate::dmilliseconds(123456789))
#> [1] "123457s (~1.43 days)"
#> [1] "123457s (~1.43 days)" # Expected

as.difftime(12345.6789, units = "secs")
#> Time difference of 12345.68 secs
#> Time difference of 12345.68 secs # Expected
round_time(as.difftime(12345.6789, units = "secs"))
#> Time difference of 12346 secs
#> Time difference of 12346 secs # Expected

hms::as_hms(12345.6789)
#> 03:25:45.6789
#> 03:25:45.6789 # Expected
round_time(hms::as_hms(12345.6789))
#> 03:25:46
#> 03:25:46 # Expected

lubridate::as_datetime(12345.6789, tz = "EST")
#> [1] "1969-12-31 22:25:45 EST"
#> [1] "1969-12-31 22:25:45 EST" # Expected
as.numeric(lubridate::as_datetime(12345.6789, tz = "EST"))
#> [1] 12345.68
#> [1] 12345.68 # Expected
round_time(lubridate::as_datetime(12345.6789, tz = "EST"))
#> [1] "1969-12-31 22:25:46 EST"
#> [1] "1969-12-31 22:25:46 EST" # Expected
as.numeric(round_time(lubridate::as_datetime(12345.6789, tz = "EST")))
#> [1] 12346
#> [1] 12346 # Expected

## Vector example

c(lubridate::dhours(5.6987), lubridate::dhours(2.6875154))
#> [1] "20515.32s (~5.7 hours)"    "9675.05544s (~2.69 hours)"
#> [1] "20515.32s (~5.7 hours)"    "9675.05544s (~2.69 hours)" # Expected
round_time(c(lubridate::dhours(5.6987), lubridate::dhours(2.6875154)))
#> [1] "20515s (~5.7 hours)" "9675s (~2.69 hours)"
#> [1] "20515s (~5.7 hours)" "9675s (~2.69 hours)" # Expected