Skip to content

Commit

Permalink
Add DateRange.prepend (opposite direction of extend) (#1748)
Browse files Browse the repository at this point in the history
* create new function to extend daterange in past time

* unit tests for preprend/extend/embiggen
  • Loading branch information
FlavSF authored and johnynek committed Oct 22, 2017
1 parent d53d06f commit e40bf1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ case class DateRange(val start: RichDate, val end: RichDate) {
*/
def extend(delta: Duration) = DateRange(start, end + delta)

/**
* Extend the length by moving the start.
* Turns out, we can start the party early.
*/
def prepend(delta: Duration) = DateRange(start - delta, end)

def contains(point: RichDate) = (start <= point) && (point <= end)
/**
* Is the given Date range a (non-strict) subset of the given range
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ class DateTest extends WordSpec {
"reject an end that is before its start" in {
intercept[IllegalArgumentException] { DateRange("2010-10-02", "2010-10-01") }
}
"correctly add time in either or both directions" in {
assert(DateRange("2010-10-01", "2010-10-02").extend(Days(3)).each(Days(1)).size === 5)
assert(DateRange("2010-10-01", "2010-10-02").prepend(Days(3)).each(Days(1)).size === 5)
assert(DateRange("2010-10-01", "2010-10-02").embiggen(Days(3)).each(Days(1)).size === 8)
assert(DateRange("2010-10-01", "2010-10-10").extend(Days(1)).prepend(Days(1)) ==
DateRange("2010-10-01", "2010-10-10").embiggen(Days(1)))
}
}
"Time units" should {
def isSame(d1: Duration, d2: Duration) = {
Expand Down

0 comments on commit e40bf1b

Please sign in to comment.