| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.format; |
| 18 |
|
| 19 |
import static android.text.format.DateUtils.FORMAT_12HOUR; |
| 20 |
import static android.text.format.DateUtils.FORMAT_24HOUR; |
| 21 |
import static android.text.format.DateUtils.FORMAT_ABBREV_ALL; |
| 22 |
import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH; |
| 23 |
import static android.text.format.DateUtils.FORMAT_ABBREV_TIME; |
| 24 |
import static android.text.format.DateUtils.FORMAT_ABBREV_WEEKDAY; |
| 25 |
import static android.text.format.DateUtils.FORMAT_NO_MONTH_DAY; |
| 26 |
import static android.text.format.DateUtils.FORMAT_NO_YEAR; |
| 27 |
import static android.text.format.DateUtils.FORMAT_NUMERIC_DATE; |
| 28 |
import static android.text.format.DateUtils.FORMAT_SHOW_DATE; |
| 29 |
import static android.text.format.DateUtils.FORMAT_SHOW_TIME; |
| 30 |
import static android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY; |
| 31 |
import static android.text.format.DateUtils.FORMAT_SHOW_YEAR; |
| 32 |
|
| 33 |
import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE; |
| 34 |
|
| 35 |
import android.icu.util.Calendar; |
| 36 |
import android.icu.util.GregorianCalendar; |
| 37 |
import android.icu.util.TimeZone; |
| 38 |
import android.icu.util.ULocale; |
| 39 |
|
| 40 |
import com.android.internal.annotations.VisibleForTesting; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@VisibleForTesting(visibility = PACKAGE) |
| 49 |
public final class DateUtilsBridge { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static TimeZone icuTimeZone(java.util.TimeZone tz) { |
| 57 |
TimeZone icuTimeZone = TimeZone.getTimeZone(tz.getID()); |
| 58 |
icuTimeZone.freeze(); |
| 59 |
return icuTimeZone; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static Calendar createIcuCalendar(TimeZone icuTimeZone, ULocale icuLocale, |
| 66 |
long timeInMillis) { |
| 67 |
Calendar calendar = new GregorianCalendar(icuTimeZone, icuLocale); |
| 68 |
calendar.setTimeInMillis(timeInMillis); |
| 69 |
return calendar; |
| 70 |
} |
| 71 |
|
| 72 |
public static String toSkeleton(Calendar calendar, int flags) { |
| 73 |
return toSkeleton(calendar, calendar, flags); |
| 74 |
} |
| 75 |
|
| 76 |
public static String toSkeleton(Calendar startCalendar, Calendar endCalendar, int flags) { |
| 77 |
if ((flags & FORMAT_ABBREV_ALL) != 0) { |
| 78 |
flags |= FORMAT_ABBREV_MONTH | FORMAT_ABBREV_TIME | FORMAT_ABBREV_WEEKDAY; |
| 79 |
} |
| 80 |
|
| 81 |
String monthPart = "MMMM"; |
| 82 |
if ((flags & FORMAT_NUMERIC_DATE) != 0) { |
| 83 |
monthPart = "M"; |
| 84 |
} else if ((flags & FORMAT_ABBREV_MONTH) != 0) { |
| 85 |
monthPart = "MMM"; |
| 86 |
} |
| 87 |
|
| 88 |
String weekPart = "EEEE"; |
| 89 |
if ((flags & FORMAT_ABBREV_WEEKDAY) != 0) { |
| 90 |
weekPart = "EEE"; |
| 91 |
} |
| 92 |
|
| 93 |
String timePart = "j"; |
| 94 |
if ((flags & FORMAT_24HOUR) != 0) { |
| 95 |
timePart = "H"; |
| 96 |
} else if ((flags & FORMAT_12HOUR) != 0) { |
| 97 |
timePart = "h"; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if ((flags & FORMAT_ABBREV_TIME) == 0 || (flags & FORMAT_24HOUR) != 0) { |
| 104 |
timePart += "m"; |
| 105 |
} else { |
| 106 |
|
| 107 |
|
| 108 |
if (!(onTheHour(startCalendar) && onTheHour(endCalendar))) { |
| 109 |
timePart = timePart + "m"; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (fallOnDifferentDates(startCalendar, endCalendar)) { |
| 114 |
flags |= FORMAT_SHOW_DATE; |
| 115 |
} |
| 116 |
|
| 117 |
if (fallInSameMonth(startCalendar, endCalendar) && (flags & FORMAT_NO_MONTH_DAY) != 0) { |
| 118 |
flags &= (~FORMAT_SHOW_WEEKDAY); |
| 119 |
flags &= (~FORMAT_SHOW_TIME); |
| 120 |
} |
| 121 |
|
| 122 |
if ((flags & (FORMAT_SHOW_DATE | FORMAT_SHOW_TIME | FORMAT_SHOW_WEEKDAY)) == 0) { |
| 123 |
flags |= FORMAT_SHOW_DATE; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
if ((flags & FORMAT_SHOW_DATE) != 0) { |
| 128 |
if ((flags & FORMAT_SHOW_YEAR) != 0) { |
| 129 |
|
| 130 |
} else if ((flags & FORMAT_NO_YEAR) != 0) { |
| 131 |
|
| 132 |
|
| 133 |
} else if (!fallInSameYear(startCalendar, endCalendar) || !isThisYear(startCalendar)) { |
| 134 |
flags |= FORMAT_SHOW_YEAR; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
StringBuilder builder = new StringBuilder(); |
| 139 |
if ((flags & (FORMAT_SHOW_DATE | FORMAT_NO_MONTH_DAY)) != 0) { |
| 140 |
if ((flags & FORMAT_SHOW_YEAR) != 0) { |
| 141 |
builder.append("y"); |
| 142 |
} |
| 143 |
builder.append(monthPart); |
| 144 |
if ((flags & FORMAT_NO_MONTH_DAY) == 0) { |
| 145 |
builder.append("d"); |
| 146 |
} |
| 147 |
} |
| 148 |
if ((flags & FORMAT_SHOW_WEEKDAY) != 0) { |
| 149 |
builder.append(weekPart); |
| 150 |
} |
| 151 |
if ((flags & FORMAT_SHOW_TIME) != 0) { |
| 152 |
builder.append(timePart); |
| 153 |
} |
| 154 |
return builder.toString(); |
| 155 |
} |
| 156 |
|
| 157 |
public static int dayDistance(Calendar c1, Calendar c2) { |
| 158 |
return c2.get(Calendar.JULIAN_DAY) - c1.get(Calendar.JULIAN_DAY); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static boolean isDisplayMidnightUsingSkeleton(Calendar c) { |
| 166 |
|
| 167 |
|
| 168 |
return c.get(Calendar.HOUR_OF_DAY) == 0 && c.get(Calendar.MINUTE) == 0; |
| 169 |
} |
| 170 |
|
| 171 |
private static boolean onTheHour(Calendar c) { |
| 172 |
return c.get(Calendar.MINUTE) == 0 && c.get(Calendar.SECOND) == 0; |
| 173 |
} |
| 174 |
|
| 175 |
private static boolean fallOnDifferentDates(Calendar c1, Calendar c2) { |
| 176 |
return c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR) |
| 177 |
|| c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH) |
| 178 |
|| c1.get(Calendar.DAY_OF_MONTH) != c2.get(Calendar.DAY_OF_MONTH); |
| 179 |
} |
| 180 |
|
| 181 |
private static boolean fallInSameMonth(Calendar c1, Calendar c2) { |
| 182 |
return c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH); |
| 183 |
} |
| 184 |
|
| 185 |
private static boolean fallInSameYear(Calendar c1, Calendar c2) { |
| 186 |
return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR); |
| 187 |
} |
| 188 |
|
| 189 |
private static boolean isThisYear(Calendar c) { |
| 190 |
Calendar now = (Calendar) c.clone(); |
| 191 |
now.setTimeInMillis(System.currentTimeMillis()); |
| 192 |
return c.get(Calendar.YEAR) == now.get(Calendar.YEAR); |
| 193 |
} |
| 194 |
} |