Skip to content

Commit

Permalink
Make round_date() really use week_start=
Browse files Browse the repository at this point in the history
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 tidyverse#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 👍 !
  • Loading branch information
xvrdm authored Nov 27, 2018
1 parent f5b92c3 commit fe5f869
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/round.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe5f869

Please # to comment.