time_point_floor()
rounds a systime or naivetime down to a multiple of
the specified precision
.
time_point_ceiling()
rounds a systime or naivetime up to a multiple of
the specified precision
.
time_point_round()
rounds up or down depending on what is closer,
rounding up on ties.
Rounding time points is mainly useful for rounding subdaily time points up to daily time points.
It can also be useful for flooring by a set number of days (like 20) with respect to some origin. By default, the origin is 19700101 00:00:00.
If you want to group by components, such as "day of the month", rather than
by "n days", see calendar_group()
.
time_point_floor(x, precision, ..., n = 1L, origin = NULL) time_point_ceiling(x, precision, ..., n = 1L, origin = NULL) time_point_round(x, precision, ..., n = 1L, origin = NULL)
x 
A systime or naivetime. 

precision 
A time point precision. One of:

...  These dots are for future extensions and must be empty. 
n 
A positive integer specifying the multiple of 
origin 
An origin to begin counting from. Mostly useful when If If The precision of If 
x
rounded to the new precision
.
To understand how flooring and ceiling work, you need to know how they create their intervals for rounding.
time_point_floor()
constructs intervals of [lower, upper)
that
bound each element of x
, then always chooses the lefthand side.
time_point_ceiling()
constructs intervals of (lower, upper]
that
bound each element of x
, then always chooses the righthand side.
As an easy example, consider 20200102 00:00:05.
To floor this to the nearest day, the following interval is constructed, and the lefthand side is returned at day precision:
[20200102 00:00:00, 20200103 00:00:00)
To ceiling this to the nearest day, the following interval is constructed, and the righthand side is returned at day precision:
(20200102 00:00:00, 20200103 00:00:00]
Here is another example, this time with a time point on a boundary, 20200102 00:00:00.
To floor this to the nearest day, the following interval is constructed, and the lefthand side is returned at day precision:
[20200102 00:00:00, 20200103 00:00:00)
To ceiling this to the nearest day, the following interval is constructed, and the righthand side is returned at day precision:
(20200101 00:00:00, 20200102 00:00:00]
Notice that, regardless of whether you are doing a floor or ceiling, if the input falls on a boundary then it will be returned as is.
#> <time_point<naive><day>[6]> #> [1] "20190101" "20190102" "20190103" "20190104" "20190105" #> [6] "20190106"# Floor by sets of 20 days # The implicit origin to start the 20 day counter is 19700101 time_point_floor(x, "day", n = 20)#> <time_point<naive><day>[41]> #> [1] "20181215" "20181215" "20181215" "20190104" "20190104" #> [6] "20190104" "20190104" "20190104" "20190104" "20190104" #> [11] "20190104" "20190104" "20190104" "20190104" "20190104" #> [16] "20190104" "20190104" "20190104" "20190104" "20190104" #> [21] "20190104" "20190104" "20190104" "20190124" "20190124" #> [26] "20190124" "20190124" "20190124" "20190124" "20190124" #> [31] "20190124" "20190124" "20190124" "20190124" "20190124" #> [36] "20190124" "20190124" "20190124" "20190124" "20190124" #> [41] "20190124"# You can easily customize the origin by creating a duration out of the # origin date of interest... origin < year_month_day(2019, 01, 01) %>% as_naive_time() %>% as_duration() # Which you can subtract from, floor, and then add to your input time_point_floor(x  origin, "day", n = 20) + origin#> <time_point<naive><day>[41]> #> [1] "20190101" "20190101" "20190101" "20190101" "20190101" #> [6] "20190101" "20190101" "20190101" "20190101" "20190101" #> [11] "20190101" "20190101" "20190101" "20190101" "20190101" #> [16] "20190101" "20190101" "20190101" "20190101" "20190101" #> [21] "20190121" "20190121" "20190121" "20190121" "20190121" #> [26] "20190121" "20190121" "20190121" "20190121" "20190121" #> [31] "20190121" "20190121" "20190121" "20190121" "20190121" #> [36] "20190121" "20190121" "20190121" "20190121" "20190121" #> [41] "20190210"# For times on the boundary, floor and ceiling both return the input # at the new precision. Notice how the first element is on the boundary, # and the second is 1 second after the boundary. y < as_naive_time(year_month_day(2020, 01, 02, 00, 00, c(00, 01))) time_point_floor(y, "day")#> <time_point<naive><day>[2]> #> [1] "20200102" "20200102"time_point_ceiling(y, "day")#> <time_point<naive><day>[2]> #> [1] "20200102" "20200103"