mirror of https://github.com/tuskyapp/Tusky.git
Browse Source
* Improve time format of posts when using absolute time * fix AbsoluteTimeFormatter, add tests * fix tests Co-authored-by: Conny Duck <k.pozniak@gmx.at>pull/2437/head
6 changed files with 127 additions and 61 deletions
@ -0,0 +1,59 @@
|
||||
/* Copyright 2022 Tusky Contributors |
||||
* |
||||
* This file is a part of Tusky. |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the |
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||
* Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not, |
||||
* see <http://www.gnu.org/licenses>. */ |
||||
|
||||
package com.keylesspalace.tusky.util |
||||
|
||||
import java.text.SimpleDateFormat |
||||
import java.util.Calendar |
||||
import java.util.Date |
||||
import java.util.Locale |
||||
import java.util.TimeZone |
||||
|
||||
class AbsoluteTimeFormatter @JvmOverloads constructor(private val tz: TimeZone = TimeZone.getDefault()) { |
||||
private val sameDaySdf = SimpleDateFormat("HH:mm", Locale.getDefault()).apply { this.timeZone = tz } |
||||
private val sameYearSdf = SimpleDateFormat("MM-dd HH:mm", Locale.getDefault()).apply { this.timeZone = tz } |
||||
private val otherYearSdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).apply { this.timeZone = tz } |
||||
private val otherYearCompleteSdf = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).apply { this.timeZone = tz } |
||||
|
||||
@JvmOverloads |
||||
fun format(time: Date?, shortFormat: Boolean = true, now: Date = Date()): String { |
||||
return when { |
||||
time == null -> "??" |
||||
isSameDate(time, now, tz) -> sameDaySdf.format(time) |
||||
isSameYear(time, now, tz) -> sameYearSdf.format(time) |
||||
shortFormat -> otherYearSdf.format(time) |
||||
else -> otherYearCompleteSdf.format(time) |
||||
} |
||||
} |
||||
|
||||
companion object { |
||||
|
||||
private fun isSameDate(dateOne: Date, dateTwo: Date, tz: TimeZone): Boolean { |
||||
val calendarOne = Calendar.getInstance(tz).apply { time = dateOne } |
||||
val calendarTwo = Calendar.getInstance(tz).apply { time = dateTwo } |
||||
|
||||
return calendarOne.get(Calendar.YEAR) == calendarTwo.get(Calendar.YEAR) && |
||||
calendarOne.get(Calendar.MONTH) == calendarTwo.get(Calendar.MONTH) && |
||||
calendarOne.get(Calendar.DAY_OF_MONTH) == calendarTwo.get(Calendar.DAY_OF_MONTH) |
||||
} |
||||
|
||||
private fun isSameYear(dateOne: Date, dateTwo: Date, timeZone1: TimeZone): Boolean { |
||||
val calendarOne = Calendar.getInstance(timeZone1).apply { time = dateOne } |
||||
val calendarTwo = Calendar.getInstance(timeZone1).apply { time = dateTwo } |
||||
|
||||
return calendarOne.get(Calendar.YEAR) == calendarTwo.get(Calendar.YEAR) |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,46 @@
|
||||
package com.keylesspalace.tusky.util |
||||
|
||||
import org.junit.Assert.assertEquals |
||||
import org.junit.Test |
||||
import java.time.Instant |
||||
import java.util.Date |
||||
import java.util.TimeZone |
||||
|
||||
class AbsoluteTimeFormatterTest { |
||||
|
||||
private val formatter = AbsoluteTimeFormatter(TimeZone.getTimeZone("UTC")) |
||||
private val now = Date.from(Instant.parse("2022-04-11T00:00:00.00Z")) |
||||
|
||||
@Test |
||||
fun `null handling`() { |
||||
assertEquals("??", formatter.format(null, true, now)) |
||||
assertEquals("??", formatter.format(null, false, now)) |
||||
} |
||||
|
||||
@Test |
||||
fun `same day formatting`() { |
||||
val tenTen = Date.from(Instant.parse("2022-04-11T10:10:00.00Z")) |
||||
assertEquals("10:10", formatter.format(tenTen, true, now)) |
||||
assertEquals("10:10", formatter.format(tenTen, false, now)) |
||||
} |
||||
|
||||
@Test |
||||
fun `same year formatting`() { |
||||
val nextDay = Date.from(Instant.parse("2022-04-12T00:10:00.00Z")) |
||||
assertEquals("04-12 00:10", formatter.format(nextDay, true, now)) |
||||
assertEquals("04-12 00:10", formatter.format(nextDay, false, now)) |
||||
val endOfYear = Date.from(Instant.parse("2022-12-31T23:59:00.00Z")) |
||||
assertEquals("12-31 23:59", formatter.format(endOfYear, true, now)) |
||||
assertEquals("12-31 23:59", formatter.format(endOfYear, false, now)) |
||||
} |
||||
|
||||
@Test |
||||
fun `other year formatting`() { |
||||
val firstDayNextYear = Date.from(Instant.parse("2023-01-01T00:00:00.00Z")) |
||||
assertEquals("2023-01-01", formatter.format(firstDayNextYear, true, now)) |
||||
assertEquals("2023-01-01 00:00", formatter.format(firstDayNextYear, false, now)) |
||||
val inTenYears = Date.from(Instant.parse("2032-04-11T10:10:00.00Z")) |
||||
assertEquals("2032-04-11", formatter.format(inTenYears, true, now)) |
||||
assertEquals("2032-04-11 10:10", formatter.format(inTenYears, false, now)) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue