Define %--%

Fixes #635
This commit is contained in:
Hadley Wickham 2022-08-09 15:57:18 -05:00
parent 4c3f5aa604
commit 776606f0a5
1 changed files with 9 additions and 6 deletions

View File

@ -519,24 +519,27 @@ It's obvious what `dyears(1) / ddays(365)` should return: one, because durations
What should `years(1) / days(1)` return?
Well, if the year was 2015 it should return 365, but if it was 2016, it should return 366!
There's not quite enough information for lubridate to give a single clear answer.
What it does instead is give an estimate, with a warning:
What it does instead is give an estimate:
```{r}
years(1) / days(1)
```
If you want a more accurate measurement, you'll have to use an **interval**.
An interval is a duration with a starting point: that makes it precise so you can determine exactly how long it is:
An interval is a pair of starting and ending date times, or you can think of it as a duration with a starting point.
You can create an interval by writing `start %--% end`:
```{r}
next_year <- today() + years(1)
(today() %--% next_year) / ddays(1)
to_next_year <- today() %--% (today() + years(1))
to_next_year
```
To find out how many periods fall into an interval, you need to use integer division:
You could then divide it by a duration or a period:
```{r}
(today() %--% next_year) %/% days(1)
to_next_year / ddays(1)
to_next_year / months(1)
```
### Summary