2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.support.v2.util;
19 import java.io.PrintWriter;
22 * Useful time utilities that are not available on all versions of Android.
24 public class TimeUtils {
25 /** @hide Field length that can hold 999 days of time */
26 public static final int HUNDRED_DAY_FIELD_LEN = 19;
28 private static final int SECONDS_PER_MINUTE = 60;
29 private static final int SECONDS_PER_HOUR = 60 * 60;
30 private static final int SECONDS_PER_DAY = 24 * 60 * 60;
32 private static final Object sFormatSync = new Object();
33 private static char[] sFormatStr = new char[HUNDRED_DAY_FIELD_LEN+5];
35 static private int accumField(int amt, int suffix, boolean always, int zeropad) {
36 if (amt > 99 || (always && zeropad >= 3)) {
39 if (amt > 9 || (always && zeropad >= 2)) {
42 if (always || amt > 0) {
48 static private int printField(char[] formatStr, int amt, char suffix, int pos,
49 boolean always, int zeropad) {
50 if (always || amt > 0) {
51 final int startPos = pos;
52 if ((always && zeropad >= 3) || amt > 99) {
54 formatStr[pos] = (char)(dig + '0');
58 if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
60 formatStr[pos] = (char)(dig + '0');
64 formatStr[pos] = (char)(amt + '0');
66 formatStr[pos] = suffix;
72 private static int formatDurationLocked(long duration, int fieldLen) {
73 if (sFormatStr.length < fieldLen) {
74 sFormatStr = new char[fieldLen];
77 char[] formatStr = sFormatStr;
82 while (pos < fieldLen) {
97 int millis = (int)(duration%1000);
98 int seconds = (int) Math.floor(duration / 1000);
99 int days = 0, hours = 0, minutes = 0;
101 if (seconds > SECONDS_PER_DAY) {
102 days = seconds / SECONDS_PER_DAY;
103 seconds -= days * SECONDS_PER_DAY;
105 if (seconds > SECONDS_PER_HOUR) {
106 hours = seconds / SECONDS_PER_HOUR;
107 seconds -= hours * SECONDS_PER_HOUR;
109 if (seconds > SECONDS_PER_MINUTE) {
110 minutes = seconds / SECONDS_PER_MINUTE;
111 seconds -= minutes * SECONDS_PER_MINUTE;
117 int myLen = accumField(days, 1, false, 0);
118 myLen += accumField(hours, 1, myLen > 0, 2);
119 myLen += accumField(minutes, 1, myLen > 0, 2);
120 myLen += accumField(seconds, 1, myLen > 0, 2);
121 myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1;
122 while (myLen < fieldLen) {
123 formatStr[pos] = ' ';
129 formatStr[pos] = prefix;
133 boolean zeropad = fieldLen != 0;
134 pos = printField(formatStr, days, 'd', pos, false, 0);
135 pos = printField(formatStr, hours, 'h', pos, pos != start, zeropad ? 2 : 0);
136 pos = printField(formatStr, minutes, 'm', pos, pos != start, zeropad ? 2 : 0);
137 pos = printField(formatStr, seconds, 's', pos, pos != start, zeropad ? 2 : 0);
138 pos = printField(formatStr, millis, 'm', pos, true, (zeropad && pos != start) ? 3 : 0);
139 formatStr[pos] = 's';
143 /** @hide Just for debugging; not internationalized. */
144 public static void formatDuration(long duration, StringBuilder builder) {
145 synchronized (sFormatSync) {
146 int len = formatDurationLocked(duration, 0);
147 builder.append(sFormatStr, 0, len);
151 /** @hide Just for debugging; not internationalized. */
152 public static void formatDuration(long duration, PrintWriter pw, int fieldLen) {
153 synchronized (sFormatSync) {
154 int len = formatDurationLocked(duration, fieldLen);
155 pw.print(new String(sFormatStr, 0, len));
159 /** @hide Just for debugging; not internationalized. */
160 public static void formatDuration(long duration, PrintWriter pw) {
161 formatDuration(duration, pw, 0);
164 /** @hide Just for debugging; not internationalized. */
165 public static void formatDuration(long time, long now, PrintWriter pw) {
170 formatDuration(time-now, pw, 0);