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