Skip to content

Instantly share code, notes, and snippets.

@f3401pal
Last active January 31, 2023 16:14
Show Gist options
  • Save f3401pal/d87f53e0a6754336dd30627acff53bc5 to your computer and use it in GitHub Desktop.
Save f3401pal/d87f53e0a6754336dd30627acff53bc5 to your computer and use it in GitHub Desktop.
Relative time utils
/**
* Parse a local date time string into a UTC date time object
* TODO: update to Java 8 time API https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html, return java.time.Instant
*/
fun String.toDate(
dateFormat: String = "yyyy-MM-dd'T'HH:mm:ss",
timeZone: TimeZone = TimeZone.getTimeZone("UTC")
): Date? {
val parser = SimpleDateFormat(dateFormat, Locale.getDefault())
parser.timeZone = timeZone
return parser.parse(this)
}
fun String.toRelativeTimeStr(
now: Long = System.currentTimeMillis(),
format: Int = FORMAT_ABBREV_ALL
): CharSequence? {
return toDate()?.time?.toRelativeTimeStr(now, format)
}
fun Long.toRelativeTimeStr(
now: Long = System.currentTimeMillis(),
format: Int = FORMAT_ABBREV_ALL
): CharSequence? {
val timeDelta = now - this
return when {
timeDelta < MINUTE_IN_MILLIS -> "Just Now"
timeDelta < HOUR_IN_MILLIS -> getRelativeTimeSpanString(
this,
now,
MINUTE_IN_MILLIS,
FORMAT_ABBREV_ALL
)
timeDelta < DAY_IN_MILLIS -> getRelativeTimeSpanString(
this,
now,
HOUR_IN_MILLIS,
FORMAT_ABBREV_ALL
)
else -> getRelativeTimeSpanString(
this,
now,
DAY_IN_MILLIS,
format
)
}
}
@RunWith(AndroidJUnit4::class)
class StringExtensionsTest {
// Aug 11 2021 19:28:53 UTC
private val now = 1628710133395
@Test
fun `given a date string in the past month, return relative time string to now`() {
val dateStr = "2021-07-26T20:17:00.115Z"
val result = dateStr.toRelativeTimeStr(now)
assertEquals("Jul 26", result?.toString())
}
@Test
fun `given a date string in the past 24+ hours, return relative time string to now`() {
val dateStr = "2021-08-10T09:09:00.115Z"
val result = dateStr.toRelativeTimeStr(now)
assertEquals("Yesterday", result?.toString())
}
@Test
fun `given a date string in the past 3 days, return relative time string to now`() {
val dateStr = "2021-08-08T20:17:00.115Z"
val result = dateStr.toRelativeTimeStr(now)
assertEquals("3 days ago", result?.toString())
}
@Test
fun `given a date string in the past 3 hours, return relative time string to now`() {
val dateStr = "2021-08-11T16:00:00.115Z"
val result = dateStr.toRelativeTimeStr(now)
assertEquals("3 hr. ago", result?.toString())
}
@Test
fun `given a date string in the past hour, return relative time string to now`() {
val dateStr = "2021-08-11T19:25:00.000Z"
val result = dateStr.toRelativeTimeStr(now)
assertEquals("3 min. ago", result?.toString())
}
@Test
fun `given a date string within a minute, return relative time string to now`() {
val dateStr = "2021-08-11T19:28:00.000Z"
val result = dateStr.toRelativeTimeStr(now)
assertEquals("Just Now", result?.toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment