| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.TestApi; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.os.Build; |
| 24 |
import android.os.SystemClock; |
| 25 |
|
| 26 |
import com.android.i18n.timezone.CountryTimeZones; |
| 27 |
import com.android.i18n.timezone.CountryTimeZones.TimeZoneMapping; |
| 28 |
import com.android.i18n.timezone.TimeZoneFinder; |
| 29 |
import com.android.i18n.timezone.ZoneInfoDb; |
| 30 |
|
| 31 |
import java.io.PrintWriter; |
| 32 |
import java.text.SimpleDateFormat; |
| 33 |
import java.time.Instant; |
| 34 |
import java.time.LocalTime; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Calendar; |
| 37 |
import java.util.Collections; |
| 38 |
import java.util.Date; |
| 39 |
import java.util.List; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class TimeUtils { |
| 45 |
public TimeUtils() {} |
| 46 |
|
| 47 |
private static final SimpleDateFormat sLoggingFormat = |
| 48 |
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 49 |
|
| 50 |
|
| 51 |
public static final SimpleDateFormat sDumpDateFormat = |
| 52 |
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static final Instant MIN_USE_DATE_OF_TIMEZONE = |
| 78 |
Instant.ofEpochMilli(1546300800000L); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static java.util.TimeZone getTimeZone( |
| 86 |
int offset, boolean dst, long when, String country) { |
| 87 |
|
| 88 |
android.icu.util.TimeZone icuTimeZone = getIcuTimeZone(offset, dst, when, country); |
| 89 |
|
| 90 |
|
| 91 |
return icuTimeZone != null ? java.util.TimeZone.getTimeZone(icuTimeZone.getID()) : null; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private static android.icu.util.TimeZone getIcuTimeZone( |
| 100 |
int offsetMillis, boolean isDst, long whenMillis, String countryIso) { |
| 101 |
if (countryIso == null) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
android.icu.util.TimeZone bias = android.icu.util.TimeZone.getDefault(); |
| 106 |
CountryTimeZones countryTimeZones = |
| 107 |
TimeZoneFinder.getInstance().lookupCountryTimeZones(countryIso); |
| 108 |
if (countryTimeZones == null) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
CountryTimeZones.OffsetResult offsetResult = countryTimeZones.lookupByOffsetWithBias( |
| 112 |
whenMillis, bias, offsetMillis, isDst); |
| 113 |
return offsetResult != null ? offsetResult.getTimeZone() : null; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public static @Nullable List<String> getTimeZoneIdsForCountryCode(@NonNull String countryCode) { |
| 129 |
if (countryCode == null) { |
| 130 |
throw new NullPointerException("countryCode == null"); |
| 131 |
} |
| 132 |
TimeZoneFinder timeZoneFinder = TimeZoneFinder.getInstance(); |
| 133 |
CountryTimeZones countryTimeZones = |
| 134 |
timeZoneFinder.lookupCountryTimeZones(countryCode.toLowerCase()); |
| 135 |
if (countryTimeZones == null) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
List<String> timeZoneIds = new ArrayList<>(); |
| 140 |
for (TimeZoneMapping timeZoneMapping : countryTimeZones.getTimeZoneMappings()) { |
| 141 |
if (timeZoneMapping.isShownInPickerAt(MIN_USE_DATE_OF_TIMEZONE)) { |
| 142 |
timeZoneIds.add(timeZoneMapping.getTimeZoneId()); |
| 143 |
} |
| 144 |
} |
| 145 |
return Collections.unmodifiableList(timeZoneIds); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static String getTimeZoneDatabaseVersion() { |
| 166 |
return ZoneInfoDb.getInstance().getVersion(); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
public static final int HUNDRED_DAY_FIELD_LEN = 19; |
| 171 |
|
| 172 |
private static final int SECONDS_PER_MINUTE = 60; |
| 173 |
private static final int SECONDS_PER_HOUR = 60 * 60; |
| 174 |
private static final int SECONDS_PER_DAY = 24 * 60 * 60; |
| 175 |
|
| 176 |
|
| 177 |
public static final long NANOS_PER_MS = 1000000; |
| 178 |
|
| 179 |
private static final Object sFormatSync = new Object(); |
| 180 |
private static char[] sFormatStr = new char[HUNDRED_DAY_FIELD_LEN+10]; |
| 181 |
private static char[] sTmpFormatStr = new char[HUNDRED_DAY_FIELD_LEN+10]; |
| 182 |
|
| 183 |
static private int accumField(int amt, int suffix, boolean always, int zeropad) { |
| 184 |
if (amt > 999) { |
| 185 |
int num = 0; |
| 186 |
while (amt != 0) { |
| 187 |
num++; |
| 188 |
amt /= 10; |
| 189 |
} |
| 190 |
return num + suffix; |
| 191 |
} else { |
| 192 |
if (amt > 99 || (always && zeropad >= 3)) { |
| 193 |
return 3+suffix; |
| 194 |
} |
| 195 |
if (amt > 9 || (always && zeropad >= 2)) { |
| 196 |
return 2+suffix; |
| 197 |
} |
| 198 |
if (always || amt > 0) { |
| 199 |
return 1+suffix; |
| 200 |
} |
| 201 |
} |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
static private int printFieldLocked(char[] formatStr, int amt, char suffix, int pos, |
| 206 |
boolean always, int zeropad) { |
| 207 |
if (always || amt > 0) { |
| 208 |
final int startPos = pos; |
| 209 |
if (amt > 999) { |
| 210 |
int tmp = 0; |
| 211 |
while (amt != 0 && tmp < sTmpFormatStr.length) { |
| 212 |
int dig = amt % 10; |
| 213 |
sTmpFormatStr[tmp] = (char)(dig + '0'); |
| 214 |
tmp++; |
| 215 |
amt /= 10; |
| 216 |
} |
| 217 |
tmp--; |
| 218 |
while (tmp >= 0) { |
| 219 |
formatStr[pos] = sTmpFormatStr[tmp]; |
| 220 |
pos++; |
| 221 |
tmp--; |
| 222 |
} |
| 223 |
} else { |
| 224 |
if ((always && zeropad >= 3) || amt > 99) { |
| 225 |
int dig = amt/100; |
| 226 |
formatStr[pos] = (char)(dig + '0'); |
| 227 |
pos++; |
| 228 |
amt -= (dig*100); |
| 229 |
} |
| 230 |
if ((always && zeropad >= 2) || amt > 9 || startPos != pos) { |
| 231 |
int dig = amt/10; |
| 232 |
formatStr[pos] = (char)(dig + '0'); |
| 233 |
pos++; |
| 234 |
amt -= (dig*10); |
| 235 |
} |
| 236 |
formatStr[pos] = (char)(amt + '0'); |
| 237 |
pos++; |
| 238 |
} |
| 239 |
formatStr[pos] = suffix; |
| 240 |
pos++; |
| 241 |
} |
| 242 |
return pos; |
| 243 |
} |
| 244 |
|
| 245 |
private static int formatDurationLocked(long duration, int fieldLen) { |
| 246 |
if (sFormatStr.length < fieldLen) { |
| 247 |
sFormatStr = new char[fieldLen]; |
| 248 |
} |
| 249 |
|
| 250 |
char[] formatStr = sFormatStr; |
| 251 |
|
| 252 |
if (duration == 0) { |
| 253 |
int pos = 0; |
| 254 |
fieldLen -= 1; |
| 255 |
while (pos < fieldLen) { |
| 256 |
formatStr[pos++] = ' '; |
| 257 |
} |
| 258 |
formatStr[pos] = '0'; |
| 259 |
return pos+1; |
| 260 |
} |
| 261 |
|
| 262 |
char prefix; |
| 263 |
if (duration > 0) { |
| 264 |
prefix = '+'; |
| 265 |
} else { |
| 266 |
prefix = '-'; |
| 267 |
duration = -duration; |
| 268 |
} |
| 269 |
|
| 270 |
int millis = (int)(duration%1000); |
| 271 |
int seconds = (int) Math.floor(duration / 1000); |
| 272 |
int days = 0, hours = 0, minutes = 0; |
| 273 |
|
| 274 |
if (seconds >= SECONDS_PER_DAY) { |
| 275 |
days = seconds / SECONDS_PER_DAY; |
| 276 |
seconds -= days * SECONDS_PER_DAY; |
| 277 |
} |
| 278 |
if (seconds >= SECONDS_PER_HOUR) { |
| 279 |
hours = seconds / SECONDS_PER_HOUR; |
| 280 |
seconds -= hours * SECONDS_PER_HOUR; |
| 281 |
} |
| 282 |
if (seconds >= SECONDS_PER_MINUTE) { |
| 283 |
minutes = seconds / SECONDS_PER_MINUTE; |
| 284 |
seconds -= minutes * SECONDS_PER_MINUTE; |
| 285 |
} |
| 286 |
|
| 287 |
int pos = 0; |
| 288 |
|
| 289 |
if (fieldLen != 0) { |
| 290 |
int myLen = accumField(days, 1, false, 0); |
| 291 |
myLen += accumField(hours, 1, myLen > 0, 2); |
| 292 |
myLen += accumField(minutes, 1, myLen > 0, 2); |
| 293 |
myLen += accumField(seconds, 1, myLen > 0, 2); |
| 294 |
myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1; |
| 295 |
while (myLen < fieldLen) { |
| 296 |
formatStr[pos] = ' '; |
| 297 |
pos++; |
| 298 |
myLen++; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
formatStr[pos] = prefix; |
| 303 |
pos++; |
| 304 |
|
| 305 |
int start = pos; |
| 306 |
boolean zeropad = fieldLen != 0; |
| 307 |
pos = printFieldLocked(formatStr, days, 'd', pos, false, 0); |
| 308 |
pos = printFieldLocked(formatStr, hours, 'h', pos, pos != start, zeropad ? 2 : 0); |
| 309 |
pos = printFieldLocked(formatStr, minutes, 'm', pos, pos != start, zeropad ? 2 : 0); |
| 310 |
pos = printFieldLocked(formatStr, seconds, 's', pos, pos != start, zeropad ? 2 : 0); |
| 311 |
pos = printFieldLocked(formatStr, millis, 'm', pos, true, (zeropad && pos != start) ? 3 : 0); |
| 312 |
formatStr[pos] = 's'; |
| 313 |
return pos + 1; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
public static void formatDuration(long duration, StringBuilder builder) { |
| 318 |
synchronized (sFormatSync) { |
| 319 |
int len = formatDurationLocked(duration, 0); |
| 320 |
builder.append(sFormatStr, 0, len); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
public static void formatDuration(long duration, StringBuilder builder, int fieldLen) { |
| 326 |
synchronized (sFormatSync) { |
| 327 |
int len = formatDurationLocked(duration, fieldLen); |
| 328 |
builder.append(sFormatStr, 0, len); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 334 |
public static void formatDuration(long duration, PrintWriter pw, int fieldLen) { |
| 335 |
synchronized (sFormatSync) { |
| 336 |
int len = formatDurationLocked(duration, fieldLen); |
| 337 |
pw.print(new String(sFormatStr, 0, len)); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
@TestApi |
| 343 |
public static String formatDuration(long duration) { |
| 344 |
synchronized (sFormatSync) { |
| 345 |
int len = formatDurationLocked(duration, 0); |
| 346 |
return new String(sFormatStr, 0, len); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 352 |
public static void formatDuration(long duration, PrintWriter pw) { |
| 353 |
formatDuration(duration, pw, 0); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
public static void formatDuration(long time, long now, PrintWriter pw) { |
| 358 |
if (time == 0) { |
| 359 |
pw.print("--"); |
| 360 |
return; |
| 361 |
} |
| 362 |
formatDuration(time-now, pw, 0); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
public static String formatUptime(long time) { |
| 367 |
return formatTime(time, SystemClock.uptimeMillis()); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
public static String formatRealtime(long time) { |
| 372 |
return formatTime(time, SystemClock.elapsedRealtime()); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
public static String formatTime(long time, long referenceTime) { |
| 377 |
long diff = time - referenceTime; |
| 378 |
if (diff > 0) { |
| 379 |
return time + " (in " + diff + " ms)"; |
| 380 |
} |
| 381 |
if (diff < 0) { |
| 382 |
return time + " (" + -diff + " ms ago)"; |
| 383 |
} |
| 384 |
return time + " (now)"; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 396 |
public static String logTimeOfDay(long millis) { |
| 397 |
Calendar c = Calendar.getInstance(); |
| 398 |
if (millis >= 0) { |
| 399 |
c.setTimeInMillis(millis); |
| 400 |
return String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c); |
| 401 |
} else { |
| 402 |
return Long.toString(millis); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
public static String formatForLogging(long millis) { |
| 408 |
if (millis <= 0) { |
| 409 |
return "unknown"; |
| 410 |
} else { |
| 411 |
return sLoggingFormat.format(new Date(millis)); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public static void dumpTime(PrintWriter pw, long time) { |
| 421 |
pw.print(sDumpDateFormat.format(new Date(time))); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public static boolean isTimeBetween(@NonNull LocalTime reference, |
| 432 |
@NonNull LocalTime start, |
| 433 |
@NonNull LocalTime end) { |
| 434 |
|
| 435 |
if ((reference.isBefore(start) && reference.isAfter(end) |
| 436 |
|
| 437 |
|| (reference.isBefore(end) && reference.isBefore(start) && start.isBefore(end)) |
| 438 |
|
| 439 |
|| (reference.isAfter(end) && reference.isAfter(start)) && start.isBefore(end))) { |
| 440 |
return false; |
| 441 |
} else { |
| 442 |
return true; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public static void dumpTimeWithDelta(PrintWriter pw, long time, long now) { |
| 452 |
pw.print(sDumpDateFormat.format(new Date(time))); |
| 453 |
if (time == now) { |
| 454 |
pw.print(" (now)"); |
| 455 |
} else { |
| 456 |
pw.print(" ("); |
| 457 |
TimeUtils.formatDuration(time, now, pw); |
| 458 |
pw.print(")"); |
| 459 |
} |
| 460 |
}} |