Adds ability to localize timestamps.

This commit is contained in:
Vavassor 2017-07-14 19:45:26 -04:00
parent 8559a6d75c
commit d55181ccd0
3 changed files with 47 additions and 13 deletions

View File

@ -169,7 +169,7 @@ public class StatusViewHolder extends RecyclerView.ViewHolder {
if (createdAt != null) { if (createdAt != null) {
long then = createdAt.getTime(); long then = createdAt.getTime();
long now = new Date().getTime(); long now = new Date().getTime();
readout = DateUtils.getRelativeTimeSpanString(then, now); readout = DateUtils.getRelativeTimeSpanString(sinceCreated.getContext(), then, now);
readoutAloud = android.text.format.DateUtils.getRelativeTimeSpanString(then, now, readoutAloud = android.text.format.DateUtils.getRelativeTimeSpanString(then, now,
android.text.format.DateUtils.SECOND_IN_MILLIS, android.text.format.DateUtils.SECOND_IN_MILLIS,
android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE); android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE);

View File

@ -15,38 +15,60 @@
package com.keylesspalace.tusky.util; package com.keylesspalace.tusky.util;
import java.text.NumberFormat; import android.content.Context;
import com.keylesspalace.tusky.R;
public class DateUtils { public class DateUtils {
/* This is a rough duplicate of android.text.format.DateUtils.getRelativeTimeSpanString, /* This is a rough duplicate of android.text.format.DateUtils.getRelativeTimeSpanString,
* but even with the FORMAT_ABBREV_RELATIVE flag it wasn't abbreviating enough. */ * but even with the FORMAT_ABBREV_RELATIVE flag it wasn't abbreviating enough. */
public static String getRelativeTimeSpanString(long then, long now) { public static String getRelativeTimeSpanString(Context context, long then, long now) {
final long MINUTE = 60; final long MINUTE = 60;
final long HOUR = 60 * MINUTE; final long HOUR = 60 * MINUTE;
final long DAY = 24 * HOUR; final long DAY = 24 * HOUR;
final long YEAR = 365 * DAY; final long YEAR = 365 * DAY;
long span = (now - then) / 1000; long span = (now - then) / 1000;
String prefix = ""; boolean future = false;
if (span < 0) { if (span < 0) {
prefix = "in "; future = true;
span = -span; span = -span;
} }
String unit; String format;
if (span < MINUTE) { if (span < MINUTE) {
unit = "s"; if (future) {
format = context.getString(R.string.abbreviated_in_seconds);
} else {
format = context.getString(R.string.abbreviated_seconds_ago);
}
} else if (span < HOUR) { } else if (span < HOUR) {
span /= MINUTE; span /= MINUTE;
unit = "m"; if (future) {
format = context.getString(R.string.abbreviated_in_minutes);
} else {
format = context.getString(R.string.abbreviated_minutes_ago);
}
} else if (span < DAY) { } else if (span < DAY) {
span /= HOUR; span /= HOUR;
unit = "h"; if (future) {
format = context.getString(R.string.abbreviated_in_hours);
} else {
format = context.getString(R.string.abbreviated_hours_ago);
}
} else if (span < YEAR) { } else if (span < YEAR) {
span /= DAY; span /= DAY;
unit = "d"; if (future) {
format = context.getString(R.string.abbreviated_in_days);
} else {
format = context.getString(R.string.abbreviated_days_ago);
}
} else { } else {
span /= YEAR; span /= YEAR;
unit = "y"; if (future) {
format = context.getString(R.string.abbreviated_in_years);
} else {
format = context.getString(R.string.abbreviated_years_ago);
} }
return prefix + NumberFormat.getIntegerInstance().format(span) + unit; }
return String.format(format, span);
} }
} }

View File

@ -197,6 +197,18 @@
<string name="combine_s1_s2">%1$s %2$s</string> <string name="combine_s1_s2">%1$s %2$s</string>
<string name="no_content">no content</string> <string name="no_content">no content</string>
<string name="action_save_one_toot">Toot saved !</string> <string name="action_save_one_toot">Toot saved!</string>
<!--These are for timestamps on statuses. For example: "16s" or "2d"-->
<string name="abbreviated_in_years">in %dy</string>
<string name="abbreviated_in_days">in %dd</string>
<string name="abbreviated_in_hours">in %dh</string>
<string name="abbreviated_in_minutes">in %dm</string>
<string name="abbreviated_in_seconds">in %ds</string>
<string name="abbreviated_years_ago">%dy</string>
<string name="abbreviated_days_ago">%dd</string>
<string name="abbreviated_hours_ago">%dh</string>
<string name="abbreviated_minutes_ago">%dm</string>
<string name="abbreviated_seconds_ago">%ds</string>
</resources> </resources>