refactor(datetime): Standardize date/time formatting with DateUtils (#4164)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2026-01-08 12:43:50 -06:00 committed by GitHub
parent 1422217303
commit 02cf1f1034
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 131 additions and 136 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2025 Meshtastic LLC
* Copyright (c) 2025-2026 Meshtastic LLC
*
* 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
@ -14,7 +14,6 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.core.model.util
import android.widget.EditText
@ -69,15 +68,14 @@ fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
@Suppress("MagicNumber")
fun formatAgo(lastSeenUnix: Int, currentTimeMillis: Long = System.currentTimeMillis()): String {
val currentTime = (currentTimeMillis / 1000).toInt()
val diffMin = (currentTime - lastSeenUnix) / 60
return when {
diffMin < 1 -> "now"
diffMin < 60 -> diffMin.toString() + " min"
diffMin < 2880 -> (diffMin / 60).toString() + " h"
diffMin < 1440000 -> (diffMin / (60 * 24)).toString() + " d"
else -> "?"
}
val timeInMillis = lastSeenUnix * 1000L
return android.text.format.DateUtils.getRelativeTimeSpanString(
timeInMillis,
currentTimeMillis,
android.text.format.DateUtils.MINUTE_IN_MILLIS,
android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE,
)
.toString()
}
private const val MPS_TO_KMPH = 3.6f

View file

@ -251,6 +251,10 @@
<string name="debug_filter_add">Add filter</string>
<string name="debug_filter_included">Filter included</string>
<string name="debug_filter_clear">Clear all filters</string>
<string name="debug_filter_add_custom">Add custom filter</string>
<string name="debug_filter_preset_title">Preset Filters</string>
<string name="debug_store_logs_title">Store mesh logs</string>
<string name="debug_store_logs_summary">Disable to skip writing mesh logs to disk</string>
<string name="debug_clear">Clear Logs</string>
<string name="match_any">Match Any | All</string>
<string name="match_all">Match All | Any</string>
@ -1082,4 +1086,5 @@
<string name="compass_uncertainty">Estimated area: \u00b1%1$s (\u00b1%2$s)</string>
<string name="compass_uncertainty_unknown">Estimated area: unknown accuracy</string>
<string name="mark_as_read">Mark as read</string>
<string name="now">now</string>
</resources>

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2025-2026 Meshtastic LLC
*
* 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.core.ui.util
import android.text.format.DateUtils
import com.meshtastic.core.strings.getString
import org.meshtastic.core.strings.Res
import org.meshtastic.core.strings.now
@Suppress("MagicNumber")
fun formatAgo(lastSeenUnix: Int, currentTimeMillis: Long = System.currentTimeMillis()): String {
val timeInMillis = lastSeenUnix * 1000L
val diff = currentTimeMillis - timeInMillis
return if (diff < 60_000L) {
getString(Res.string.now)
} else {
DateUtils.getRelativeTimeSpanString(
timeInMillis,
currentTimeMillis,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE,
)
.toString()
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2025 Meshtastic LLC
* Copyright (c) 2025-2026 Meshtastic LLC
*
* 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
@ -14,10 +14,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.core.ui.util
import android.text.format.DateUtils
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import org.jetbrains.compose.resources.stringResource
import org.meshtastic.core.strings.Res
import org.meshtastic.core.strings.unknown_age
@ -28,13 +29,12 @@ import org.meshtastic.proto.MeshProtos.MeshPacket
import org.meshtastic.proto.MeshProtos.Position
import org.meshtastic.proto.channel
import org.meshtastic.proto.channelSettings
import java.text.DateFormat
import kotlin.time.Duration.Companion.days
private const val SECONDS_TO_MILLIS = 1000L
@Composable
fun MeshProtos.Position.formatPositionTime(dateFormat: DateFormat): String {
fun MeshProtos.Position.formatPositionTime(): String {
val currentTime = System.currentTimeMillis()
val sixMonthsAgo = currentTime - 180.days.inWholeMilliseconds
val isOlderThanSixMonths = time * SECONDS_TO_MILLIS < sixMonthsAgo
@ -42,7 +42,11 @@ fun MeshProtos.Position.formatPositionTime(dateFormat: DateFormat): String {
if (isOlderThanSixMonths) {
stringResource(Res.string.unknown_age)
} else {
dateFormat.format(time * SECONDS_TO_MILLIS)
DateUtils.formatDateTime(
LocalContext.current,
time * SECONDS_TO_MILLIS,
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_TIME or DateUtils.FORMAT_ABBREV_ALL,
)
}
return timeText
}