-
Notifications
You must be signed in to change notification settings - Fork 708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add before() and after() to RichDate #1538
Add before() and after() to RichDate #1538
Conversation
@megaserg Can you add a test case that fails the |
b50ac14
to
9d10cbb
Compare
@@ -97,6 +98,12 @@ class DateTest extends WordSpec { | |||
assert(rd1 >= rd1) | |||
assert(rd2 >= rd2) | |||
} | |||
"be able to compare with before() and after() with TimeZone in context" in { | |||
//implicit val tz: TimeZone = TimeZone.getDefault |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's not have this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uncommented. It would be failing even if I would remove the comment, but then the validness of the test would depend on that implicit tz on the top level (which someone someday might decide to remove, rendering my test trivial).
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.
9d10cbb
to
615cc9e
Compare
+1 The test fails when I run it in the console on |
Thanks for the contribution! |
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()
andafter()
methods:boolean before(Date when)
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:d1.before(d2)
, whered1, d2: RichDate
.d1
to Date will happen, andd1.before(Date when)
will be applied tod2
(converted to Date), comparing the millis.d1
to Calendar will happen,d1.before(Object when)
will be applied tod2
(which is a DateRange), and theinstanceof
check will fail the comparison.before()
will always returnfalse
!Solution: define
before()
andafter()
methods directly in RichDate. This way, implicit conversion will not have to take place.Complete example to reproduce. Comment/uncomment
tz
declaration to printtrue
orfalse
.