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