Skip to content

Commit

Permalink
Add before() and after() to RichDate
Browse files Browse the repository at this point in the history
There are two implicit conversions for RichDate:
  implicit def toDate(rd: RichDate): java.util.Date
  implicit def toCalendar(rd: RichDate)(implicit tz: TimeZone): java.util.Calendar
Each has its own implementation of before() and after() methods:
  Date: boolean before(Date when)
  Calendar: boolean before(Object when)
Their implementation in java.util is pretty much the same (comparing millis), except that Calendar's one does an additional "instanceof Calendar" check. Therefore, an ambiguous behavior arises:
- Imagine a user writes: d1.before(d2), where d1, d2: RichDate.
- Normally, implicit conversion to Date will happen, and Date.before(Date when) will be applied, comparing the millis.
- However, if a user happened to have an implicit TimeZone in the context (as many Scalding job base classes do), the Calendar implicit conversion will happen, Calendar.before(Object when) will be applied, and the `instanceof` check will fail the comparison. before() will always return false!

Solution: define before() and after() methods directly in RichDate. This way, implicit conversion will not have to take place.
  • Loading branch information
megaserg committed Mar 23, 2016
1 parent 9561948 commit 615cc9e
Showing 1 changed file with 3 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ case class RichDate(val timestamp: Long) extends Ordered[RichDate] {
case _ => false
}

def before(that: RichDate): Boolean = compare(that) < 0
def after(that: RichDate): Boolean = compare(that) > 0

/**
* Use String.format to format the date, as opposed to toString, which uses SimpleDateFormat.
*/
Expand Down

0 comments on commit 615cc9e

Please sign in to comment.