Java Second appendSecondsToEncodeOutput(StringBuilder sb, int sec, int fsec, int precision, boolean fillzeros)

Here you can find the source of appendSecondsToEncodeOutput(StringBuilder sb, int sec, int fsec, int precision, boolean fillzeros)

Description

Append sections and fractional seconds (if any) at *cp.

License

Apache License

Declaration

public static void appendSecondsToEncodeOutput(StringBuilder sb, int sec, int fsec, int precision,
        boolean fillzeros) 

Method Source Code

//package com.java2s;
/**/*from  w  w w .  j a v  a  2s.  c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static int MAX_FRACTION_LENGTH = 6;

    /**
     * Append sections and fractional seconds (if any) at *cp.
     * precision is the max number of fraction digits, fillzeros says to
     * pad to two integral-seconds digits.
     * Note that any sign is stripped from the input seconds values.
     *
     * This method is originated form AppendSeconds in datetime.c of PostgreSQL.
     */
    public static void appendSecondsToEncodeOutput(StringBuilder sb, int sec, int fsec, int precision,
            boolean fillzeros) {
        if (fsec == 0) {
            if (fillzeros)
                sb.append(String.format("%02d", Math.abs(sec)));
            else
                sb.append(String.format("%d", Math.abs(sec)));
        } else {
            if (fillzeros) {
                sb.append(String.format("%02d", Math.abs(sec)));
            } else {
                sb.append(String.format("%d", Math.abs(sec)));
            }

            if (precision > MAX_FRACTION_LENGTH) {
                precision = MAX_FRACTION_LENGTH;
            }

            if (precision > 0) {
                char[] fracChars = String.valueOf(fsec).toCharArray();
                char[] resultChars = new char[MAX_FRACTION_LENGTH];

                int numFillZero = MAX_FRACTION_LENGTH - fracChars.length;
                for (int i = 0, fracIdx = 0; i < MAX_FRACTION_LENGTH; i++) {
                    if (i < numFillZero) {
                        resultChars[i] = '0';
                    } else {
                        resultChars[i] = fracChars[fracIdx];
                        fracIdx++;
                    }
                }
                sb.append(".").append(resultChars, 0, precision);
            }
            trimTrailingZeros(sb);
        }
    }

    /**
     * ... resulting from printing numbers with full precision.
     *
     * Before Postgres 8.4, this always left at least 2 fractional digits,
     * but conversations on the lists suggest this isn't desired
     * since showing '0.10' is misleading with values of precision(1).
     *
     * This method is originated form AppendSeconds in datetime.c of PostgreSQL.
     * @param sb
     */
    public static void trimTrailingZeros(StringBuilder sb) {
        int len = sb.length();
        while (len > 1 && sb.charAt(len - 1) == '0' && sb.charAt(len - 2) != '.') {
            len--;
            sb.setLength(len);
        }
    }
}

Related

  1. addSecond(Date beginDate, int addcount)
  2. addSeconds(Date date, int m)
  3. addTimes(String dateTime, int second)
  4. advanceCurrentTimeSeconds()
  5. appendSeconds(StringBuilder time, long seconds)
  6. asStringInSeconds(long duration)
  7. bytesPerSecond(long totalByteCount, long elapsed)
  8. calculateSeconds(long duration)
  9. countSecond(Long time)