xref: /aosp_15_r20/external/robolectric/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTime.java (revision e6ba16074e6af37d123cb567d575f496bf0a58ee)
1 package org.robolectric.shadows;
2 
3 import android.text.format.Time;
4 import android.util.TimeFormatException;
5 import org.robolectric.annotation.Implementation;
6 import org.robolectric.annotation.Implements;
7 import org.robolectric.annotation.RealObject;
8 import org.robolectric.util.ReflectionHelpers;
9 
10 @Implements(value = Time.class)
11 public class ShadowTime {
12   @RealObject private Time time;
13 
14   private static final long SECOND_IN_MILLIS = 1000;
15   private static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
16   private static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
17   private static final long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
18 
19   @Implementation
getChar(String s, int spos, int mul)20   protected static int getChar(String s, int spos, int mul) {
21     char c = s.charAt(spos);
22     if (Character.isDigit(c)) {
23       return Character.getNumericValue(c) * mul;
24     } else {
25       throwTimeFormatException("Parse error at pos=" + spos);
26     }
27     return -1;
28   }
29 
30   @Implementation
checkChar(String s, int spos, char expected)31   protected void checkChar(String s, int spos, char expected) {
32     char c = s.charAt(spos);
33     if (c != expected) {
34       throwTimeFormatException(
35           String.format(
36               "Unexpected character 0x%02d at pos=%d.  Expected 0x%02d (\'%c\').",
37               (int) c, spos, (int) expected, expected));
38     }
39   }
40 
throwTimeFormatException(String optionalMessage)41   private static void throwTimeFormatException(String optionalMessage) {
42     throw ReflectionHelpers.callConstructor(
43         TimeFormatException.class,
44         ReflectionHelpers.ClassParameter.from(
45             String.class, optionalMessage == null ? "fail" : optionalMessage));
46   }
47 }
48