TimeUtilspublic class TimeUtils extends Object A class containing utility methods related to time zones. |
Fields Summary |
---|
private static final boolean | DBG | private static final String | TAG | private static final Object | sLastLockObjCached results of getTineZones | private static ArrayList | sLastZones | private static String | sLastCountry | private static final Object | sLastUniqueLockObjCached results of getTimeZonesWithUniqueOffsets | private static ArrayList | sLastUniqueZoneOffsets | private static String | sLastUniqueCountry | public static final int | HUNDRED_DAY_FIELD_LEN | private static final int | SECONDS_PER_MINUTE | private static final int | SECONDS_PER_HOUR | private static final int | SECONDS_PER_DAY | public static final long | NANOS_PER_MS | private static final Object | sFormatSync | private static char[] | sFormatStr | private static final long | LARGEST_DURATION |
Constructors Summary |
---|
public TimeUtils()
|
Methods Summary |
---|
private static int | accumField(int amt, int suffix, boolean always, int zeropad)
if (amt > 99 || (always && zeropad >= 3)) {
return 3+suffix;
}
if (amt > 9 || (always && zeropad >= 2)) {
return 2+suffix;
}
if (always || amt > 0) {
return 1+suffix;
}
return 0;
| public static void | formatDuration(long duration, java.io.PrintWriter pw, int fieldLen)
synchronized (sFormatSync) {
int len = formatDurationLocked(duration, fieldLen);
pw.print(new String(sFormatStr, 0, len));
}
| public static void | formatDuration(long duration, java.io.PrintWriter pw)
formatDuration(duration, pw, 0);
| public static void | formatDuration(long time, long now, java.io.PrintWriter pw)
if (time == 0) {
pw.print("--");
return;
}
formatDuration(time-now, pw, 0);
| public static void | formatDuration(long duration, java.lang.StringBuilder builder)
synchronized (sFormatSync) {
int len = formatDurationLocked(duration, 0);
builder.append(sFormatStr, 0, len);
}
| private static int | formatDurationLocked(long duration, int fieldLen)
if (sFormatStr.length < fieldLen) {
sFormatStr = new char[fieldLen];
}
char[] formatStr = sFormatStr;
if (duration == 0) {
int pos = 0;
fieldLen -= 1;
while (pos < fieldLen) {
formatStr[pos++] = ' ";
}
formatStr[pos] = '0";
return pos+1;
}
char prefix;
if (duration > 0) {
prefix = '+";
} else {
prefix = '-";
duration = -duration;
}
if (duration > LARGEST_DURATION) {
duration = LARGEST_DURATION;
}
int millis = (int)(duration%1000);
int seconds = (int) Math.floor(duration / 1000);
int days = 0, hours = 0, minutes = 0;
if (seconds > SECONDS_PER_DAY) {
days = seconds / SECONDS_PER_DAY;
seconds -= days * SECONDS_PER_DAY;
}
if (seconds > SECONDS_PER_HOUR) {
hours = seconds / SECONDS_PER_HOUR;
seconds -= hours * SECONDS_PER_HOUR;
}
if (seconds > SECONDS_PER_MINUTE) {
minutes = seconds / SECONDS_PER_MINUTE;
seconds -= minutes * SECONDS_PER_MINUTE;
}
int pos = 0;
if (fieldLen != 0) {
int myLen = accumField(days, 1, false, 0);
myLen += accumField(hours, 1, myLen > 0, 2);
myLen += accumField(minutes, 1, myLen > 0, 2);
myLen += accumField(seconds, 1, myLen > 0, 2);
myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1;
while (myLen < fieldLen) {
formatStr[pos] = ' ";
pos++;
myLen++;
}
}
formatStr[pos] = prefix;
pos++;
int start = pos;
boolean zeropad = fieldLen != 0;
pos = printField(formatStr, days, 'd", pos, false, 0);
pos = printField(formatStr, hours, 'h", pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, minutes, 'm", pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, seconds, 's", pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, millis, 'm", pos, true, (zeropad && pos != start) ? 3 : 0);
formatStr[pos] = 's";
return pos + 1;
| public static java.lang.String | formatUptime(long time)
final long diff = time - SystemClock.uptimeMillis();
if (diff > 0) {
return time + " (in " + diff + " ms)";
}
if (diff < 0) {
return time + " (" + -diff + " ms ago)";
}
return time + " (now)";
| public static java.util.TimeZone | getTimeZone(int offset, boolean dst, long when, java.lang.String country)Tries to return a time zone that would have had the specified offset
and DST value at the specified moment in the specified country.
Returns null if no suitable zone could be found.
TimeZone best = null;
final Date d = new Date(when);
TimeZone current = TimeZone.getDefault();
String currentName = current.getID();
int currentOffset = current.getOffset(when);
boolean currentDst = current.inDaylightTime(d);
for (TimeZone tz : getTimeZones(country)) {
// If the current time zone is from the right country
// and meets the other known properties, keep it
// instead of changing to another one.
if (tz.getID().equals(currentName)) {
if (currentOffset == offset && currentDst == dst) {
return current;
}
}
// Otherwise, take the first zone from the right
// country that has the correct current offset and DST.
// (Keep iterating instead of returning in case we
// haven't encountered the current time zone yet.)
if (best == null) {
if (tz.getOffset(when) == offset &&
tz.inDaylightTime(d) == dst) {
best = tz;
}
}
}
return best;
| public static java.lang.String | getTimeZoneDatabaseVersion()Returns a String indicating the version of the time zone database currently
in use. The format of the string is dependent on the underlying time zone
database implementation, but will typically contain the year in which the database
was updated plus a letter from a to z indicating changes made within that year.
Time zone database updates should be expected to occur periodically due to
political and legal changes that cannot be anticipated in advance. Therefore,
when computing the UTC time for a future event, applications should be aware that
the results may differ following a time zone database update. This method allows
applications to detect that a database change has occurred, and to recalculate any
cached times accordingly.
The time zone database may be assumed to change only when the device runtime
is restarted. Therefore, it is not necessary to re-query the database version
during the lifetime of an activity.
return ZoneInfoDB.getInstance().getVersion();
| public static java.util.ArrayList | getTimeZones(java.lang.String country)Returns the time zones for the country, which is the code
attribute of the timezone element in time_zones_by_country.xml. Do not modify.
synchronized (sLastLockObj) {
if ((country != null) && country.equals(sLastCountry)) {
if (DBG) Log.d(TAG, "getTimeZones(" + country + "): return cached version");
return sLastZones;
}
}
ArrayList<TimeZone> tzs = new ArrayList<TimeZone>();
if (country == null) {
if (DBG) Log.d(TAG, "getTimeZones(null): return empty list");
return tzs;
}
Resources r = Resources.getSystem();
XmlResourceParser parser = r.getXml(com.android.internal.R.xml.time_zones_by_country);
try {
XmlUtils.beginDocument(parser, "timezones");
while (true) {
XmlUtils.nextElement(parser);
String element = parser.getName();
if (element == null || !(element.equals("timezone"))) {
break;
}
String code = parser.getAttributeValue(null, "code");
if (country.equals(code)) {
if (parser.next() == XmlPullParser.TEXT) {
String zoneIdString = parser.getText();
TimeZone tz = TimeZone.getTimeZone(zoneIdString);
if (tz.getID().startsWith("GMT") == false) {
// tz.getID doesn't start not "GMT" so its valid
tzs.add(tz);
if (DBG) {
Log.d(TAG, "getTimeZone('" + country + "'): found tz.getID=="
+ ((tz != null) ? tz.getID() : "<no tz>"));
}
}
}
}
}
} catch (XmlPullParserException e) {
Log.e(TAG, "Got xml parser exception getTimeZone('" + country + "'): e=", e);
} catch (IOException e) {
Log.e(TAG, "Got IO exception getTimeZone('" + country + "'): e=", e);
} finally {
parser.close();
}
synchronized(sLastLockObj) {
// Cache the last result;
sLastZones = tzs;
sLastCountry = country;
return sLastZones;
}
| public static java.util.ArrayList | getTimeZonesWithUniqueOffsets(java.lang.String country)Return list of unique time zones for the country. Do not modify
synchronized(sLastUniqueLockObj) {
if ((country != null) && country.equals(sLastUniqueCountry)) {
if (DBG) {
Log.d(TAG, "getTimeZonesWithUniqueOffsets(" +
country + "): return cached version");
}
return sLastUniqueZoneOffsets;
}
}
Collection<TimeZone> zones = getTimeZones(country);
ArrayList<TimeZone> uniqueTimeZones = new ArrayList<TimeZone>();
for (TimeZone zone : zones) {
// See if we already have this offset,
// Using slow but space efficient and these are small.
boolean found = false;
for (int i = 0; i < uniqueTimeZones.size(); i++) {
if (uniqueTimeZones.get(i).getRawOffset() == zone.getRawOffset()) {
found = true;
break;
}
}
if (found == false) {
if (DBG) {
Log.d(TAG, "getTimeZonesWithUniqueOffsets: add unique offset=" +
zone.getRawOffset() + " zone.getID=" + zone.getID());
}
uniqueTimeZones.add(zone);
}
}
synchronized(sLastUniqueLockObj) {
// Cache the last result
sLastUniqueZoneOffsets = uniqueTimeZones;
sLastUniqueCountry = country;
return sLastUniqueZoneOffsets;
}
| public static java.lang.String | logTimeOfDay(long millis)Convert a System.currentTimeMillis() value to a time of day value like
that printed in logs. MM-DD HH:MM:SS.MMM
Calendar c = Calendar.getInstance();
if (millis >= 0) {
c.setTimeInMillis(millis);
return String.format("%tm-%td %tH:%tM:%tS.%tL", c, c, c, c, c, c);
} else {
return Long.toString(millis);
}
| private static int | printField(char[] formatStr, int amt, char suffix, int pos, boolean always, int zeropad)
if (always || amt > 0) {
final int startPos = pos;
if ((always && zeropad >= 3) || amt > 99) {
int dig = amt/100;
formatStr[pos] = (char)(dig + '0");
pos++;
amt -= (dig*100);
}
if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
int dig = amt/10;
formatStr[pos] = (char)(dig + '0");
pos++;
amt -= (dig*10);
}
formatStr[pos] = (char)(amt + '0");
pos++;
formatStr[pos] = suffix;
pos++;
}
return pos;
|
|