1 package net.pterodactylus.sone.text
3 import net.pterodactylus.sone.freenet.L10nText
4 import java.util.concurrent.TimeUnit.DAYS
5 import java.util.concurrent.TimeUnit.HOURS
6 import java.util.concurrent.TimeUnit.MILLISECONDS
7 import java.util.concurrent.TimeUnit.MINUTES
8 import java.util.concurrent.TimeUnit.SECONDS
11 * Converts a time (in Java milliseconds) to an L10n key and a refresh time.
13 class TimeTextConverter(private val timeSuppler: () -> Long = { System.currentTimeMillis() }) {
15 fun getTimeText(time: Long): TimeText {
16 val age = timeSuppler.invoke() - time
18 time == 0L -> TimeText(L10nText("View.Sone.Text.UnknownDate"), 12.hours())
19 age < 0 -> TimeText(L10nText("View.Time.InTheFuture"), 5.minutes())
20 age < 20.seconds() -> TimeText(L10nText("View.Time.AFewSecondsAgo"), 10.seconds())
21 age < 45.seconds() -> TimeText(L10nText("View.Time.HalfAMinuteAgo"), 20.seconds())
22 age < 90.seconds() -> TimeText(L10nText("View.Time.AMinuteAgo"), 1.minutes())
23 age < 30.minutes() -> TimeText(L10nText("View.Time.XMinutesAgo", listOf((age + 30.seconds()).toMinutes())), 1.minutes())
24 age < 45.minutes() -> TimeText(L10nText("View.Time.HalfAnHourAgo"), 10.minutes())
25 age < 90.minutes() -> TimeText(L10nText("View.Time.AnHourAgo"), 1.hours())
26 age < 21.hours() -> TimeText(L10nText("View.Time.XHoursAgo", listOf((age + 30.minutes()).toHours())), 1.hours())
27 age < 42.hours() -> TimeText(L10nText("View.Time.ADayAgo"), 1.days())
28 age < 6.days() -> TimeText(L10nText("View.Time.XDaysAgo", listOf((age + 12.hours()).toDays())), 1.days())
29 age < 11.days() -> TimeText(L10nText("View.Time.AWeekAgo"), 1.days())
30 age < 28.days() -> TimeText(L10nText("View.Time.XWeeksAgo", listOf((age + 3.days() + 12.hours()).toWeeks())), 1.days())
31 age < 42.days() -> TimeText(L10nText("View.Time.AMonthAgo"), 1.days())
32 age < 330.days() -> TimeText(L10nText("View.Time.XMonthsAgo", listOf((age + 15.days()).toMonths())), 1.days())
33 age < 540.days() -> TimeText(L10nText("View.Time.AYearAgo"), 7.days())
34 else -> TimeText(L10nText("View.Time.XYearsAgo", listOf((age + 182.days() + 12.hours()).toYears())), 7.days())
38 private fun Long.toMinutes() = MILLISECONDS.toMinutes(this)
39 private fun Long.toHours() = MILLISECONDS.toHours(this)
40 private fun Long.toDays() = MILLISECONDS.toDays(this)
41 private fun Long.toWeeks() = MILLISECONDS.toDays(this) / 7
42 private fun Long.toMonths() = MILLISECONDS.toDays(this) / 30
43 private fun Long.toYears() = MILLISECONDS.toDays(this) / 365
44 private fun Int.seconds() = SECONDS.toMillis(this.toLong())
45 private fun Int.minutes() = MINUTES.toMillis(this.toLong())
46 private fun Int.hours() = HOURS.toMillis(this.toLong())
47 private fun Int.days() = DAYS.toMillis(this.toLong())