mirror of
https://github.com/M66B/FairEmail.git
synced 2026-04-01 06:38:29 +02:00
Workaround 12/24 hour Android 5.1/6 bug
This commit is contained in:
@@ -45,6 +45,8 @@ import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.os.PowerManager;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.DateUtils;
|
||||
import android.text.format.Time;
|
||||
import android.view.Display;
|
||||
import android.view.Menu;
|
||||
import android.view.View;
|
||||
@@ -101,6 +103,7 @@ import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
@@ -1163,4 +1166,32 @@ public class Helper {
|
||||
bundle.writeToParcel(p, 0);
|
||||
return p.dataSize();
|
||||
}
|
||||
|
||||
static DateFormat getTimeInstance(Context context, int style) {
|
||||
// https://issuetracker.google.com/issues/37054851
|
||||
if (context != null &&
|
||||
(style == SimpleDateFormat.SHORT || style == SimpleDateFormat.MEDIUM)) {
|
||||
Locale locale = Locale.getDefault();
|
||||
boolean is24Hour = android.text.format.DateFormat.is24HourFormat(context);
|
||||
String skeleton = (is24Hour ? "Hm" : "hm");
|
||||
if (style == SimpleDateFormat.MEDIUM)
|
||||
skeleton += "s";
|
||||
String pattern = android.text.format.DateFormat.getBestDateTimePattern(locale, skeleton);
|
||||
return new SimpleDateFormat(pattern, locale);
|
||||
} else
|
||||
return SimpleDateFormat.getTimeInstance(style);
|
||||
}
|
||||
|
||||
static CharSequence getRelativeTimeSpanString(Context context, long millis) {
|
||||
long now = System.currentTimeMillis();
|
||||
long span = Math.abs(now - millis);
|
||||
Time nowTime = new Time();
|
||||
Time thenTime = new Time();
|
||||
nowTime.set(now);
|
||||
thenTime.set(millis);
|
||||
if (span < DateUtils.DAY_IN_MILLIS && nowTime.weekDay == thenTime.weekDay)
|
||||
return getTimeInstance(context, SimpleDateFormat.SHORT).format(millis);
|
||||
else
|
||||
return DateUtils.getRelativeTimeSpanString(context, millis);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user