mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 17:29:32 +01:00
Pluralize time strings.
This commit is contained in:
committed by
Greyson Parrelli
parent
7542614580
commit
83aee4a084
@@ -26,6 +26,7 @@ import org.thoughtcrime.securesms.conversation.v2.computed.FormattedDate
|
||||
import java.text.DateFormatSymbols
|
||||
import java.text.ParseException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import java.util.concurrent.TimeUnit
|
||||
@@ -182,14 +183,30 @@ object DateUtils : android.text.format.DateUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the timestamp as a date, without the year, followed by the time
|
||||
* eg. Jan 15 at 9:00pm
|
||||
* Given a timestamp, formats as "at time".
|
||||
* Pluralization allows for Romance languages to be translated correctly
|
||||
* eg. at 7:23pm, at 13:20
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getOnlyTimeAtString(context: Context, timestamp: Long): String {
|
||||
val time = timestamp.toLocalTime().formatHours(context)
|
||||
val hour = getHour(context, timestamp)
|
||||
|
||||
return context.resources.getQuantityString(R.plurals.DateUtils_time_at, hour, time)
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the timestamp as a date, without the year, followed by the time.
|
||||
* Pluralization allows for Romance languages to be translated correctly
|
||||
* eg. on Jan 15 at 9:00pm
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getDateTimeString(context: Context, locale: Locale, timestamp: Long): String {
|
||||
val date = timestamp.toDateString("MMM d", locale)
|
||||
val time = timestamp.toLocalTime().formatHours(context)
|
||||
return context.getString(R.string.DateUtils_date_at, date, time)
|
||||
val hour = getHour(context, timestamp)
|
||||
|
||||
return context.resources.getQuantityString(R.plurals.DateUtils_date_time_at, hour, date, time)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -363,6 +380,16 @@ object DateUtils : android.text.format.DateUtils() {
|
||||
return isToday(time + TimeUnit.DAYS.toMillis(1))
|
||||
}
|
||||
|
||||
private fun getHour(context: Context, timestamp: Long): Int {
|
||||
val cal = Calendar.getInstance(Locale.getDefault())
|
||||
cal.timeInMillis = timestamp
|
||||
return if (context.is24HourFormat()) {
|
||||
cal[Calendar.HOUR_OF_DAY]
|
||||
} else {
|
||||
cal[Calendar.HOUR]
|
||||
}
|
||||
}
|
||||
|
||||
private fun Context.is24HourFormat(): Boolean {
|
||||
is24HourDateCache?.let {
|
||||
if (it.lastUpdated.isWithin(10.seconds)) {
|
||||
|
||||
Reference in New Issue
Block a user