From 5e711f6c57444517930eb0c56fd66ecae3c30896 Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Fri, 24 Jul 2020 22:31:47 +1200 Subject: [PATCH 1/2] Fix relative date time formatting (#822) The transitionResolution parameter has to go back to the beginning of the day. In general, going back 24h is not enough. --- .../app/core/date/VectorDateFormatter.kt | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/app/core/date/VectorDateFormatter.kt b/vector/src/main/java/im/vector/app/core/date/VectorDateFormatter.kt index f383ea8216..1b92511104 100644 --- a/vector/src/main/java/im/vector/app/core/date/VectorDateFormatter.kt +++ b/vector/src/main/java/im/vector/app/core/date/VectorDateFormatter.kt @@ -21,8 +21,24 @@ import android.text.format.DateUtils import im.vector.app.core.resources.LocaleProvider import org.threeten.bp.LocalDateTime import org.threeten.bp.format.DateTimeFormatter +import java.util.Calendar +import java.util.Date import javax.inject.Inject +/** + * Returns the timestamp for the start of the day of the provided time. + * For example, for the time "Jul 21, 11:11" the start of the day: "Jul 21, 00:00" is returned. + */ +fun startOfDay(time: Long): Long { + val calendar = Calendar.getInstance() + calendar.time = Date(time) + calendar.set(Calendar.HOUR_OF_DAY, 0) + calendar.set(Calendar.MINUTE, 0) + calendar.set(Calendar.SECOND, 0) + calendar.set(Calendar.MILLISECOND, 0) + return calendar.time.time +} + class VectorDateFormatter @Inject constructor(private val context: Context, private val localeProvider: LocaleProvider) { @@ -41,15 +57,23 @@ class VectorDateFormatter @Inject constructor(private val context: Context, return messageDayFormatter.format(localDateTime) } + /** + * Formats a localized relative date time for the last 2 days, e.g, "Today, HH:MM", "Yesterday, HH:MM" or + * "2 days ago, HH:MM". + * For earlier timestamps the absolute date time is returned, e.g. "Month Day, HH:MM". + * + * @param time the absolute timestamp [ms] that should be formatted relative to now + */ fun formatRelativeDateTime(time: Long?): String { if (time == null) { return "" } + val now = System.currentTimeMillis() return DateUtils.getRelativeDateTimeString( context, time, DateUtils.DAY_IN_MILLIS, - 2 * DateUtils.DAY_IN_MILLIS, + now - startOfDay(now - 2 * DateUtils.DAY_IN_MILLIS), DateUtils.FORMAT_SHOW_WEEKDAY or DateUtils.FORMAT_SHOW_TIME ).toString() } From 159881645c0dd1796ace42e1eca871a1a8b74b2c Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Fri, 24 Jul 2020 22:35:33 +1200 Subject: [PATCH 2/2] Add change log entry --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 6f40a758fc..b8f7adbeb0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ Improvements 🙌: Bugfix 🐛: - Fix invisible toolbar (Status.im theme) (#1746) + - Fix relative date time formatting (#822) Translations 🗣: - Add PlayStore description resources in the Triple-T format, to let Weblate handle them