Add test cases for utcToLocalTime in the ConvertUtils

Bug: 177406865
Bug: 185187729
Test: make SettingsRoboTests
Test: make SettingsGoogleRoboTests
Change-Id: I22bda2db821e69c40202db692ada52e96af25e16
This commit is contained in:
ykhung
2021-04-24 01:04:30 +08:00
parent 81399d6fec
commit ef6cea8c84
3 changed files with 47 additions and 16 deletions

View File

@@ -70,8 +70,12 @@ public final class ConvertUtils {
public static final int CONSUMER_TYPE_SYSTEM_BATTERY = 3;
private static String sZoneId;
private static SimpleDateFormat sSimpleDateFormat;
private static SimpleDateFormat sSimpleDateFormatForHour;
private static String sZoneIdForHour;
@VisibleForTesting
static SimpleDateFormat sSimpleDateFormat;
@VisibleForTesting
static SimpleDateFormat sSimpleDateFormatForHour;
private ConvertUtils() {}
@@ -140,25 +144,19 @@ public final class ConvertUtils {
sZoneId = currentZoneId;
sSimpleDateFormat =
new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", Locale.ENGLISH);
sSimpleDateFormatForHour = null;
}
return sSimpleDateFormat.format(new Date(timestamp));
}
/** Converts UTC timestamp to local time hour data. */
public static int utcToLocalTimeHour(long timestamp) {
public static String utcToLocalTimeHour(long timestamp) {
final String currentZoneId = TimeZone.getDefault().getID();
if (!currentZoneId.equals(sZoneId) || sSimpleDateFormatForHour == null) {
sZoneId = currentZoneId;
sSimpleDateFormat = null;
sSimpleDateFormatForHour = new SimpleDateFormat("HH", Locale.ENGLISH);
}
try {
return Integer.parseInt(
sSimpleDateFormatForHour.format(new Date(timestamp)));
} catch (NumberFormatException e) {
return Integer.MIN_VALUE;
if (!currentZoneId.equals(sZoneIdForHour) || sSimpleDateFormatForHour == null) {
sZoneIdForHour = currentZoneId;
sSimpleDateFormatForHour = new SimpleDateFormat("h aa", Locale.ENGLISH);
}
return sSimpleDateFormatForHour.format(new Date(timestamp))
.toLowerCase(Locale.getDefault());
}
/** Gets indexed battery usage data for each corresponding time slot. */