    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.time;
    018
    019import java.text.SimpleDateFormat;
    020import java.util.ArrayList;
    021import java.util.Calendar;
    022import java.util.Date;
    023import java.util.GregorianCalendar;
    024import java.util.Objects;
    025import java.util.TimeZone;
    026import java.util.stream.Stream;
    027
    028import org.apache.commons.lang3.StringUtils;
    029import org.apache.commons.lang3.Validate;
    030
    031/**
    032 * Duration formatting utilities and constants. The following table describes the tokens
    033 * used in the pattern language for formatting.
    034 * <table border="1">
    035 *  <caption>Pattern Tokens</caption>
    036 *  <tr><th>character</th><th>duration element</th></tr>
    037 *  <tr><td>y</td><td>years</td></tr>
    038 *  <tr><td>M</td><td>months</td></tr>
    039 *  <tr><td>d</td><td>days</td></tr>
    040 *  <tr><td>H</td><td>hours</td></tr>
    041 *  <tr><td>m</td><td>minutes</td></tr>
    042 *  <tr><td>s</td><td>seconds</td></tr>
    043 *  <tr><td>S</td><td>milliseconds</td></tr>
    044 *  <tr><td>'text'</td><td>arbitrary text content</td></tr>
    045 * </table>
    046 *
    047 * <b>Note: It's not currently possible to include a single-quote in a format.</b>
    048 * <br>
    049 * Token values are printed using decimal digits.
    050 * A token character can be repeated to ensure that the field occupies a certain minimum
    051 * size. Values will be left-padded with 0 unless padding is disabled in the method invocation.
    052 * <br>
    053 * Tokens can be marked as optional by surrounding them with brackets [ ]. These tokens will
    054 * only be printed if the token value is non-zero. Literals within optional blocks will only be
    055 * printed if the preceding non-literal token is non-zero. Leading optional literals will only
    056 * be printed if the following non-literal is non-zero.
    057 * Multiple optional blocks can be used to group literals with the desired token.
    058 * <p>
    059 * Notes on Optional Tokens:<br>
    060 * <b>Multiple optional tokens without literals can result in impossible to understand output.</b><br>
    061 * <b>Patterns where all tokens are optional can produce empty strings.</b><br>
    062 * (See examples below)
    063 * </p>
    064 * <br>
    065 * <table border="1">
    066 * <caption>Example Output</caption>
    067 * <tr><th>pattern</th><th>Duration.ofDays(1)</th><th>Duration.ofHours(1)</th><th>Duration.ofMinutes(1)</th><th>Duration.ZERO</th></tr>
    068 * <tr><td>d'd'H'h'm'm's's'</td><td>1d0h0m0s</td><td>0d1h0m0s</td><td>0d0h1m0s</td><td>0d0h0m0s</td></tr>
    069 * <tr><td>d'd'[H'h'm'm']s's'</td><td>1d0s</td><td>0d1h0s</td><td>0d1m0s</td><td>0d0s</td></tr>
    070 * <tr><td>[d'd'H'h'm'm']s's'</td><td>1d0s</td><td>1h0s</td><td>1m0s</td><td>0s</td></tr>
    071 * <tr><td>[d'd'H'h'm'm's's']</td><td>1d</td><td>1h</td><td>1m</td><td></td></tr>
    072 * <tr><td>['{'d'}']HH':'mm</td><td>{1}00:00</td><td>01:00</td><td>00:01</td><td>00:00</td></tr>
    073 * <tr><td>['{'dd'}']['&lt;'HH'&gt;']['('mm')']</td><td>{01}</td><td>&lt;01&gt;</td><td>(00)</td><td></td></tr>
    074 * <tr><td>[dHms]</td><td>1</td><td>1</td><td>1</td><td></td></tr>
    075 * </table>
    076 * <b>Note: Optional blocks cannot be nested.</b>
    077 *
    078 * @since 2.1
    079 */
    080public class DurationFormatUtils {
    081
    082    /**
    083     * Element that is parsed from the format pattern.
    084     */
    085    static class Token {
    086
    087        /** Empty array. */
    088        private static final Token[] EMPTY_ARRAY = {};
    089
    090        /**
    091         * Helper method to determine if a set of tokens contain a value
    092         *
    093         * @param tokens set to look in
    094         * @param value to look for
    095         * @return boolean {@code true} if contained
    096         */
    097        static boolean containsTokenWithValue(final Token[] tokens, final Object value) {
    098            return Stream.of(tokens).anyMatch(token -> token.getValue() == value);
    099        }
    100
    101        private final Object value;
    102        private int count;
    103        private int optionalIndex = -1;
    104
    105        /**
    106         * Wraps a token around a value. A value would be something like a 'Y'.
    107         *
    108         * @param value value to wrap, non-null.
    109         * @param optional whether the token is optional
    110         * @param optionalIndex the index of the optional token within the pattern
    111         */
    112        Token(final Object value, final boolean optional, final int optionalIndex) {
    113            this.value = Objects.requireNonNull(value, "value");
    114            this.count = 1;
    115            if (optional) {
    116                this.optionalIndex = optionalIndex;
    117            }
    118        }
    119
    120        /**
    121         * Supports equality of this Token to another Token.
    122         *
    123         * @param obj2 Object to consider equality of
    124         * @return boolean {@code true} if equal
    125         */
    126        @Override
    127        public boolean equals(final Object obj2) {
    128            if (obj2 instanceof Token) {
    129                final Token tok2 = (Token) obj2;
    130                if (this.value.getClass() != tok2.value.getClass()) {
    131                    return false;
    132                }
    133                if (this.count != tok2.count) {
    134                    return false;
    135                }
    136                if (this.value instanceof StringBuilder) {
    137                    return this.value.toString().equals(tok2.value.toString());
    138                }
    139                if (this.value instanceof Number) {
    140                    return this.value.equals(tok2.value);
    141                }
    142                return this.value == tok2.value;
    143            }
    144            return false;
    145        }
    146
    147        /**
    148         * Gets the current number of values represented
    149         *
    150         * @return int number of values represented
    151         */
    152        int getCount() {
    153            return count;
    154        }
    155
    156        /**
    157         * Gets the particular value this token represents.
    158         *
    159         * @return Object value, non-null.
    160         */
    161        Object getValue() {
    162            return value;
    163        }
    164
    165        /**
    166         * Returns a hash code for the token equal to the
    167         * hash code for the token's value. Thus 'TT' and 'TTTT'
    168         * will have the same hash code.
    169         *
    170         * @return The hash code for the token
    171         */
    172        @Override
    173        public int hashCode() {
    174            return this.value.hashCode();
    175        }
    176
    177        /**
    178         * Adds another one of the value
    179         */
    180        void increment() {
    181            count++;
    182        }
    183
    184        /**
    185         * Represents this token as a String.
    186         *
    187         * @return String representation of the token
    188         */
    189        @Override
    190        public String toString() {
    191            return StringUtils.repeat(this.value.toString(), this.count);
    192        }
    193    }
    194
    195    /**
    196     * Pattern used with {@link FastDateFormat} and {@link SimpleDateFormat}
    197     * for the ISO 8601 period format used in durations.
    198     *
    199     * @see org.apache.commons.lang3.time.FastDateFormat
    200     * @see java.text.SimpleDateFormat
    201     */
    202    public static final String ISO_EXTENDED_FORMAT_PATTERN = "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.SSS'S'";
    203
    204    static final String y = "y";
    205
    206    static final String M = "M";
    207
    208    static final String d = "d";
    209
    210    static final String H = "H";
    211
    212    static final String m = "m";
    213
    214    static final String s = "s";
    215
    216    static final String S = "S";
    217
    218    /**
    219     * The internal method to do the formatting.
    220     *
    221     * @param tokens  the tokens
    222     * @param years  the number of years
    223     * @param months  the number of months
    224     * @param days  the number of days
    225     * @param hours  the number of hours
    226     * @param minutes  the number of minutes
    227     * @param seconds  the number of seconds
    228     * @param milliseconds  the number of millis
    229     * @param padWithZeros  whether to pad
    230     * @return the formatted string
    231     */
    232    static String format(final Token[] tokens, final long years, final long months, final long days, final long hours, final long minutes,
    233            final long seconds,
    234            final long milliseconds, final boolean padWithZeros) {
    235        final StringBuilder buffer = new StringBuilder();
    236        boolean lastOutputSeconds = false;
    237        boolean lastOutputZero = false;
    238        int optionalStart = -1;
    239        boolean firstOptionalNonLiteral = false;
    240        int optionalIndex = -1;
    241        boolean inOptional = false;
    242        for (final Token token : tokens) {
    243            final Object value = token.getValue();
    244            final boolean isLiteral = value instanceof StringBuilder;
    245            final int count = token.getCount();
    246            if (optionalIndex != token.optionalIndex) {
    247              optionalIndex = token.optionalIndex;
    248              if (optionalIndex > -1) {
    249                //entering new optional block
    250                optionalStart = buffer.length();
    251                lastOutputZero = false;
    252                inOptional = true;
    253                firstOptionalNonLiteral = false;
    254              } else {
    255                //leaving optional block
    256                inOptional = false;
    257              }
    258            }
    259            if (isLiteral) {
    260               if (!inOptional || !lastOutputZero) {
    261                     buffer.append(value.toString());
    262               }
    263            } else if (value.equals(y)) {
    264                lastOutputSeconds = false;
    265                lastOutputZero = years == 0;
    266                if (!inOptional || !lastOutputZero) {
    267                    buffer.append(paddedValue(years, padWithZeros, count));
    268                }
    269            } else if (value.equals(M)) {
    270                lastOutputSeconds = false;
    271                lastOutputZero = months == 0;
    272                if (!inOptional || !lastOutputZero) {
    273                    buffer.append(paddedValue(months, padWithZeros, count));
    274                }
    275            } else if (value.equals(d)) {
    276                lastOutputSeconds = false;
    277                lastOutputZero = days == 0;
    278                if (!inOptional || !lastOutputZero) {
    279                    buffer.append(paddedValue(days, padWithZeros, count));
    280                }
    281            } else if (value.equals(H)) {
    282                lastOutputSeconds = false;
    283                lastOutputZero = hours == 0;
    284                if (!inOptional || !lastOutputZero) {
    285                    buffer.append(paddedValue(hours, padWithZeros, count));
    286                }
    287            } else if (value.equals(m)) {
    288                lastOutputSeconds = false;
    289                lastOutputZero = minutes == 0;
    290                if (!inOptional || !lastOutputZero) {
    291                    buffer.append(paddedValue(minutes, padWithZeros, count));
    292                }
    293            } else if (value.equals(s)) {
    294                lastOutputSeconds = true;
    295                lastOutputZero = seconds == 0;
    296                if (!inOptional || !lastOutputZero) {
    297                    buffer.append(paddedValue(seconds, padWithZeros, count));
    298                }
    299            } else if (value.equals(S)) {
    300                lastOutputZero = milliseconds == 0;
    301                if (!inOptional || !lastOutputZero) {
    302                    if (lastOutputSeconds) {
    303                        // ensure at least 3 digits are displayed even if padding is not selected
    304                        final int width = padWithZeros ? Math.max(3, count) : 3;
    305                        buffer.append(paddedValue(milliseconds, true, width));
    306                    } else {
    307                        buffer.append(paddedValue(milliseconds, padWithZeros, count));
    308                    }
    309                }
    310                lastOutputSeconds = false;
    311            }
    312            //as soon as we hit first nonliteral in optional, check for literal prefix
    313            if (inOptional && !isLiteral && !firstOptionalNonLiteral){
    314                firstOptionalNonLiteral = true;
    315                if (lastOutputZero) {
    316                    buffer.delete(optionalStart, buffer.length());
    317                }
    318            }
    319        }
    320        return buffer.toString();
    321    }
    322
    323    /**
    324     * Formats the time gap as a string, using the specified format, and padding with zeros.
    325     *
    326     * <p>This method formats durations using the days and lower fields of the
    327     * format pattern. Months and larger are not used.</p>
    328     *
    329     * @param durationMillis  the duration to format
    330     * @param format  the way in which to format the duration, not null
    331     * @return the formatted duration, not null
    332     * @throws IllegalArgumentException if durationMillis is negative
    333     */
    334    public static String formatDuration(final long durationMillis, final String format) {
    335        return formatDuration(durationMillis, format, true);
    336    }
    337
    338    /**
    339     * Formats the time gap as a string, using the specified format.
    340     * Padding the left-hand side of numbers with zeroes is optional.
    341     *
    342     * <p>This method formats durations using the days and lower fields of the
    343     * format pattern. Months and larger are not used.</p>
    344     *
    345     * @param durationMillis  the duration to format
    346     * @param format  the way in which to format the duration, not null
    347     * @param padWithZeros  whether to pad the left-hand side of numbers with 0's
    348     * @return the formatted duration, not null
    349     * @throws IllegalArgumentException if durationMillis is negative
    350     */
    351    public static String formatDuration(final long durationMillis, final String format, final boolean padWithZeros) {
    352        Validate.inclusiveBetween(0, Long.MAX_VALUE, durationMillis, "durationMillis must not be negative");
    353
    354        final Token[] tokens = lexx(format);
    355
    356        long days = 0;
    357        long hours = 0;
    358        long minutes = 0;
    359        long seconds = 0;
    360        long milliseconds = durationMillis;
    361
    362        if (Token.containsTokenWithValue(tokens, d)) {
    363            days = milliseconds / DateUtils.MILLIS_PER_DAY;
    364            milliseconds = milliseconds - (days * DateUtils.MILLIS_PER_DAY);
    365        }
    366        if (Token.containsTokenWithValue(tokens, H)) {
    367            hours = milliseconds / DateUtils.MILLIS_PER_HOUR;
    368            milliseconds = milliseconds - (hours * DateUtils.MILLIS_PER_HOUR);
    369        }
    370        if (Token.containsTokenWithValue(tokens, m)) {
    371            minutes = milliseconds / DateUtils.MILLIS_PER_MINUTE;
    372            milliseconds = milliseconds - (minutes * DateUtils.MILLIS_PER_MINUTE);
    373        }
    374        if (Token.containsTokenWithValue(tokens, s)) {
    375            seconds = milliseconds / DateUtils.MILLIS_PER_SECOND;
    376            milliseconds = milliseconds - (seconds * DateUtils.MILLIS_PER_SECOND);
    377        }
    378
    379        return format(tokens, 0, 0, days, hours, minutes, seconds, milliseconds, padWithZeros);
    380    }
    381
    382    /**
    383     * Formats the time gap as a string.
    384     *
    385     * <p>The format used is ISO 8601-like: {@code HH:mm:ss.SSS}.</p>
    386     *
    387     * @param durationMillis  the duration to format
    388     * @return the formatted duration, not null
    389     * @throws IllegalArgumentException if durationMillis is negative
    390     */
    391    public static String formatDurationHMS(final long durationMillis) {
    392        return formatDuration(durationMillis, "HH:mm:ss.SSS");
    393    }
    394    /**
    395     * Formats the time gap as a string.
    396     *
    397     * <p>The format used is the ISO 8601 period format.</p>
    398     *
    399     * <p>This method formats durations using the days and lower fields of the
    400     * ISO format pattern, such as P7D6TH5M4.321S.</p>
    401     *
    402     * @param durationMillis  the duration to format
    403     * @return the formatted duration, not null
    404     * @throws IllegalArgumentException if durationMillis is negative
    405     */
    406    public static String formatDurationISO(final long durationMillis) {
    407        return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false);
    408    }
    409    /**
    410     * Formats an elapsed time into a pluralization correct string.
    411     *
    412     * <p>This method formats durations using the days and lower fields of the
    413     * format pattern. Months and larger are not used.</p>
    414     *
    415     * @param durationMillis  the elapsed time to report in milliseconds
    416     * @param suppressLeadingZeroElements  suppresses leading 0 elements
    417     * @param suppressTrailingZeroElements  suppresses trailing 0 elements
    418     * @return the formatted text in days/hours/minutes/seconds, not null
    419     * @throws IllegalArgumentException if durationMillis is negative
    420     */
    421    public static String formatDurationWords(
    422        final long durationMillis,
    423        final boolean suppressLeadingZeroElements,
    424        final boolean suppressTrailingZeroElements) {
    425
    426        // This method is generally replaceable by the format method, but
    427        // there are a series of tweaks and special cases that require
    428        // trickery to replicate.
    429        String duration = formatDuration(durationMillis, "d' days 'H' hours 'm' minutes 's' seconds'");
    430        if (suppressLeadingZeroElements) {
    431            // this is a temporary marker on the front. Like ^ in regexp.
    432            duration = " " + duration;
    433            String tmp = StringUtils.replaceOnce(duration, " 0 days", StringUtils.EMPTY);
    434            if (tmp.length() != duration.length()) {
    435                duration = tmp;
    436                tmp = StringUtils.replaceOnce(duration, " 0 hours", StringUtils.EMPTY);
    437                if (tmp.length() != duration.length()) {
    438                    duration = tmp;
    439                    tmp = StringUtils.replaceOnce(duration, " 0 minutes", StringUtils.EMPTY);
    440                    duration = tmp;
    441                }
    442            }
    443            if (!duration.isEmpty()) {
    444                // strip the space off again
    445                duration = duration.substring(1);
    446            }
    447        }
    448        if (suppressTrailingZeroElements) {
    449            String tmp = StringUtils.replaceOnce(duration, " 0 seconds", StringUtils.EMPTY);
    450            if (tmp.length() != duration.length()) {
    451                duration = tmp;
    452                tmp = StringUtils.replaceOnce(duration, " 0 minutes", StringUtils.EMPTY);
    453                if (tmp.length() != duration.length()) {
    454                    duration = tmp;
    455                    tmp = StringUtils.replaceOnce(duration, " 0 hours", StringUtils.EMPTY);
    456                    if (tmp.length() != duration.length()) {
    457                        duration = StringUtils.replaceOnce(tmp, " 0 days", StringUtils.EMPTY);
    458                    }
    459                }
    460            }
    461        }
    462        // handle plurals
    463        duration = " " + duration;
    464        duration = StringUtils.replaceOnce(duration, " 1 seconds", " 1 second");
    465        duration = StringUtils.replaceOnce(duration, " 1 minutes", " 1 minute");
    466        duration = StringUtils.replaceOnce(duration, " 1 hours", " 1 hour");
    467        duration = StringUtils.replaceOnce(duration, " 1 days", " 1 day");
    468        return duration.trim();
    469    }
    470    /**
    471     * Formats the time gap as a string, using the specified format.
    472     * Padding the left-hand side of numbers with zeroes is optional.
    473     *
    474     * @param startMillis  the start of the duration
    475     * @param endMillis  the end of the duration
    476     * @param format  the way in which to format the duration, not null
    477     * @return the formatted duration, not null
    478     * @throws IllegalArgumentException if startMillis is greater than endMillis
    479     */
    480    public static String formatPeriod(final long startMillis, final long endMillis, final String format) {
    481        return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault());
    482    }
    483    /**
    484     * <p>Formats the time gap as a string, using the specified format.
    485     * Padding the left-hand side of numbers with zeroes is optional and
    486     * the time zone may be specified.
    487     *
    488     * <p>When calculating the difference between months/days, it chooses to
    489     * calculate months first. So when working out the number of months and
    490     * days between January 15th and March 10th, it choose 1 month and
    491     * 23 days gained by choosing January-&gt;February = 1 month and then
    492     * calculating days forwards, and not the 1 month and 26 days gained by
    493     * choosing March -&gt; February = 1 month and then calculating days
    494     * backwards.</p>
    495     *
    496     * <p>For more control, the <a href="https://www.joda.org/joda-time/">Joda-Time</a>
    497     * library is recommended.</p>
    498     *
    499     * @param startMillis  the start of the duration
    500     * @param endMillis  the end of the duration
    501     * @param format  the way in which to format the duration, not null
    502     * @param padWithZeros  whether to pad the left-hand side of numbers with 0's
    503     * @param timezone  the millis are defined in
    504     * @return the formatted duration, not null
    505     * @throws IllegalArgumentException if startMillis is greater than endMillis
    506     */
    507    public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros,
    508            final TimeZone timezone) {
    509        Validate.isTrue(startMillis <= endMillis, "startMillis must not be greater than endMillis");
    510
    511
    512        // Used to optimise for differences under 28 days and
    513        // called formatDuration(millis, format); however this did not work
    514        // over leap years.
    515        // TODO: Compare performance to see if anything was lost by
    516        // losing this optimisation.
    517
    518        final Token[] tokens = lexx(format);
    519
    520        // time zones get funky around 0, so normalizing everything to GMT
    521        // stops the hours being off
    522        final Calendar start = Calendar.getInstance(timezone);
    523        start.setTime(new Date(startMillis));
    524        final Calendar end = Calendar.getInstance(timezone);
    525        end.setTime(new Date(endMillis));
    526
    527        // initial estimates
    528        int milliseconds = end.get(Calendar.MILLISECOND) - start.get(Calendar.MILLISECOND);
    529        int seconds = end.get(Calendar.SECOND) - start.get(Calendar.SECOND);
    530        int minutes = end.get(Calendar.MINUTE) - start.get(Calendar.MINUTE);
    531        int hours = end.get(Calendar.HOUR_OF_DAY) - start.get(Calendar.HOUR_OF_DAY);
    532        int days = end.get(Calendar.DAY_OF_MONTH) - start.get(Calendar.DAY_OF_MONTH);
    533        int months = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
    534        int years = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
    535
    536        // each initial estimate is adjusted in case it is under 0
    537        while (milliseconds < 0) {
    538            milliseconds += 1000;
    539            seconds -= 1;
    540        }
    541        while (seconds < 0) {
    542            seconds += 60;
    543            minutes -= 1;
    544        }
    545        while (minutes < 0) {
    546            minutes += 60;
    547            hours -= 1;
    548        }
    549        while (hours < 0) {
    550            hours += 24;
    551            days -= 1;
    552        }
    553
    554        if (Token.containsTokenWithValue(tokens, M)) {
    555            while (days < 0) {
    556                days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
    557                months -= 1;
    558                start.add(Calendar.MONTH, 1);
    559            }
    560
    561            while (months < 0) {
    562                months += 12;
    563                years -= 1;
    564            }
    565
    566            if (!Token.containsTokenWithValue(tokens, y) && years != 0) {
    567                while (years != 0) {
    568                    months += 12 * years;
    569                    years = 0;
    570                }
    571            }
    572        } else {
    573            // there are no M's in the format string
    574
    575            if (!Token.containsTokenWithValue(tokens, y)) {
    576                int target = end.get(Calendar.YEAR);
    577                if (months < 0) {
    578                    // target is end-year -1
    579                    target -= 1;
    580                }
    581
    582                while (start.get(Calendar.YEAR) != target) {
    583                    days += start.getActualMaximum(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR);
    584
    585                    // Not sure I grok why this is needed, but the brutal tests show it is
    586                    if (start instanceof GregorianCalendar &&
    587                            start.get(Calendar.MONTH) == Calendar.FEBRUARY &&
    588                            start.get(Calendar.DAY_OF_MONTH) == 29) {
    589                        days += 1;
    590                    }
    591
    592                    start.add(Calendar.YEAR, 1);
    593
    594                    days += start.get(Calendar.DAY_OF_YEAR);
    595                }
    596
    597                years = 0;
    598            }
    599
    600            while (start.get(Calendar.MONTH) != end.get(Calendar.MONTH)) {
    601                days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
    602                start.add(Calendar.MONTH, 1);
    603            }
    604
    605            months = 0;
    606
    607            while (days < 0) {
    608                days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
    609                months -= 1;
    610                start.add(Calendar.MONTH, 1);
    611            }
    612
    613        }
    614
    615        // The rest of this code adds in values that
    616        // aren't requested. This allows the user to ask for the
    617        // number of months and get the real count and not just 0->11.
    618
    619        if (!Token.containsTokenWithValue(tokens, d)) {
    620            hours += 24 * days;
    621            days = 0;
    622        }
    623        if (!Token.containsTokenWithValue(tokens, H)) {
    624            minutes += 60 * hours;
    625            hours = 0;
    626        }
    627        if (!Token.containsTokenWithValue(tokens, m)) {
    628            seconds += 60 * minutes;
    629            minutes = 0;
    630        }
    631        if (!Token.containsTokenWithValue(tokens, s)) {
    632            milliseconds += 1000 * seconds;
    633            seconds = 0;
    634        }
    635
    636        return format(tokens, years, months, days, hours, minutes, seconds, milliseconds, padWithZeros);
    637    }
    638    /**
    639     * Formats the time gap as a string.
    640     *
    641     * <p>The format used is the ISO 8601 period format.</p>
    642     *
    643     * @param startMillis  the start of the duration to format
    644     * @param endMillis  the end of the duration to format
    645     * @return the formatted duration, not null
    646     * @throws IllegalArgumentException if startMillis is greater than endMillis
    647     */
    648    public static String formatPeriodISO(final long startMillis, final long endMillis) {
    649        return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault());
    650    }
    651    /**
    652     * Parses a classic date format string into Tokens
    653     *
    654     * @param format  the format to parse, not null
    655     * @return array of Token[]
    656     */
    657    static Token[] lexx(final String format) {
    658        final ArrayList<Token> list = new ArrayList<>(format.length());
    659
    660        boolean inLiteral = false;
    661        // Although the buffer is stored in a Token, the Tokens are only
    662        // used internally, so cannot be accessed by other threads
    663        StringBuilder buffer = null;
    664        Token previous = null;
    665        boolean inOptional = false;
    666        int optionalIndex = -1;
    667        for (int i = 0; i < format.length(); i++) {
    668            final char ch = format.charAt(i);
    669            if (inLiteral && ch != '\'') {
    670                buffer.append(ch); // buffer can't be null if inLiteral is true
    671                continue;
    672            }
    673            String value = null;
    674            switch (ch) {
    675            // TODO: Need to handle escaping of '
    676            case '[':
    677                if (inOptional) {
    678                  throw new IllegalArgumentException("Nested optional block at index: "+i);
    679                }
    680                optionalIndex++;
    681                inOptional = true;
    682                break;
    683            case ']':
    684                if (!inOptional) {
    685                  throw new IllegalArgumentException("Attempting to close unopened optional block at index: "+i);
    686                }
    687                inOptional = false;
    688                break;
    689            case '\'':
    690                if (inLiteral) {
    691                    buffer = null;
    692                    inLiteral = false;
    693                } else {
    694                    buffer = new StringBuilder();
    695                    list.add(new Token(buffer, inOptional, optionalIndex));
    696                    inLiteral = true;
    697                }
    698                break;
    699            case 'y':
    700                value = y;
    701                break;
    702            case 'M':
    703                value = M;
    704                break;
    705            case 'd':
    706                value = d;
    707                break;
    708            case 'H':
    709                value = H;
    710                break;
    711            case 'm':
    712                value = m;
    713                break;
    714            case 's':
    715                value = s;
    716                break;
    717            case 'S':
    718                value = S;
    719                break;
    720            default:
    721                if (buffer == null) {
    722                    buffer = new StringBuilder();
    723                    list.add(new Token(buffer, inOptional, optionalIndex));
    724                }
    725                buffer.append(ch);
    726            }
    727
    728            if (value != null) {
    729                if (previous != null && previous.getValue().equals(value)) {
    730                    previous.increment();
    731                } else {
    732                    final Token token = new Token(value, inOptional, optionalIndex);
    733                    list.add(token);
    734                    previous = token;
    735                }
    736                buffer = null;
    737            }
    738        }
    739        if (inLiteral) { // i.e. we have not found the end of the literal
    740            throw new IllegalArgumentException("Unmatched quote in format: " + format);
    741        }
    742        if (inOptional) { // i.e. we have not found the end of the literal
    743            throw new IllegalArgumentException("Unmatched optional in format: " + format);
    744        }
    745        return list.toArray(Token.EMPTY_ARRAY);
    746    }
    747
    748    /**
    749     * Converts a {@code long} to a {@link String} with optional
    750     * zero padding.
    751     *
    752     * @param value the value to convert
    753     * @param padWithZeros whether to pad with zeroes
    754     * @param count the size to pad to (ignored if {@code padWithZeros} is false)
    755     * @return the string result
    756     */
    757    private static String paddedValue(final long value, final boolean padWithZeros, final int count) {
    758        final String longString = Long.toString(value);
    759        return padWithZeros ? StringUtils.leftPad(longString, count, '0') : longString;
    760    }
    761
    762    /**
    763     * DurationFormatUtils instances should NOT be constructed in standard programming.
    764     *
    765     * <p>This constructor is public to permit tools that require a JavaBean instance
    766     * to operate.</p>
    767     */
    768    public DurationFormatUtils() {
    769    }
    770
    771}