TimeUtils.java revision cba2e2c881e8e16ea5025b564c94320174d65f01
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package android.support.v4.util;
18
19import java.io.PrintWriter;
20
21/**
22 * Useful time utilities that are not available on all versions of Android.
23 */
24public class TimeUtils {
25    /** @hide Field length that can hold 999 days of time */
26    public static final int HUNDRED_DAY_FIELD_LEN = 19;
27
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;
31
32    private static final Object sFormatSync = new Object();
33    private static char[] sFormatStr = new char[HUNDRED_DAY_FIELD_LEN+5];
34
35    static private int accumField(int amt, int suffix, boolean always, int zeropad) {
36        if (amt > 99 || (always && zeropad >= 3)) {
37            return 3+suffix;
38        }
39        if (amt > 9 || (always && zeropad >= 2)) {
40            return 2+suffix;
41        }
42        if (always || amt > 0) {
43            return 1+suffix;
44        }
45        return 0;
46    }
47
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) {
53                int dig = amt/100;
54                formatStr[pos] = (char)(dig + '0');
55                pos++;
56                amt -= (dig*100);
57            }
58            if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
59                int dig = amt/10;
60                formatStr[pos] = (char)(dig + '0');
61                pos++;
62                amt -= (dig*10);
63            }
64            formatStr[pos] = (char)(amt + '0');
65            pos++;
66            formatStr[pos] = suffix;
67            pos++;
68        }
69        return pos;
70    }
71
72    private static int formatDurationLocked(long duration, int fieldLen) {
73        if (sFormatStr.length < fieldLen) {
74            sFormatStr = new char[fieldLen];
75        }
76
77        char[] formatStr = sFormatStr;
78
79        if (duration == 0) {
80            int pos = 0;
81            fieldLen -= 1;
82            while (pos < fieldLen) {
83                formatStr[pos] = ' ';
84            }
85            formatStr[pos] = '0';
86            return pos+1;
87        }
88
89        char prefix;
90        if (duration > 0) {
91            prefix = '+';
92        } else {
93            prefix = '-';
94            duration = -duration;
95        }
96
97        int millis = (int)(duration%1000);
98        int seconds = (int) Math.floor(duration / 1000);
99        int days = 0, hours = 0, minutes = 0;
100
101        if (seconds > SECONDS_PER_DAY) {
102            days = seconds / SECONDS_PER_DAY;
103            seconds -= days * SECONDS_PER_DAY;
104        }
105        if (seconds > SECONDS_PER_HOUR) {
106            hours = seconds / SECONDS_PER_HOUR;
107            seconds -= hours * SECONDS_PER_HOUR;
108        }
109        if (seconds > SECONDS_PER_MINUTE) {
110            minutes = seconds / SECONDS_PER_MINUTE;
111            seconds -= minutes * SECONDS_PER_MINUTE;
112        }
113
114        int pos = 0;
115
116        if (fieldLen != 0) {
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] = ' ';
124                pos++;
125                myLen++;
126            }
127        }
128
129        formatStr[pos] = prefix;
130        pos++;
131
132        int start = pos;
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';
140        return pos + 1;
141    }
142
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);
148        }
149    }
150
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));
156        }
157    }
158
159    /** @hide Just for debugging; not internationalized. */
160    public static void formatDuration(long duration, PrintWriter pw) {
161        formatDuration(duration, pw, 0);
162    }
163
164    /** @hide Just for debugging; not internationalized. */
165    public static void formatDuration(long time, long now, PrintWriter pw) {
166        if (time == 0) {
167            pw.print("--");
168            return;
169        }
170        formatDuration(time-now, pw, 0);
171    }
172}
173