diff --git a/core/java/android/text/format/Time.java b/core/java/android/text/format/Time.java index f34e746adf6f7..aa6ad20487190 100644 --- a/core/java/android/text/format/Time.java +++ b/core/java/android/text/format/Time.java @@ -16,19 +16,38 @@ package android.text.format; -import android.content.res.Resources; +import android.util.TimeFormatException; +import java.io.IOException; import java.util.Locale; import java.util.TimeZone; -import libcore.icu.LocaleData; +import libcore.util.ZoneInfo; +import libcore.util.ZoneInfoDB; /** * An alternative to the {@link java.util.Calendar} and * {@link java.util.GregorianCalendar} classes. An instance of the Time class represents * a moment in time, specified with second precision. It is modelled after - * struct tm, and in fact, uses struct tm to implement most of the - * functionality. + * struct tm. This class is not thread-safe and does not consider leap seconds. + * + *
This class has a number of issues and it is recommended that + * {@link java.util.GregorianCalendar} is used instead. + * + *
Known issues: + *
This method can return incorrect results if the date / time cannot be normalized.
*/
- native public void switchTimezone(String timezone);
+ public void switchTimezone(String timezone) {
+ calculator.copyFieldsFromTime(this);
+ calculator.switchTimeZone(timezone);
+ calculator.copyFieldsToTime(this);
+ this.timezone = timezone;
+ }
private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31 };
@@ -265,13 +289,13 @@ public class Time {
/**
* Clears all values, setting the timezone to the given timezone. Sets isDst
* to a negative value to mean "unknown".
- * @param timezone the timezone to use.
+ * @param timezoneId the timezone to use.
*/
- public void clear(String timezone) {
- if (timezone == null) {
+ public void clear(String timezoneId) {
+ if (timezoneId == null) {
throw new NullPointerException("timezone is null!");
}
- this.timezone = timezone;
+ this.timezone = timezoneId;
this.allDay = false;
this.second = 0;
this.minute = 0;
@@ -304,12 +328,12 @@ public class Time {
} else if (b == null) {
throw new NullPointerException("b == null");
}
+ a.calculator.copyFieldsFromTime(a);
+ b.calculator.copyFieldsFromTime(b);
- return nativeCompare(a, b);
+ return TimeCalculator.compare(a.calculator, b.calculator);
}
- private static native int nativeCompare(Time a, Time b);
-
/**
* Print the current value given the format string provided. See man
* strftime for what means what. The final string must be less than 256
@@ -318,61 +342,21 @@ public class Time {
* @return a String containing the current time expressed in the current locale.
*/
public String format(String format) {
- synchronized (Time.class) {
- Locale locale = Locale.getDefault();
-
- if (sLocale == null || locale == null || !(locale.equals(sLocale))) {
- LocaleData localeData = LocaleData.get(locale);
-
- sAm = localeData.amPm[0];
- sPm = localeData.amPm[1];
- sZeroDigit = localeData.zeroDigit;
-
- sShortMonths = localeData.shortMonthNames;
- sLongMonths = localeData.longMonthNames;
- sLongStandaloneMonths = localeData.longStandAloneMonthNames;
- sShortWeekdays = localeData.shortWeekdayNames;
- sLongWeekdays = localeData.longWeekdayNames;
-
- Resources r = Resources.getSystem();
- sTimeOnlyFormat = r.getString(com.android.internal.R.string.time_of_day);
- sDateOnlyFormat = r.getString(com.android.internal.R.string.month_day_year);
- sDateTimeFormat = r.getString(com.android.internal.R.string.date_and_time);
-
- sLocale = locale;
- }
-
- String result = format1(format);
- if (sZeroDigit != '0') {
- result = localizeDigits(result);
- }
- return result;
- }
+ calculator.copyFieldsFromTime(this);
+ return calculator.format(format);
}
- native private String format1(String format);
-
- // TODO: unify this with java.util.Formatter's copy.
- private String localizeDigits(String s) {
- int length = s.length();
- int offsetToLocalizedDigits = sZeroDigit - '0';
- StringBuilder result = new StringBuilder(length);
- for (int i = 0; i < length; ++i) {
- char ch = s.charAt(i);
- if (ch >= '0' && ch <= '9') {
- ch += offsetToLocalizedDigits;
- }
- result.append(ch);
- }
- return result.toString();
- }
-
-
/**
* Return the current time in YYYYMMDDTHHMMSS
- * The same as format("%Y%m%dT%H%M%S").
+ * The same as format("%Y%m%dT%H%M%S"), or format("%Y%m%dT%H%M%SZ") for a Time with a
+ * timezone set to "UTC".
*/
- native public String format2445();
+ public String format2445() {
+ calculator.copyFieldsFromTime(this);
+ return calculator.format2445(!allDay);
+ }
/**
* Copy the value of that to this Time object. No normalization happens.
@@ -682,7 +892,6 @@ public class Time {
* Otherwise, if the timezone is UTC, expresses the time as Y-M-D-T-H-M-S UTC
* Otherwise the time is expressed the time as Y-M-D-T-H-M-S +- GMT
- * Use {@link #toMillis(boolean)} to get the milliseconds.
+ * Callers must pass the time in UTC millisecond (as can be returned
+ * by {@link #toMillis(boolean)} or {@link #normalize(boolean)})
+ * and the offset from UTC of the timezone in seconds (as might be in
+ * {@link #gmtoff}).
+ *
+ * The Julian day is useful for testing if two events occur on the
+ * same calendar date and for determining the relative time of an event
+ * from the present ("yesterday", "3 days ago", etc.).
*
* @param millis the time in UTC milliseconds
* @param gmtoff the offset from UTC in seconds
@@ -810,4 +1021,240 @@ public class Time {
public static int getJulianMondayFromWeeksSinceEpoch(int week) {
return MONDAY_BEFORE_JULIAN_EPOCH + week * 7;
}
+
+ /**
+ * A class that handles date/time calculations.
+ *
+ * This class originated as a port of a native C++ class ("android.Time") to pure Java. It is
+ * separate from the enclosing class because some methods copy the result of calculations back
+ * to the enclosing object, but others do not: thus separate state is retained.
+ */
+ private static class TimeCalculator {
+ public final ZoneInfo.WallTime wallTime;
+ public String timezone;
+
+ // Information about the current timezone.
+ private ZoneInfo zoneInfo;
+
+ public TimeCalculator(String timezoneId) {
+ this.zoneInfo = lookupZoneInfo(timezoneId);
+ this.wallTime = new ZoneInfo.WallTime();
+ }
+
+ public long toMillis(boolean ignoreDst) {
+ if (ignoreDst) {
+ wallTime.setIsDst(-1);
+ }
+
+ int r = wallTime.mktime(zoneInfo);
+ if (r == -1) {
+ return -1;
+ }
+ return r * 1000L;
+ }
+
+ public void setTimeInMillis(long millis) {
+ // Preserve old 32-bit Android behavior.
+ int intSeconds = (int) (millis / 1000);
+
+ updateZoneInfoFromTimeZone();
+ wallTime.localtime(intSeconds, zoneInfo);
+ }
+
+ public String format(String format) {
+ if (format == null) {
+ format = "%c";
+ }
+ TimeFormatter formatter = new TimeFormatter();
+ return formatter.format(format, wallTime, zoneInfo);
+ }
+
+ private void updateZoneInfoFromTimeZone() {
+ if (!zoneInfo.getID().equals(timezone)) {
+ this.zoneInfo = lookupZoneInfo(timezone);
+ }
+ }
+
+ private static ZoneInfo lookupZoneInfo(String timezoneId) {
+ try {
+ ZoneInfo zoneInfo = ZoneInfoDB.getInstance().makeTimeZone(timezoneId);
+ if (zoneInfo == null) {
+ zoneInfo = ZoneInfoDB.getInstance().makeTimeZone("GMT");
+ }
+ if (zoneInfo == null) {
+ throw new AssertionError("GMT not found: \"" + timezoneId + "\"");
+ }
+ return zoneInfo;
+ } catch (IOException e) {
+ // This should not ever be thrown.
+ throw new AssertionError("Error loading timezone: \"" + timezoneId + "\"", e);
+ }
+ }
+
+ public void switchTimeZone(String timezone) {
+ int seconds = wallTime.mktime(zoneInfo);
+ this.timezone = timezone;
+ updateZoneInfoFromTimeZone();
+ wallTime.localtime(seconds, zoneInfo);
+ }
+
+ public String format2445(boolean hasTime) {
+ char[] buf = new char[hasTime ? 16 : 8];
+ int n = wallTime.getYear();
+
+ buf[0] = toChar(n / 1000);
+ n %= 1000;
+ buf[1] = toChar(n / 100);
+ n %= 100;
+ buf[2] = toChar(n / 10);
+ n %= 10;
+ buf[3] = toChar(n);
+
+ n = wallTime.getMonth() + 1;
+ buf[4] = toChar(n / 10);
+ buf[5] = toChar(n % 10);
+
+ n = wallTime.getMonthDay();
+ buf[6] = toChar(n / 10);
+ buf[7] = toChar(n % 10);
+
+ if (!hasTime) {
+ return new String(buf, 0, 8);
+ }
+
+ buf[8] = 'T';
+
+ n = wallTime.getHour();
+ buf[9] = toChar(n / 10);
+ buf[10] = toChar(n % 10);
+
+ n = wallTime.getMinute();
+ buf[11] = toChar(n / 10);
+ buf[12] = toChar(n % 10);
+
+ n = wallTime.getSecond();
+ buf[13] = toChar(n / 10);
+ buf[14] = toChar(n % 10);
+
+ if (TIMEZONE_UTC.equals(timezone)) {
+ // The letter 'Z' is appended to the end.
+ buf[15] = 'Z';
+ return new String(buf, 0, 16);
+ } else {
+ return new String(buf, 0, 15);
+ }
+ }
+
+ private char toChar(int n) {
+ return (n >= 0 && n <= 9) ? (char) (n + '0') : ' ';
+ }
+
+ /**
+ * A method that will return the state of this object in string form. Note: it has side
+ * effects and so has deliberately not been made the default {@link #toString()}.
+ */
+ public String toStringInternal() {
+ // This implementation possibly displays the un-normalized fields because that is
+ // what it has always done.
+ return String.format("%04d%02d%02dT%02d%02d%02d%s(%d,%d,%d,%d,%d)",
+ wallTime.getYear(),
+ wallTime.getMonth() + 1,
+ wallTime.getMonthDay(),
+ wallTime.getHour(),
+ wallTime.getMinute(),
+ wallTime.getSecond(),
+ timezone,
+ wallTime.getWeekDay(),
+ wallTime.getYearDay(),
+ wallTime.getGmtOffset(),
+ wallTime.getIsDst(),
+ toMillis(false /* use isDst */) / 1000
+ );
+
+ }
+
+ public static int compare(TimeCalculator aObject, TimeCalculator bObject) {
+ if (aObject.timezone.equals(bObject.timezone)) {
+ // If the timezones are the same, we can easily compare the two times.
+ int diff = aObject.wallTime.getYear() - bObject.wallTime.getYear();
+ if (diff != 0) {
+ return diff;
+ }
+
+ diff = aObject.wallTime.getMonth() - bObject.wallTime.getMonth();
+ if (diff != 0) {
+ return diff;
+ }
+
+ diff = aObject.wallTime.getMonthDay() - bObject.wallTime.getMonthDay();
+ if (diff != 0) {
+ return diff;
+ }
+
+ diff = aObject.wallTime.getHour() - bObject.wallTime.getHour();
+ if (diff != 0) {
+ return diff;
+ }
+
+ diff = aObject.wallTime.getMinute() - bObject.wallTime.getMinute();
+ if (diff != 0) {
+ return diff;
+ }
+
+ diff = aObject.wallTime.getSecond() - bObject.wallTime.getSecond();
+ if (diff != 0) {
+ return diff;
+ }
+
+ return 0;
+ } else {
+ // Otherwise, convert to milliseconds and compare that. This requires that object be
+ // normalized. Note: For dates that do not exist: toMillis() can return -1, which
+ // can be confused with a valid time.
+ long am = aObject.toMillis(false /* use isDst */);
+ long bm = bObject.toMillis(false /* use isDst */);
+ long diff = am - bm;
+ return (diff < 0) ? -1 : ((diff > 0) ? 1 : 0);
+ }
+
+ }
+
+ public void copyFieldsToTime(Time time) {
+ time.second = wallTime.getSecond();
+ time.minute = wallTime.getMinute();
+ time.hour = wallTime.getHour();
+ time.monthDay = wallTime.getMonthDay();
+ time.month = wallTime.getMonth();
+ time.year = wallTime.getYear();
+
+ // Read-only fields that are derived from other information above.
+ time.weekDay = wallTime.getWeekDay();
+ time.yearDay = wallTime.getYearDay();
+
+ // < 0: DST status unknown, 0: is not in DST, 1: is in DST
+ time.isDst = wallTime.getIsDst();
+ // This is in seconds and includes any DST offset too.
+ time.gmtoff = wallTime.getGmtOffset();
+ }
+
+ public void copyFieldsFromTime(Time time) {
+ wallTime.setSecond(time.second);
+ wallTime.setMinute(time.minute);
+ wallTime.setHour(time.hour);
+ wallTime.setMonthDay(time.monthDay);
+ wallTime.setMonth(time.month);
+ wallTime.setYear(time.year);
+ wallTime.setWeekDay(time.weekDay);
+ wallTime.setYearDay(time.yearDay);
+ wallTime.setIsDst(time.isDst);
+ wallTime.setGmtOffset((int) time.gmtoff);
+
+ if (time.allDay && (time.second != 0 || time.minute != 0 || time.hour != 0)) {
+ throw new IllegalArgumentException("allDay is true but sec, min, hour are not 0.");
+ }
+
+ timezone = time.timezone;
+ updateZoneInfoFromTimeZone();
+ }
+ }
}
diff --git a/core/java/android/text/format/TimeFormatter.java b/core/java/android/text/format/TimeFormatter.java
new file mode 100644
index 0000000000000..ec79b3650cb28
--- /dev/null
+++ b/core/java/android/text/format/TimeFormatter.java
@@ -0,0 +1,519 @@
+/*
+ * Based on the UCB version of strftime.c with the copyright notice appearing below.
+ */
+
+/*
+** Copyright (c) 1989 The Regents of the University of California.
+** All rights reserved.
+**
+** Redistribution and use in source and binary forms are permitted
+** provided that the above copyright notice and this paragraph are
+** duplicated in all such forms and that any documentation,
+** advertising materials, and other materials related to such
+** distribution and use acknowledge that the software was developed
+** by the University of California, Berkeley. The name of the
+** University may not be used to endorse or promote products derived
+** from this software without specific prior written permission.
+** THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
+** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+*/
+package android.text.format;
+
+import android.content.res.Resources;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.Formatter;
+import java.util.Locale;
+import java.util.TimeZone;
+import libcore.icu.LocaleData;
+import libcore.util.ZoneInfo;
+
+/**
+ * Formatting logic for {@link Time}. Contains a port of Bionic's broken strftime_tz to Java. The
+ * main issue with this implementation is the treatment of characters as ASCII, despite returning
+ * localized (UTF-16) strings from the LocaleData.
+ *
+ * This class is not thread safe.
+ */
+class TimeFormatter {
+ // An arbitrary value outside the range representable by a byte / ASCII character code.
+ private static final int FORCE_LOWER_CASE = 0x100;
+
+ private static final int SECSPERMIN = 60;
+ private static final int MINSPERHOUR = 60;
+ private static final int DAYSPERWEEK = 7;
+ private static final int MONSPERYEAR = 12;
+ private static final int HOURSPERDAY = 24;
+ private static final int DAYSPERLYEAR = 366;
+ private static final int DAYSPERNYEAR = 365;
+
+ /**
+ * The Locale for which the cached LocaleData and formats have been loaded.
+ */
+ private static Locale sLocale;
+ private static LocaleData sLocaleData;
+ private static String sTimeOnlyFormat;
+ private static String sDateOnlyFormat;
+ private static String sDateTimeFormat;
+
+ private final LocaleData localeData;
+ private final String dateTimeFormat;
+ private final String timeOnlyFormat;
+ private final String dateOnlyFormat;
+ private final Locale locale;
+
+ private StringBuilder outputBuilder;
+ private Formatter outputFormatter;
+
+ public TimeFormatter() {
+ synchronized (TimeFormatter.class) {
+ Locale locale = Locale.getDefault();
+
+ if (sLocale == null || !(locale.equals(sLocale))) {
+ sLocale = locale;
+ sLocaleData = LocaleData.get(locale);
+
+ Resources r = Resources.getSystem();
+ sTimeOnlyFormat = r.getString(com.android.internal.R.string.time_of_day);
+ sDateOnlyFormat = r.getString(com.android.internal.R.string.month_day_year);
+ sDateTimeFormat = r.getString(com.android.internal.R.string.date_and_time);
+ }
+
+ this.dateTimeFormat = sDateTimeFormat;
+ this.timeOnlyFormat = sTimeOnlyFormat;
+ this.dateOnlyFormat = sDateOnlyFormat;
+ this.locale = locale;
+ localeData = sLocaleData;
+ }
+ }
+
+ /**
+ * Format the specified {@code wallTime} using {@code pattern}. The output is returned.
+ */
+ public String format(String pattern, ZoneInfo.WallTime wallTime, ZoneInfo zoneInfo) {
+ try {
+ StringBuilder stringBuilder = new StringBuilder();
+
+ outputBuilder = stringBuilder;
+ outputFormatter = new Formatter(stringBuilder, locale);
+
+ formatInternal(pattern, wallTime, zoneInfo);
+ String result = stringBuilder.toString();
+ // This behavior is the source of a bug since some formats are defined as being
+ // in ASCII. Generally localization is very broken.
+ if (localeData.zeroDigit != '0') {
+ result = localizeDigits(result);
+ }
+ return result;
+ } finally {
+ outputBuilder = null;
+ outputFormatter = null;
+ }
+ }
+
+ private String localizeDigits(String s) {
+ int length = s.length();
+ int offsetToLocalizedDigits = localeData.zeroDigit - '0';
+ StringBuilder result = new StringBuilder(length);
+ for (int i = 0; i < length; ++i) {
+ char ch = s.charAt(i);
+ if (ch >= '0' && ch <= '9') {
+ ch += offsetToLocalizedDigits;
+ }
+ result.append(ch);
+ }
+ return result.toString();
+ }
+
+ /**
+ * Format the specified {@code wallTime} using {@code pattern}. The output is written to
+ * {@link #outputBuilder}.
+ */
+ private void formatInternal(String pattern, ZoneInfo.WallTime wallTime, ZoneInfo zoneInfo) {
+ // Convert to ASCII bytes to be compatible with old implementation behavior.
+ byte[] bytes = pattern.getBytes(StandardCharsets.US_ASCII);
+ if (bytes.length == 0) {
+ return;
+ }
+
+ ByteBuffer formatBuffer = ByteBuffer.wrap(bytes);
+ while (formatBuffer.remaining() > 0) {
+ boolean outputCurrentByte = true;
+ char currentByteAsChar = convertToChar(formatBuffer.get(formatBuffer.position()));
+ if (currentByteAsChar == '%') {
+ outputCurrentByte = handleToken(formatBuffer, wallTime, zoneInfo);
+ }
+ if (outputCurrentByte) {
+ currentByteAsChar = convertToChar(formatBuffer.get(formatBuffer.position()));
+ outputBuilder.append(currentByteAsChar);
+ }
+
+ formatBuffer.position(formatBuffer.position() + 1);
+ }
+ }
+
+ private boolean handleToken(ByteBuffer formatBuffer, ZoneInfo.WallTime wallTime,
+ ZoneInfo zoneInfo) {
+
+ // The byte at formatBuffer.position() is expected to be '%' at this point.
+ int modifier = 0;
+ while (formatBuffer.remaining() > 1) {
+ // Increment the position then get the new current byte.
+ formatBuffer.position(formatBuffer.position() + 1);
+ char currentByteAsChar = convertToChar(formatBuffer.get(formatBuffer.position()));
+ switch (currentByteAsChar) {
+ case 'A':
+ modifyAndAppend((wallTime.getWeekDay() < 0
+ || wallTime.getWeekDay() >= DAYSPERWEEK)
+ ? "?" : localeData.longWeekdayNames[wallTime.getWeekDay() + 1],
+ modifier);
+ return false;
+ case 'a':
+ modifyAndAppend((wallTime.getWeekDay() < 0
+ || wallTime.getWeekDay() >= DAYSPERWEEK)
+ ? "?" : localeData.shortWeekdayNames[wallTime.getWeekDay() + 1],
+ modifier);
+ return false;
+ case 'B':
+ if (modifier == '-') {
+ modifyAndAppend((wallTime.getMonth() < 0
+ || wallTime.getMonth() >= MONSPERYEAR)
+ ? "?"
+ : localeData.longStandAloneMonthNames[wallTime.getMonth()],
+ modifier);
+ } else {
+ modifyAndAppend((wallTime.getMonth() < 0
+ || wallTime.getMonth() >= MONSPERYEAR)
+ ? "?" : localeData.longMonthNames[wallTime.getMonth()],
+ modifier);
+ }
+ return false;
+ case 'b':
+ case 'h':
+ modifyAndAppend((wallTime.getMonth() < 0 || wallTime.getMonth() >= MONSPERYEAR)
+ ? "?" : localeData.shortMonthNames[wallTime.getMonth()],
+ modifier);
+ return false;
+ case 'C':
+ outputYear(wallTime.getYear(), true, false, modifier);
+ return false;
+ case 'c':
+ formatInternal(dateTimeFormat, wallTime, zoneInfo);
+ return false;
+ case 'D':
+ formatInternal("%m/%d/%y", wallTime, zoneInfo);
+ return false;
+ case 'd':
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"),
+ wallTime.getMonthDay());
+ return false;
+ case 'E':
+ case 'O':
+ // C99 locale modifiers are not supported.
+ continue;
+ case '_':
+ case '-':
+ case '0':
+ case '^':
+ case '#':
+ modifier = currentByteAsChar;
+ continue;
+ case 'e':
+ outputFormatter.format(getFormat(modifier, "%2d", "%2d", "%d", "%02d"),
+ wallTime.getMonthDay());
+ return false;
+ case 'F':
+ formatInternal("%Y-%m-%d", wallTime, zoneInfo);
+ return false;
+ case 'H':
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"),
+ wallTime.getHour());
+ return false;
+ case 'I':
+ int hour = (wallTime.getHour() % 12 != 0) ? (wallTime.getHour() % 12) : 12;
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"), hour);
+ return false;
+ case 'j':
+ int yearDay = wallTime.getYearDay() + 1;
+ outputFormatter.format(getFormat(modifier, "%03d", "%3d", "%d", "%03d"),
+ yearDay);
+ return false;
+ case 'k':
+ outputFormatter.format(getFormat(modifier, "%2d", "%2d", "%d", "%02d"),
+ wallTime.getHour());
+ return false;
+ case 'l':
+ int n2 = (wallTime.getHour() % 12 != 0) ? (wallTime.getHour() % 12) : 12;
+ outputFormatter.format(getFormat(modifier, "%2d", "%2d", "%d", "%02d"), n2);
+ return false;
+ case 'M':
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"),
+ wallTime.getMinute());
+ return false;
+ case 'm':
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"),
+ wallTime.getMonth() + 1);
+ return false;
+ case 'n':
+ modifyAndAppend("\n", modifier);
+ return false;
+ case 'p':
+ modifyAndAppend((wallTime.getHour() >= (HOURSPERDAY / 2)) ? localeData.amPm[1]
+ : localeData.amPm[0], modifier);
+ return false;
+ case 'P':
+ modifyAndAppend((wallTime.getHour() >= (HOURSPERDAY / 2)) ? localeData.amPm[1]
+ : localeData.amPm[0], FORCE_LOWER_CASE);
+ return false;
+ case 'R':
+ formatInternal("%H:%M", wallTime, zoneInfo);
+ return false;
+ case 'r':
+ formatInternal("%I:%M:%S %p", wallTime, zoneInfo);
+ return false;
+ case 'S':
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"),
+ wallTime.getSecond());
+ return false;
+ case 's':
+ int timeInSeconds = wallTime.mktime(zoneInfo);
+ modifyAndAppend(Integer.toString(timeInSeconds), modifier);
+ return false;
+ case 'T':
+ formatInternal("%H:%M:%S", wallTime, zoneInfo);
+ return false;
+ case 't':
+ modifyAndAppend("\t", modifier);
+ return false;
+ case 'U':
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"),
+ (wallTime.getYearDay() + DAYSPERWEEK - wallTime.getWeekDay())
+ / DAYSPERWEEK);
+ return false;
+ case 'u':
+ int day = (wallTime.getWeekDay() == 0) ? DAYSPERWEEK : wallTime.getWeekDay();
+ outputFormatter.format("%d", day);
+ return false;
+ case 'V': /* ISO 8601 week number */
+ case 'G': /* ISO 8601 year (four digits) */
+ case 'g': /* ISO 8601 year (two digits) */
+ {
+ int year = wallTime.getYear();
+ int yday = wallTime.getYearDay();
+ int wday = wallTime.getWeekDay();
+ int w;
+ while (true) {
+ int len = isLeap(year) ? DAYSPERLYEAR : DAYSPERNYEAR;
+ // What yday (-3 ... 3) does the ISO year begin on?
+ int bot = ((yday + 11 - wday) % DAYSPERWEEK) - 3;
+ // What yday does the NEXT ISO year begin on?
+ int top = bot - (len % DAYSPERWEEK);
+ if (top < -3) {
+ top += DAYSPERWEEK;
+ }
+ top += len;
+ if (yday >= top) {
+ ++year;
+ w = 1;
+ break;
+ }
+ if (yday >= bot) {
+ w = 1 + ((yday - bot) / DAYSPERWEEK);
+ break;
+ }
+ --year;
+ yday += isLeap(year) ? DAYSPERLYEAR : DAYSPERNYEAR;
+ }
+ if (currentByteAsChar == 'V') {
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"), w);
+ } else if (currentByteAsChar == 'g') {
+ outputYear(year, false, true, modifier);
+ } else {
+ outputYear(year, true, true, modifier);
+ }
+ return false;
+ }
+ case 'v':
+ formatInternal("%e-%b-%Y", wallTime, zoneInfo);
+ return false;
+ case 'W':
+ int n = (wallTime.getYearDay() + DAYSPERWEEK - (
+ wallTime.getWeekDay() != 0 ? (wallTime.getWeekDay() - 1)
+ : (DAYSPERWEEK - 1))) / DAYSPERWEEK;
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"), n);
+ return false;
+ case 'w':
+ outputFormatter.format("%d", wallTime.getWeekDay());
+ return false;
+ case 'X':
+ formatInternal(timeOnlyFormat, wallTime, zoneInfo);
+ return false;
+ case 'x':
+ formatInternal(dateOnlyFormat, wallTime, zoneInfo);
+ return false;
+ case 'y':
+ outputYear(wallTime.getYear(), false, true, modifier);
+ return false;
+ case 'Y':
+ outputYear(wallTime.getYear(), true, true, modifier);
+ return false;
+ case 'Z':
+ if (wallTime.getIsDst() < 0) {
+ return false;
+ }
+ boolean isDst = wallTime.getIsDst() != 0;
+ modifyAndAppend(zoneInfo.getDisplayName(isDst, TimeZone.SHORT), modifier);
+ return false;
+ case 'z': {
+ if (wallTime.getIsDst() < 0) {
+ return false;
+ }
+ int diff = wallTime.getGmtOffset();
+ String sign;
+ if (diff < 0) {
+ sign = "-";
+ diff = -diff;
+ } else {
+ sign = "+";
+ }
+ modifyAndAppend(sign, modifier);
+ diff /= SECSPERMIN;
+ diff = (diff / MINSPERHOUR) * 100 + (diff % MINSPERHOUR);
+ outputFormatter.format(getFormat(modifier, "%04d", "%4d", "%d", "%04d"), diff);
+ return false;
+ }
+ case '+':
+ formatInternal("%a %b %e %H:%M:%S %Z %Y", wallTime, zoneInfo);
+ return false;
+ case '%':
+ // If conversion char is undefined, behavior is undefined. Print out the
+ // character itself.
+ default:
+ return true;
+ }
+ }
+ return true;
+ }
+
+ private void modifyAndAppend(CharSequence str, int modifier) {
+ switch (modifier) {
+ case FORCE_LOWER_CASE:
+ for (int i = 0; i < str.length(); i++) {
+ outputBuilder.append(brokenToLower(str.charAt(i)));
+ }
+ break;
+ case '^':
+ for (int i = 0; i < str.length(); i++) {
+ outputBuilder.append(brokenToUpper(str.charAt(i)));
+ }
+ break;
+ case '#':
+ for (int i = 0; i < str.length(); i++) {
+ char c = str.charAt(i);
+ if (brokenIsUpper(c)) {
+ c = brokenToLower(c);
+ } else if (brokenIsLower(c)) {
+ c = brokenToUpper(c);
+ }
+ outputBuilder.append(c);
+ }
+ break;
+ default:
+ outputBuilder.append(str);
+
+ }
+ }
+
+ private void outputYear(int value, boolean outputTop, boolean outputBottom, int modifier) {
+ int lead;
+ int trail;
+
+ final int DIVISOR = 100;
+ trail = value % DIVISOR;
+ lead = value / DIVISOR + trail / DIVISOR;
+ trail %= DIVISOR;
+ if (trail < 0 && lead > 0) {
+ trail += DIVISOR;
+ --lead;
+ } else if (lead < 0 && trail > 0) {
+ trail -= DIVISOR;
+ ++lead;
+ }
+ if (outputTop) {
+ if (lead == 0 && trail < 0) {
+ modifyAndAppend("-0", modifier);
+ } else {
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"), lead);
+ }
+ }
+ if (outputBottom) {
+ int n = ((trail < 0) ? -trail : trail);
+ outputFormatter.format(getFormat(modifier, "%02d", "%2d", "%d", "%02d"), n);
+ }
+ }
+
+ private static String getFormat(int modifier, String normal, String underscore, String dash,
+ String zero) {
+ switch (modifier) {
+ case '_':
+ return underscore;
+ case '-':
+ return dash;
+ case '0':
+ return zero;
+ }
+ return normal;
+ }
+
+ private static boolean isLeap(int year) {
+ return (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0));
+ }
+
+ /**
+ * A broken implementation of {@link Character#isUpperCase(char)} that assumes ASCII in order to
+ * be compatible with the old native implementation.
+ */
+ private static boolean brokenIsUpper(char toCheck) {
+ return toCheck >= 'A' && toCheck <= 'Z';
+ }
+
+ /**
+ * A broken implementation of {@link Character#isLowerCase(char)} that assumes ASCII in order to
+ * be compatible with the old native implementation.
+ */
+ private static boolean brokenIsLower(char toCheck) {
+ return toCheck >= 'a' && toCheck <= 'z';
+ }
+
+ /**
+ * A broken implementation of {@link Character#toLowerCase(char)} that assumes ASCII in order to
+ * be compatible with the old native implementation.
+ */
+ private static char brokenToLower(char input) {
+ if (input >= 'A' && input <= 'Z') {
+ return (char) (input - 'A' + 'a');
+ }
+ return input;
+ }
+
+ /**
+ * A broken implementation of {@link Character#toUpperCase(char)} that assumes ASCII in order to
+ * be compatible with the old native implementation.
+ */
+ private static char brokenToUpper(char input) {
+ if (input >= 'a' && input <= 'z') {
+ return (char) (input - 'a' + 'A');
+ }
+ return input;
+ }
+
+ /**
+ * Safely convert a byte containing an ASCII character to a char, even for character codes
+ * > 127.
+ */
+ private static char convertToChar(byte b) {
+ return (char) (b & 0xFF);
+ }
+}
diff --git a/core/java/android/util/TimeFormatException.java b/core/java/android/util/TimeFormatException.java
index d7a898bf0d20f..f52052368f923 100644
--- a/core/java/android/util/TimeFormatException.java
+++ b/core/java/android/util/TimeFormatException.java
@@ -18,7 +18,11 @@ package android.util;
public class TimeFormatException extends RuntimeException
{
- TimeFormatException(String s)
+
+ /**
+ * @hide
+ */
+ public TimeFormatException(String s)
{
super(s);
}
diff --git a/core/jni/Android.mk b/core/jni/Android.mk
index 7b5395b167b5b..480383b4443e0 100644
--- a/core/jni/Android.mk
+++ b/core/jni/Android.mk
@@ -23,7 +23,6 @@ LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES
LOCAL_SRC_FILES:= \
AndroidRuntime.cpp \
- Time.cpp \
com_android_internal_content_NativeLibraryHelper.cpp \
com_google_android_gles_jni_EGLImpl.cpp \
com_google_android_gles_jni_GLImpl.cpp.arm \
@@ -81,7 +80,6 @@ LOCAL_SRC_FILES:= \
android_net_NetUtils.cpp \
android_net_TrafficStats.cpp \
android_nio_utils.cpp \
- android_text_format_Time.cpp \
android_util_AssetManager.cpp \
android_util_Binder.cpp \
android_util_EventLog.cpp \
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index 92a8fcaa5ec47..3a02ab9d78012 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -144,7 +144,6 @@ extern int register_android_database_SQLiteConnection(JNIEnv* env);
extern int register_android_database_SQLiteGlobal(JNIEnv* env);
extern int register_android_database_SQLiteDebug(JNIEnv* env);
extern int register_android_nio_utils(JNIEnv* env);
-extern int register_android_text_format_Time(JNIEnv* env);
extern int register_android_os_Debug(JNIEnv* env);
extern int register_android_os_MessageQueue(JNIEnv* env);
extern int register_android_os_Parcel(JNIEnv* env);
@@ -1223,7 +1222,6 @@ static const RegJNIRec gRegJNI[] = {
REG_JNI(register_android_util_EventLog),
REG_JNI(register_android_util_Log),
REG_JNI(register_android_util_FloatMath),
- REG_JNI(register_android_text_format_Time),
REG_JNI(register_android_content_AssetManager),
REG_JNI(register_android_content_StringBlock),
REG_JNI(register_android_content_XmlBlock),
diff --git a/core/jni/Time.cpp b/core/jni/Time.cpp
deleted file mode 100644
index f3037f3830390..0000000000000
--- a/core/jni/Time.cpp
+++ /dev/null
@@ -1,199 +0,0 @@
-#include "TimeUtils.h"
-#include