commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/time DurationFormatUtils.java
Date Mon, 27 Sep 2004 04:49:07 GMT
bayard      2004/09/26 21:49:07

  Modified:    lang/src/java/org/apache/commons/lang/time
                        DurationFormatUtils.java
  Log:
  typo fixed, a couple of extra overload methods added (need javadoc) and formatting for start->end
changed to match millis implementation, for a pattern of M, 4 years is reported as 48 months
and not 0 months.
  
  Revision  Changes    Path
  1.19      +46 -5     jakarta-commons/lang/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
  
  Index: DurationFormatUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/time/DurationFormatUtils.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DurationFormatUtils.java	27 Sep 2004 03:40:15 -0000	1.18
  +++ DurationFormatUtils.java	27 Sep 2004 04:49:07 -0000	1.19
  @@ -95,6 +95,14 @@
           return format(millis, "H:mm:ss.SSS");
       }
   
  +    public static String format(long millis) {
  +        return format(millis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault()
);
  +    }
  +    public static String format(long startMillis, long endMillis) {
  +        return format(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault()
);
  +    }
  +
  +
       /**
        * <p>Get the time gap as a string, using the specified format, and padding with
zeros and 
        * using the default timezone.</p>
  @@ -221,11 +229,10 @@
        * @param startMillis  the start of the duration
        * @param endMillis  the end of the duration
        * @param format  the way in which to format the duration
  -     * @param padWithZeros whether to pad the left hand side of numbers with 0's
        * @return the time as a String
        */
  -    public static String format(long startMillis, long endMillis, String format, boolean
padWithZeros) {
  -        return format(startMillis, endMillis, format, padWithZeros, TimeZone.getDefault());
  +    public static String format(long startMillis, long endMillis, String format) {
  +        return format(startMillis, endMillis, format, true, TimeZone.getDefault());
       }
       /**
        * <p>Get the time gap as a string, using the specified format.
  @@ -266,7 +273,7 @@
           int days = end.get(Calendar.DAY_OF_MONTH) - start.get(Calendar.DAY_OF_MONTH);
           while(days < 0) {
               days += 31;  // such overshooting is taken care of later on
  -            days -= 1;
  +            months -= 1;
           }
           int hours = end.get(Calendar.HOUR_OF_DAY) - start.get(Calendar.HOUR_OF_DAY);
           while(hours < 0) {
  @@ -297,6 +304,40 @@
           days -= reduceAndCorrect( start, end, Calendar.DAY_OF_MONTH, days );
           months -= reduceAndCorrect( start, end, Calendar.MONTH, months );
           years -= reduceAndCorrect( start, end, Calendar.YEAR, years );
  +
  +        // This next block of code adds in values that 
  +        // aren't requested. This allows the user to ask for the 
  +        // number of months and get the real count and not just 0->11.
  +        if(!Token.containsTokenWithValue(tokens, y) ) {
  +            if(Token.containsTokenWithValue(tokens, M) ) {
  +                months += 12 * years;
  +                years = 0;
  +            } else {
  +                // TODO: this is a bit weak, needs work to know about leap years
  +                days += 365 * years;
  +                years = 0;
  +            }
  +        }
  +        if(!Token.containsTokenWithValue(tokens, M) ) {
  +            days += end.get(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR);
  +            months = 0;
  +        }
  +        if(!Token.containsTokenWithValue(tokens, d) ) {
  +            hours += 24 * days;
  +            days = 0;
  +        }
  +        if(!Token.containsTokenWithValue(tokens, H) ) {
  +            minutes += 60 * hours;
  +            hours = 0;
  +        }
  +        if(!Token.containsTokenWithValue(tokens, m) ) {
  +            seconds += 60 * minutes;
  +            minutes = 0;
  +        }
  +        if(!Token.containsTokenWithValue(tokens, s) ) {
  +            milliseconds += 1000 * seconds;
  +            seconds = 0;
  +        }
   
           return formatDuration(tokens, years, months, days, hours, minutes, seconds, milliseconds,
padWithZeros);
       }
  
  
  

---------------------------------------------------------------------
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