commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Li Zhang (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-59) [lang] DateUtils.truncate method is buggy when dealing with DST switching hours
Date Tue, 04 Jul 2006 18:44:30 GMT
    [ http://issues.apache.org/jira/browse/LANG-59?page=comments#action_12419165 ] 

Li Zhang commented on LANG-59:
------------------------------

I did a little hack and found this bug could be fixed with the following modification on DateUtils.modify
method:
    private static void modify(Calendar val, int field, boolean round) {   
        ...
            if (!offsetSet) {
                int min = val.getActualMinimum(fields[i][0]);
                int max = val.getActualMaximum(fields[i][0]);
                //Calculate the offset from the minimum allowed value
                offset = val.get(fields[i][0]) - min;
                //Set roundUp if this is more than half way between the minimum and maximum
                roundUp = offset > ((max - min) / 2);
            }
            //We need to remove this field - Do nothing if there is no change
            if (offset != 0) {     <--- add the check
            	val.set(fields[i][0], val.get(fields[i][0]) - offset);
  	    }
        }
        throw new IllegalArgumentException("The field " + field + " is not supported");
   }

Calendar.set method resets some fields (e.g., DST_OFFSET) even if the new value is the same
as the old value.  We just need to add a check before setting the new value to a field. If
there is no change on this field, do not call Calendar.set method.

> [lang] DateUtils.truncate method is buggy when dealing with DST switching hours
> -------------------------------------------------------------------------------
>
>          Key: LANG-59
>          URL: http://issues.apache.org/jira/browse/LANG-59
>      Project: Commons Lang
>         Type: Bug

>     Versions: 2.1
>  Environment: Operating System: Windows XP
> Platform: Other
>     Reporter: Li Zhang
>      Fix For: 2.2

>
> Try to truncate 2004-10-31 01:00:00 MDT by hour and you'll actually get 2004-10-
> 31 01:00:00 MST, which is one hour after the input hour.
>     // truncate 2004-10-31 01:00:00 MDT
>     Date oct31_01MDT = new Date(1099206000000L);    
>     Date result = DateUtils.truncate(oct31_01MDT, Calendar.HOUR_OF_DAY);
>     assertEquals(oct31_01MDT, result);

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message