The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 17 | package android.util; |
| 18 | |
Neil Fuller | 6caa954 | 2018-10-03 13:59:29 +0100 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Mathew Inwood | b407568 | 2018-08-14 17:32:44 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Mathew Inwood | 55418ea | 2018-12-20 15:30:45 +0000 | [diff] [blame^] | 22 | import android.os.Build; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 23 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
Neil Fuller | 69ffbd6 | 2018-11-16 21:14:13 +0000 | [diff] [blame] | 25 | import libcore.timezone.CountryTimeZones; |
| 26 | import libcore.timezone.CountryTimeZones.TimeZoneMapping; |
| 27 | import libcore.timezone.TimeZoneFinder; |
Neil Fuller | b8383a1 | 2018-11-16 21:46:33 +0000 | [diff] [blame] | 28 | import libcore.timezone.ZoneInfoDB; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Neil Fuller | 11351b6 | 2018-01-09 18:38:08 +0000 | [diff] [blame] | 30 | import java.io.PrintWriter; |
| 31 | import java.text.SimpleDateFormat; |
Neil Fuller | 6caa954 | 2018-10-03 13:59:29 +0100 | [diff] [blame] | 32 | import java.util.ArrayList; |
Neil Fuller | 11351b6 | 2018-01-09 18:38:08 +0000 | [diff] [blame] | 33 | import java.util.Calendar; |
Neil Fuller | 6caa954 | 2018-10-03 13:59:29 +0100 | [diff] [blame] | 34 | import java.util.Collections; |
Neil Fuller | 11351b6 | 2018-01-09 18:38:08 +0000 | [diff] [blame] | 35 | import java.util.Date; |
Neil Fuller | 6caa954 | 2018-10-03 13:59:29 +0100 | [diff] [blame] | 36 | import java.util.List; |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | /** |
| 39 | * A class containing utility methods related to time zones. |
| 40 | */ |
| 41 | public class TimeUtils { |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 42 | /** @hide */ public TimeUtils() {} |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 43 | /** {@hide} */ |
| 44 | private static SimpleDateFormat sLoggingFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Wink Saville | a27421a | 2012-03-01 10:25:48 -0800 | [diff] [blame] | 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Tries to return a time zone that would have had the specified offset |
| 48 | * and DST value at the specified moment in the specified country. |
| 49 | * Returns null if no suitable zone could be found. |
| 50 | */ |
Neil Fuller | 1e49f6c | 2017-03-31 13:05:01 +0100 | [diff] [blame] | 51 | public static java.util.TimeZone getTimeZone( |
| 52 | int offset, boolean dst, long when, String country) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
Neil Fuller | 1e49f6c | 2017-03-31 13:05:01 +0100 | [diff] [blame] | 54 | android.icu.util.TimeZone icuTimeZone = getIcuTimeZone(offset, dst, when, country); |
| 55 | // We must expose a java.util.TimeZone here for API compatibility because this is a public |
| 56 | // API method. |
| 57 | return icuTimeZone != null ? java.util.TimeZone.getTimeZone(icuTimeZone.getID()) : null; |
Wink Saville | a27421a | 2012-03-01 10:25:48 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Neil Fuller | 1e49f6c | 2017-03-31 13:05:01 +0100 | [diff] [blame] | 61 | * Tries to return a frozen ICU time zone that would have had the specified offset |
| 62 | * and DST value at the specified moment in the specified country. |
| 63 | * Returns null if no suitable zone could be found. |
| 64 | */ |
| 65 | private static android.icu.util.TimeZone getIcuTimeZone( |
| 66 | int offset, boolean dst, long when, String country) { |
| 67 | if (country == null) { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | android.icu.util.TimeZone bias = android.icu.util.TimeZone.getDefault(); |
| 72 | return TimeZoneFinder.getInstance() |
| 73 | .lookupTimeZoneByCountryAndOffset(country, offset, dst, when, bias); |
| 74 | } |
| 75 | |
| 76 | /** |
Neil Fuller | 6caa954 | 2018-10-03 13:59:29 +0100 | [diff] [blame] | 77 | * Returns time zone IDs for time zones known to be associated with a country. |
| 78 | * |
| 79 | * <p>The list returned may be different from other on-device sources like |
| 80 | * {@link android.icu.util.TimeZone#getRegion(String)} as it can be curated to avoid |
| 81 | * contentious mappings. |
| 82 | * |
| 83 | * @param countryCode the ISO 3166-1 alpha-2 code for the country as can be obtained using |
| 84 | * {@link java.util.Locale#getCountry()} |
| 85 | * @return IDs that can be passed to {@link java.util.TimeZone#getTimeZone(String)} or similar |
| 86 | * methods, or {@code null} if the countryCode is unrecognized |
| 87 | */ |
| 88 | public static @Nullable List<String> getTimeZoneIdsForCountryCode(@NonNull String countryCode) { |
| 89 | if (countryCode == null) { |
| 90 | throw new NullPointerException("countryCode == null"); |
| 91 | } |
| 92 | TimeZoneFinder timeZoneFinder = TimeZoneFinder.getInstance(); |
| 93 | CountryTimeZones countryTimeZones = |
| 94 | timeZoneFinder.lookupCountryTimeZones(countryCode.toLowerCase()); |
| 95 | if (countryTimeZones == null) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | List<String> timeZoneIds = new ArrayList<>(); |
| 100 | for (TimeZoneMapping timeZoneMapping : countryTimeZones.getTimeZoneMappings()) { |
| 101 | if (timeZoneMapping.showInPicker) { |
| 102 | timeZoneIds.add(timeZoneMapping.timeZoneId); |
| 103 | } |
| 104 | } |
| 105 | return Collections.unmodifiableList(timeZoneIds); |
| 106 | } |
| 107 | |
| 108 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | * Returns a String indicating the version of the time zone database currently |
| 110 | * in use. The format of the string is dependent on the underlying time zone |
| 111 | * database implementation, but will typically contain the year in which the database |
| 112 | * was updated plus a letter from a to z indicating changes made within that year. |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 113 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | * <p>Time zone database updates should be expected to occur periodically due to |
| 115 | * political and legal changes that cannot be anticipated in advance. Therefore, |
| 116 | * when computing the UTC time for a future event, applications should be aware that |
| 117 | * the results may differ following a time zone database update. This method allows |
| 118 | * applications to detect that a database change has occurred, and to recalculate any |
| 119 | * cached times accordingly. |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 120 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | * <p>The time zone database may be assumed to change only when the device runtime |
| 122 | * is restarted. Therefore, it is not necessary to re-query the database version |
| 123 | * during the lifetime of an activity. |
| 124 | */ |
| 125 | public static String getTimeZoneDatabaseVersion() { |
Elliott Hughes | 4828943 | 2013-04-26 11:30:54 -0700 | [diff] [blame] | 126 | return ZoneInfoDB.getInstance().getVersion(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | } |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 128 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 129 | /** @hide Field length that can hold 999 days of time */ |
| 130 | public static final int HUNDRED_DAY_FIELD_LEN = 19; |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 131 | |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 132 | private static final int SECONDS_PER_MINUTE = 60; |
| 133 | private static final int SECONDS_PER_HOUR = 60 * 60; |
| 134 | private static final int SECONDS_PER_DAY = 24 * 60 * 60; |
| 135 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 136 | /** @hide */ |
| 137 | public static final long NANOS_PER_MS = 1000000; |
| 138 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 139 | private static final Object sFormatSync = new Object(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 140 | private static char[] sFormatStr = new char[HUNDRED_DAY_FIELD_LEN+10]; |
| 141 | private static char[] sTmpFormatStr = new char[HUNDRED_DAY_FIELD_LEN+10]; |
Jeff Sharkey | eaaf396 | 2012-07-21 12:37:26 -0700 | [diff] [blame] | 142 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 143 | static private int accumField(int amt, int suffix, boolean always, int zeropad) { |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 144 | if (amt > 999) { |
| 145 | int num = 0; |
| 146 | while (amt != 0) { |
| 147 | num++; |
| 148 | amt /= 10; |
| 149 | } |
| 150 | return num + suffix; |
| 151 | } else { |
| 152 | if (amt > 99 || (always && zeropad >= 3)) { |
| 153 | return 3+suffix; |
| 154 | } |
| 155 | if (amt > 9 || (always && zeropad >= 2)) { |
| 156 | return 2+suffix; |
| 157 | } |
| 158 | if (always || amt > 0) { |
| 159 | return 1+suffix; |
| 160 | } |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 161 | } |
| 162 | return 0; |
| 163 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 164 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 165 | static private int printFieldLocked(char[] formatStr, int amt, char suffix, int pos, |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 166 | boolean always, int zeropad) { |
| 167 | if (always || amt > 0) { |
Bjorn Bringert | 901b379 | 2010-11-23 14:43:12 +0000 | [diff] [blame] | 168 | final int startPos = pos; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 169 | if (amt > 999) { |
| 170 | int tmp = 0; |
| 171 | while (amt != 0 && tmp < sTmpFormatStr.length) { |
| 172 | int dig = amt % 10; |
| 173 | sTmpFormatStr[tmp] = (char)(dig + '0'); |
| 174 | tmp++; |
| 175 | amt /= 10; |
| 176 | } |
| 177 | tmp--; |
| 178 | while (tmp >= 0) { |
| 179 | formatStr[pos] = sTmpFormatStr[tmp]; |
| 180 | pos++; |
| 181 | tmp--; |
| 182 | } |
| 183 | } else { |
| 184 | if ((always && zeropad >= 3) || amt > 99) { |
| 185 | int dig = amt/100; |
| 186 | formatStr[pos] = (char)(dig + '0'); |
| 187 | pos++; |
| 188 | amt -= (dig*100); |
| 189 | } |
| 190 | if ((always && zeropad >= 2) || amt > 9 || startPos != pos) { |
| 191 | int dig = amt/10; |
| 192 | formatStr[pos] = (char)(dig + '0'); |
| 193 | pos++; |
| 194 | amt -= (dig*10); |
| 195 | } |
| 196 | formatStr[pos] = (char)(amt + '0'); |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 197 | pos++; |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 198 | } |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 199 | formatStr[pos] = suffix; |
| 200 | pos++; |
| 201 | } |
| 202 | return pos; |
| 203 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 204 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 205 | private static int formatDurationLocked(long duration, int fieldLen) { |
| 206 | if (sFormatStr.length < fieldLen) { |
| 207 | sFormatStr = new char[fieldLen]; |
| 208 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 209 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 210 | char[] formatStr = sFormatStr; |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 211 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 212 | if (duration == 0) { |
| 213 | int pos = 0; |
| 214 | fieldLen -= 1; |
| 215 | while (pos < fieldLen) { |
Jozef BABJAK | 47f13e7 | 2011-02-22 07:20:30 +0100 | [diff] [blame] | 216 | formatStr[pos++] = ' '; |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 217 | } |
| 218 | formatStr[pos] = '0'; |
| 219 | return pos+1; |
| 220 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 221 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 222 | char prefix; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 223 | if (duration > 0) { |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 224 | prefix = '+'; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 225 | } else { |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 226 | prefix = '-'; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 227 | duration = -duration; |
| 228 | } |
| 229 | |
| 230 | int millis = (int)(duration%1000); |
| 231 | int seconds = (int) Math.floor(duration / 1000); |
| 232 | int days = 0, hours = 0, minutes = 0; |
| 233 | |
Mitchell Wills | f9a80cb | 2015-09-22 15:30:08 -0700 | [diff] [blame] | 234 | if (seconds >= SECONDS_PER_DAY) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 235 | days = seconds / SECONDS_PER_DAY; |
| 236 | seconds -= days * SECONDS_PER_DAY; |
| 237 | } |
Mitchell Wills | f9a80cb | 2015-09-22 15:30:08 -0700 | [diff] [blame] | 238 | if (seconds >= SECONDS_PER_HOUR) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 239 | hours = seconds / SECONDS_PER_HOUR; |
| 240 | seconds -= hours * SECONDS_PER_HOUR; |
| 241 | } |
Mitchell Wills | f9a80cb | 2015-09-22 15:30:08 -0700 | [diff] [blame] | 242 | if (seconds >= SECONDS_PER_MINUTE) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 243 | minutes = seconds / SECONDS_PER_MINUTE; |
| 244 | seconds -= minutes * SECONDS_PER_MINUTE; |
| 245 | } |
| 246 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 247 | int pos = 0; |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 248 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 249 | if (fieldLen != 0) { |
| 250 | int myLen = accumField(days, 1, false, 0); |
| 251 | myLen += accumField(hours, 1, myLen > 0, 2); |
| 252 | myLen += accumField(minutes, 1, myLen > 0, 2); |
| 253 | myLen += accumField(seconds, 1, myLen > 0, 2); |
| 254 | myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1; |
| 255 | while (myLen < fieldLen) { |
| 256 | formatStr[pos] = ' '; |
| 257 | pos++; |
| 258 | myLen++; |
| 259 | } |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 260 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 261 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 262 | formatStr[pos] = prefix; |
| 263 | pos++; |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 264 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 265 | int start = pos; |
| 266 | boolean zeropad = fieldLen != 0; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 267 | pos = printFieldLocked(formatStr, days, 'd', pos, false, 0); |
| 268 | pos = printFieldLocked(formatStr, hours, 'h', pos, pos != start, zeropad ? 2 : 0); |
| 269 | pos = printFieldLocked(formatStr, minutes, 'm', pos, pos != start, zeropad ? 2 : 0); |
| 270 | pos = printFieldLocked(formatStr, seconds, 's', pos, pos != start, zeropad ? 2 : 0); |
| 271 | pos = printFieldLocked(formatStr, millis, 'm', pos, true, (zeropad && pos != start) ? 3 : 0); |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 272 | formatStr[pos] = 's'; |
| 273 | return pos + 1; |
| 274 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 275 | |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 276 | /** @hide Just for debugging; not internationalized. */ |
| 277 | public static void formatDuration(long duration, StringBuilder builder) { |
| 278 | synchronized (sFormatSync) { |
| 279 | int len = formatDurationLocked(duration, 0); |
| 280 | builder.append(sFormatStr, 0, len); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 281 | } |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** @hide Just for debugging; not internationalized. */ |
Kweku Adams | 71a9531 | 2018-04-16 16:54:24 -0700 | [diff] [blame] | 285 | public static void formatDuration(long duration, StringBuilder builder, int fieldLen) { |
| 286 | synchronized (sFormatSync) { |
| 287 | int len = formatDurationLocked(duration, fieldLen); |
| 288 | builder.append(sFormatStr, 0, len); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** @hide Just for debugging; not internationalized. */ |
Mathew Inwood | 55418ea | 2018-12-20 15:30:45 +0000 | [diff] [blame^] | 293 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 294 | public static void formatDuration(long duration, PrintWriter pw, int fieldLen) { |
| 295 | synchronized (sFormatSync) { |
| 296 | int len = formatDurationLocked(duration, fieldLen); |
| 297 | pw.print(new String(sFormatStr, 0, len)); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 298 | } |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** @hide Just for debugging; not internationalized. */ |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 302 | public static String formatDuration(long duration) { |
| 303 | synchronized (sFormatSync) { |
| 304 | int len = formatDurationLocked(duration, 0); |
| 305 | return new String(sFormatStr, 0, len); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** @hide Just for debugging; not internationalized. */ |
Mathew Inwood | 55418ea | 2018-12-20 15:30:45 +0000 | [diff] [blame^] | 310 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 311 | public static void formatDuration(long duration, PrintWriter pw) { |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 312 | formatDuration(duration, pw, 0); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 313 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 314 | |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 315 | /** @hide Just for debugging; not internationalized. */ |
| 316 | public static void formatDuration(long time, long now, PrintWriter pw) { |
| 317 | if (time == 0) { |
| 318 | pw.print("--"); |
| 319 | return; |
| 320 | } |
Dianne Hackborn | b5e3165 | 2010-09-07 12:13:55 -0700 | [diff] [blame] | 321 | formatDuration(time-now, pw, 0); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 322 | } |
Wink Saville | 26e1a02 | 2012-05-10 16:23:39 -0700 | [diff] [blame] | 323 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 324 | /** @hide Just for debugging; not internationalized. */ |
| 325 | public static String formatUptime(long time) { |
| 326 | final long diff = time - SystemClock.uptimeMillis(); |
| 327 | if (diff > 0) { |
| 328 | return time + " (in " + diff + " ms)"; |
| 329 | } |
| 330 | if (diff < 0) { |
| 331 | return time + " (" + -diff + " ms ago)"; |
| 332 | } |
| 333 | return time + " (now)"; |
| 334 | } |
| 335 | |
Wink Saville | 26e1a02 | 2012-05-10 16:23:39 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Convert a System.currentTimeMillis() value to a time of day value like |
| 338 | * that printed in logs. MM-DD HH:MM:SS.MMM |
| 339 | * |
| 340 | * @param millis since the epoch (1/1/1970) |
| 341 | * @return String representation of the time. |
| 342 | * @hide |
| 343 | */ |
Mathew Inwood | b407568 | 2018-08-14 17:32:44 +0100 | [diff] [blame] | 344 | @UnsupportedAppUsage |
Wink Saville | 26e1a02 | 2012-05-10 16:23:39 -0700 | [diff] [blame] | 345 | public static String logTimeOfDay(long millis) { |
| 346 | Calendar c = Calendar.getInstance(); |
| 347 | if (millis >= 0) { |
| 348 | c.setTimeInMillis(millis); |
| 349 | return String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c); |
| 350 | } else { |
| 351 | return Long.toString(millis); |
| 352 | } |
| 353 | } |
Jeff Sharkey | e8a4b66 | 2015-06-27 15:43:45 -0700 | [diff] [blame] | 354 | |
| 355 | /** {@hide} */ |
| 356 | public static String formatForLogging(long millis) { |
| 357 | if (millis <= 0) { |
| 358 | return "unknown"; |
| 359 | } else { |
| 360 | return sLoggingFormat.format(new Date(millis)); |
| 361 | } |
| 362 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |