DateUtils: move event details formatting to DateUtils

Also do not display seconds, because it is not
meaningful and is not selected in date picker.
This commit is contained in:
2025-09-29 03:29:50 +02:00
parent 0633b4d084
commit 7fdd4afa06
2 changed files with 28 additions and 9 deletions

View File

@@ -1,12 +1,17 @@
package utils
import android.content.Context
import android.os.Build
import android.text.format.DateFormat
import it.danieleverducci.lunatracker.R
import java.util.Date
class DateUtils {
companion object {
/**
* Format time duration in seconds as e.g. "2 hours, 1 min".
* Used for the duration to the next/previous event in the event details dialog.
*/
fun formatTimeDuration(context: Context, secondsDiff: Long): String {
var seconds = secondsDiff
@@ -65,7 +70,8 @@ class DateUtils {
}
/**
* Formats the provided unix timestamp in a string like "3 hours, 26 minutes ago)
* Formats the provided unix timestamp in a string like "3 hours, 26 minutes ago".
* Used for the event list.
*/
fun formatTimeAgo(context: Context, unixTime: Long): String {
val secondsDiff = (System.currentTimeMillis() / 1000) - unixTime
@@ -100,5 +106,21 @@ class DateUtils {
}
return formattedTime.toString()
}
/**
* Format time as localized string without seconds. E.g. "Sept 18, 2025, 03:36 PM".
* Used in the event detail dialog.
*/
fun formatDateTime(unixTime: Long): String {
val date = Date(unixTime * 1000)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val dateFormat = android.icu.text.DateFormat.getDateTimeInstance(android.icu.text.DateFormat.RELATIVE_SHORT, android.icu.text.DateFormat.SHORT)
return dateFormat.format(date)
} else {
// fallback
val dateFormat = java.text.DateFormat.getDateTimeInstance()
return dateFormat.format(date)
}
}
}
}
}