-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
android/app/src/main/java/com/emergetools/hackernews/data/Dates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.emergetools.hackernews.data | ||
|
||
import java.time.Instant | ||
|
||
/** | ||
* Convert the difference between two epoch second times | ||
* to a relative timestamp label like "2m ago" or "5d ago" | ||
*/ | ||
fun relativeTimeStamp(epochSeconds: Long): String { | ||
val now = Instant.now().epochSecond | ||
val difference = now - epochSeconds | ||
|
||
val minutes = difference / SECONDS_IN_MINUTE | ||
if (minutes < 60) { | ||
return "${minutes.toInt()}m ago" | ||
} | ||
|
||
val hours = minutes / MINUTES_IN_HOUR | ||
if (hours < 24) { | ||
return "${hours.toInt()}h ago" | ||
} | ||
|
||
val days = hours / HOURS_IN_DAY | ||
return "${days.toInt()}d ago" | ||
} | ||
|
||
private const val SECONDS_IN_MINUTE = 60.0 | ||
private const val MINUTES_IN_HOUR = 60.0 | ||
private const val HOURS_IN_DAY = 24.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters