commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sebastian Götz (JIRA) <>
Subject [jira] [Updated] (LANG-951) Fragments are wrong by 1 day when using fragment YEAR or MONTH
Date Mon, 20 Jan 2014 08:41:20 GMT


Sebastian Götz updated LANG-951:

    Attachment: LANG-951.patch

Dear Beneditk,

I have attached a path file containing the JUnit testcase and the suggested fix. Since this
is the first time I ever provided a patch to an open source project I hope I made it the way
you are used to have it.

After applying my fix my own testcase runs without error but actually breaks 16 others. After
reviewing what is tested there they seem to be broken rightly. I haven't done anything to
prevent them from breaking. Actually the testcases are wrong.

In the the date tested against is August 23rd, 19:53:47.991. So for example
to calculate the passed hours of the month (which is what the testcase 'testHoursOfMonthWithDate'
does) one would have to do the following math 22 days * 24 hours + 19 hours = 547. The testcase
assumes that the correct value is 571 which resolves to 23 days * 24 hours + 19 hours which
is actually wrong from my point of view.

> Fragments are wrong by 1 day when using fragment YEAR or MONTH
> --------------------------------------------------------------
>                 Key: LANG-951
>                 URL:
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.time.*
>    Affects Versions: 3.1
>            Reporter: Sebastian Götz
>             Fix For: 3.3, Review Patch
>         Attachments: LANG-951.patch
>   Original Estimate: 1h
>  Remaining Estimate: 1h
> When one trys to get the fragments of a calendar object and uses the fragment YEAR or
MONTH, the returned value is wrong by 1 day in the targeted timeunit. The bug resides in the
class *DateUtils* in function 
> {code}
> private static long getFragment(Calendar calendar, int fragment, int unit)
> {code}
> There is an initial recalculation if the fragment is YEAR or MONTH. So if one would like
to have the minutes for the fragment YEAR for the date 2000-01-01 00:00:00 this would return
1440 which is actually wrong. The error can be found on lines 1635 - 1643.
> Suggested fix:
> {code}
> // Fragments bigger than a day require a breakdown to days
>         switch (fragment) {
>             case Calendar.YEAR:
>                 result += ((calendar.get(Calendar.DAY_OF_YEAR)-1) * MILLIS_PER_DAY) /
>                 break;
>             case Calendar.MONTH:
>                 result += ((calendar.get(Calendar.DAY_OF_MONTH)-1) * MILLIS_PER_DAY)
/ millisPerUnit;
>                 break;
>         }
> {code}

This message was sent by Atlassian JIRA

View raw message