TimeUtils.java revision b31c3281d870e9abb673db239234d580dcc4feff
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 androidx.core.util;
18
19import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20
21import androidx.annotation.RestrictTo;
22
23import java.io.PrintWriter;
24
25/**
26 * Helper for accessing features in {@link android.util.TimeUtils}.
27 *
28 * @hide
29 */
30@RestrictTo(LIBRARY_GROUP)
31public final class TimeUtils {
32    /** @hide Field length that can hold 999 days of time */
33    @RestrictTo(LIBRARY_GROUP)
34    public static final int HUNDRED_DAY_FIELD_LEN = 19;
35
36    private static final int SECONDS_PER_MINUTE = 60;
37    private static final int SECONDS_PER_HOUR = 60 * 60;
38    private static final int SECONDS_PER_DAY = 24 * 60 * 60;
39
40    private static final Object sFormatSync = new Object();
41    private static char[] sFormatStr = new char[HUNDRED_DAY_FIELD_LEN+5];
42
43    static private int accumField(int amt, int suffix, boolean always, int zeropad) {
44        if (amt > 99 || (always && zeropad >= 3)) {
45            return 3+suffix;
46        }
47        if (amt > 9 || (always && zeropad >= 2)) {
48            return 2+suffix;
49        }
50        if (always || amt > 0) {
51            return 1+suffix;
52        }
53        return 0;
54    }
55
56    static private int printField(char[] formatStr, int amt, char suffix, int pos,
57            boolean always, int zeropad) {
58        if (always || amt > 0) {
59            final int startPos = pos;
60            if ((always && zeropad >= 3) || amt > 99) {
61                int dig = amt/100;
62                formatStr[pos] = (char)(dig + '0');
63                pos++;
64                amt -= (dig*100);
65            }
66            if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
67                int dig = amt/10;
68                formatStr[pos] = (char)(dig + '0');
69                pos++;
70                amt -= (dig*10);
71            }
72            formatStr[pos] = (char)(amt + '0');
73            pos++;
74            formatStr[pos] = suffix;
75            pos++;
76        }
77        return pos;
78    }
79
80    private static int formatDurationLocked(long duration, int fieldLen) {
81        if (sFormatStr.length < fieldLen) {
82            sFormatStr = new char[fieldLen];
83        }
84
85        char[] formatStr = sFormatStr;
86
87        if (duration == 0) {
88            int pos = 0;
89            fieldLen -= 1;
90            while (pos < fieldLen) {
91                formatStr[pos] = ' ';
92            }
93            formatStr[pos] = '0';
94            return pos+1;
95        }
96
97        char prefix;
98        if (duration > 0) {
99            prefix = '+';
100        } else {
101            prefix = '-';
102            duration = -duration;
103        }
104
105        int millis = (int)(duration%1000);
106        int seconds = (int) Math.floor(duration / 1000);
107        int days = 0, hours = 0, minutes = 0;
108
109        if (seconds > SECONDS_PER_DAY) {
110            days = seconds / SECONDS_PER_DAY;
111            seconds -= days * SECONDS_PER_DAY;
112        }
113        if (seconds > SECONDS_PER_HOUR) {
114            hours = seconds / SECONDS_PER_HOUR;
115            seconds -= hours * SECONDS_PER_HOUR;
116        }
117        if (seconds > SECONDS_PER_MINUTE) {
118            minutes = seconds / SECONDS_PER_MINUTE;
119            seconds -= minutes * SECONDS_PER_MINUTE;
120        }
121
122        int pos = 0;
123
124        if (fieldLen != 0) {
125            int myLen = accumField(days, 1, false, 0);
126            myLen += accumField(hours, 1, myLen > 0, 2);
127            myLen += accumField(minutes, 1, myLen > 0, 2);
128            myLen += accumField(seconds, 1, myLen > 0, 2);
129            myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1;
130            while (myLen < fieldLen) {
131                formatStr[pos] = ' ';
132                pos++;
133                myLen++;
134            }
135        }
136
137        formatStr[pos] = prefix;
138        pos++;
139
140        int start = pos;
141        boolean zeropad = fieldLen != 0;
142        pos = printField(formatStr, days, 'd', pos, false, 0);
143        pos = printField(formatStr, hours, 'h', pos, pos != start, zeropad ? 2 : 0);
144        pos = printField(formatStr, minutes, 'm', pos, pos != start, zeropad ? 2 : 0);
145        pos = printField(formatStr, seconds, 's', pos, pos != start, zeropad ? 2 : 0);
146        pos = printField(formatStr, millis, 'm', pos, true, (zeropad && pos != start) ? 3 : 0);
147        formatStr[pos] = 's';
148        return pos + 1;
149    }
150
151    /** @hide Just for debugging; not internationalized. */
152    @RestrictTo(LIBRARY_GROUP)
153    public static void formatDuration(long duration, StringBuilder builder) {
154        synchronized (sFormatSync) {
155            int len = formatDurationLocked(duration, 0);
156            builder.append(sFormatStr, 0, len);
157        }
158    }
159
160    /** @hide Just for debugging; not internationalized. */
161    @RestrictTo(LIBRARY_GROUP)
162    public static void formatDuration(long duration, PrintWriter pw, int fieldLen) {
163        synchronized (sFormatSync) {
164            int len = formatDurationLocked(duration, fieldLen);
165            pw.print(new String(sFormatStr, 0, len));
166        }
167    }
168
169    /** @hide Just for debugging; not internationalized. */
170    @RestrictTo(LIBRARY_GROUP)
171    public static void formatDuration(long duration, PrintWriter pw) {
172        formatDuration(duration, pw, 0);
173    }
174
175    /** @hide Just for debugging; not internationalized. */
176    @RestrictTo(LIBRARY_GROUP)
177    public static void formatDuration(long time, long now, PrintWriter pw) {
178        if (time == 0) {
179            pw.print("--");
180            return;
181        }
182        formatDuration(time-now, pw, 0);
183    }
184
185    private TimeUtils() {}
186}
187