Fix regression introduced in 37e23e8d5082

The idea was good, but it applied the second-precise date format everywhere.
This broke dates on apps and apks, which have the format yyyy-MM-dd on the
index.

Fix this by having two sets of funcs in Utils, one for dates (precise to a
day) and one for times (precise to a second). We should use the latter for
dates we ourselves measure, like the last repo update time.
This commit is contained in:
Daniel Martí 2015-10-07 10:48:42 +02:00
parent a6b416e7c9
commit 995036f4e4
3 changed files with 28 additions and 8 deletions

View File

@ -191,7 +191,7 @@ public class RepoUpdater {
private ContentValues prepareRepoDetailsForSaving(RepoXMLHandler handler, String etag) {
ContentValues values = new ContentValues();
values.put(RepoProvider.DataColumns.LAST_UPDATED, Utils.formatDate(new Date(), ""));
values.put(RepoProvider.DataColumns.LAST_UPDATED, Utils.formatTime(new Date(), ""));
if (repo.lastetag == null || !repo.lastetag.equals(etag)) {
values.put(RepoProvider.DataColumns.LAST_ETAG, etag);
@ -305,7 +305,7 @@ public class RepoUpdater {
if (trustNewSigningCertificate) {
Utils.debugLog(TAG, "Saving new signing certificate in the database for " + repo.address);
ContentValues values = new ContentValues(2);
values.put(RepoProvider.DataColumns.LAST_UPDATED, Utils.formatDate(new Date(), ""));
values.put(RepoProvider.DataColumns.LAST_UPDATED, Utils.formatTime(new Date(), ""));
values.put(RepoProvider.DataColumns.PUBLIC_KEY, Hasher.hex(rawCertFromJar));
RepoProvider.Helper.update(context, repo, values);
}

View File

@ -58,6 +58,7 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -76,6 +77,9 @@ public final class Utils {
// The date format used for storing dates (e.g. lastupdated, added) in the
// database.
private static final SimpleDateFormat DATE_FORMAT =
new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
private static final SimpleDateFormat TIME_FORMAT =
new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss", Locale.ENGLISH);
private static final String[] FRIENDLY_SIZE_FORMAT = {
@ -562,24 +566,40 @@ public final class Utils {
return result;
}
public static Date parseDate(String str, Date fallback) {
private static Date parseDateFormat(DateFormat format, String str, Date fallback) {
if (str == null || str.length() == 0) {
return fallback;
}
Date result;
try {
result = DATE_FORMAT.parse(str);
result = format.parse(str);
} catch (ParseException e) {
result = fallback;
}
return result;
}
public static String formatDate(Date date, String fallback) {
private static String formatDateFormat(DateFormat format, Date date, String fallback) {
if (date == null) {
return fallback;
}
return DATE_FORMAT.format(date);
return format.format(date);
}
public static Date parseDate(String str, Date fallback) {
return parseDateFormat(DATE_FORMAT, str, fallback);
}
public static String formatDate(Date date, String fallback) {
return formatDateFormat(DATE_FORMAT, date, fallback);
}
public static Date parseTime(String str, Date fallback) {
return parseDateFormat(TIME_FORMAT, str, fallback);
}
public static String formatTime(Date date, String fallback) {
return formatDateFormat(TIME_FORMAT, date, fallback);
}
// Need this to add the unimplemented support for ordered and unordered

View File

@ -65,7 +65,7 @@ public class Repo extends ValueObject {
inuse = cursor.getInt(i) == 1;
break;
case RepoProvider.DataColumns.LAST_UPDATED:
lastUpdated = Utils.parseDate(cursor.getString(i), null);
lastUpdated = Utils.parseTime(cursor.getString(i), null);
break;
case RepoProvider.DataColumns.MAX_AGE:
maxage = cursor.getInt(i);
@ -160,7 +160,7 @@ public class Repo extends ValueObject {
if (values.containsKey(RepoProvider.DataColumns.LAST_UPDATED)) {
final String dateString = values.getAsString(RepoProvider.DataColumns.LAST_UPDATED);
lastUpdated = Utils.parseDate(dateString, null);
lastUpdated = Utils.parseTime(dateString, null);
}
if (values.containsKey(RepoProvider.DataColumns.MAX_AGE)) {