From fe5f869fbf65a739d501f579e17d6e0007c9d77f Mon Sep 17 00:00:00 2001 From: Xavier A Date: Tue, 27 Nov 2018 11:25:43 +0100 Subject: [PATCH] Make round_date() really use week_start= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After reading the documentation and many SO questions, I still couldn't really explain the difference between `round_date()` and `ceiling/floor_date` when using `unit="week"` and `week_start=1`. I thought it might just be that `round_date()` ignore `week_start=`, a bit like `floor/ceiling_date` back in the #509 days. Without `week_start=`, everything looks as expected. ``` > date <- parse_date_time("November 27 2018 23:45", orders="bdyHM") > date [1] "2018-11-27 23:45:00 UTC" > lubridate::round_date(date, "week") [1] "2018-11-25 UTC" > lubridate::floor_date(date, "week") [1] "2018-11-25 UTC" > lubridate::ceiling_date(date, "week") [1] "2018-12-02 UTC" ``` But if you ask for weeks starting on Mondays (or any other day). Only `floor/ceiling_date` seem affected: ``` > lubridate::round_date(date, "week", week_start = 1) [1] "2018-11-25 UTC" > lubridate::floor_date(date, "week", week_start = 1) [1] "2018-11-26 UTC" > lubridate::ceiling_date(date, "week", week_start = 1) [1] "2018-12-03 UTC" ``` Apart from this tiny glitch, thanks for the awesome library: I don't want to use anything else when it comes to dates 👍 ! --- R/round.r | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/round.r b/R/round.r index fc8d084c..c852a0fa 100644 --- a/R/round.r +++ b/R/round.r @@ -131,9 +131,9 @@ round_date <- function(x, unit = "second", week_start = getOption("lubridate.wee ## special case for fast rounding round.POSIXt(x, units = lub2base_units[[basic_unit]]) } else { - above <- unclass(as.POSIXct(ceiling_date(x, unit))) + above <- unclass(as.POSIXct(ceiling_date(x, unit, week_start))) mid <- unclass(as.POSIXct(x)) - below <- unclass(as.POSIXct(floor_date(x, unit))) + below <- unclass(as.POSIXct(floor_date(x, unit, week_start))) wabove <- (above - mid) <= (mid - below) wabove <- !is.na(wabove) & wabove new <- below