-3

I'm trying to calculate the number of days between two specific pairs of dates but the assert is failing on the second test, which is only a week further apart from the first test.

The code is below.

Is there a bug in my code? Or is this a weird java/groovy bug?

use(groovy.time.TimeCategory) {
    def duration = Date.parse("yyyy-MM-dd", "2013-03-10") - Date.parse("yyyy-MM-dd", "2012-12-30")
    assert duration.days == 70

    def duration2 = Date.parse("yyyy-MM-dd", "2013-03-17") - Date.parse("yyyy-MM-dd", "2012-12-30")
    assert duration2.days == 77
}
4

1 回答 1

0

@Damien_The_Unbeliever had it right. Since EST was being used, around March, it is switched to EDT which is 1 hour ahead (so converting 2013-03-17 EDT to EST would mean it would lose one hour.)

I've changed the test to confirm that this is true. The second and third asserts pass.

use(groovy.time.TimeCategory) {
    def duration = Date.parse("yyyy-MM-dd", "2013-03-10") - Date.parse("yyyy-MM-dd", "2012-12-30")
    assert duration.days == 70

    def duration2 = Date.parse("yyyy-MM-dd", "2013-03-17") - Date.parse("yyyy-MM-dd", "2012-12-30")
    assert duration2.days == 76
    assert duration2.hours == 23
}
于 2013-03-18T17:01:50.590 回答