fix(common): UTC 상대시간 파싱을 보완한다
This commit is contained in:
@@ -2,7 +2,7 @@ package kr.co.vividnext.sodalive.common
|
||||
|
||||
import android.content.Context
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import java.text.ParseException
|
||||
import java.text.ParsePosition
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
@@ -10,8 +10,9 @@ import java.util.Locale
|
||||
import java.util.TimeZone
|
||||
|
||||
fun formatUtcRelativeTimeText(context: Context, utcText: String?): String {
|
||||
val pastMillis = parseServerUtcToMillis(utcText)
|
||||
val parsedTime = parseServerUtcToMillis(utcText)
|
||||
?: return context.getString(R.string.character_comment_time_just_now)
|
||||
val pastMillis = parsedTime.millis
|
||||
|
||||
val nowMillis = System.currentTimeMillis()
|
||||
var diff = nowMillis - pastMillis
|
||||
@@ -21,6 +22,10 @@ fun formatUtcRelativeTimeText(context: Context, utcText: String?): String {
|
||||
val hour = 60 * minute
|
||||
val day = 24 * hour
|
||||
|
||||
if (parsedTime.isIsoText && diff < RECENT_ISO_TIME_GRACE_MILLIS) {
|
||||
return context.getString(R.string.character_comment_time_just_now)
|
||||
}
|
||||
|
||||
if (diff < minute) {
|
||||
return context.getString(R.string.character_comment_time_just_now)
|
||||
}
|
||||
@@ -75,19 +80,25 @@ class AndroidUtcRelativeTimeTextFormatter(context: Context) : UtcRelativeTimeTex
|
||||
override fun format(utcText: String?): String = formatUtcRelativeTimeText(applicationContext, utcText)
|
||||
}
|
||||
|
||||
private fun parseServerUtcToMillis(utcText: String?): Long? {
|
||||
private fun parseServerUtcToMillis(utcText: String?): ParsedServerTime? {
|
||||
if (utcText.isNullOrBlank()) return null
|
||||
|
||||
val value = utcText.trim()
|
||||
val value = utcText.trim().normalizeIsoFraction()
|
||||
if (value.all { it.isDigit() }) {
|
||||
return try {
|
||||
value.toLong()
|
||||
val number = value.toLong()
|
||||
ParsedServerTime(
|
||||
millis = if (value.length <= EPOCH_SECONDS_MAX_LENGTH) number * 1000 else number,
|
||||
isIsoText = false
|
||||
)
|
||||
} catch (_: NumberFormatException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
val patterns = listOf(
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
|
||||
"yyyy-MM-dd'T'HH:mm:ssXXX",
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
|
||||
"yyyy-MM-dd'T'HH:mm:ss'Z'",
|
||||
"yyyy-MM-dd'T'HH:mm:ss",
|
||||
@@ -99,11 +110,26 @@ private fun parseServerUtcToMillis(utcText: String?): Long? {
|
||||
try {
|
||||
val dateFormat = SimpleDateFormat(pattern, Locale.US)
|
||||
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
|
||||
val parsed: Date? = dateFormat.parse(value)
|
||||
if (parsed != null) return parsed.time
|
||||
} catch (_: ParseException) {
|
||||
val position = ParsePosition(0)
|
||||
val parsed: Date? = dateFormat.parse(value, position)
|
||||
if (parsed != null && position.index == value.length) {
|
||||
return ParsedServerTime(millis = parsed.time, isIsoText = true)
|
||||
}
|
||||
} catch (_: RuntimeException) {
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun String.normalizeIsoFraction(): String {
|
||||
return replace(Regex("""\.(\d{3})\d+"""), ".$1")
|
||||
}
|
||||
|
||||
private data class ParsedServerTime(
|
||||
val millis: Long,
|
||||
val isIsoText: Boolean
|
||||
)
|
||||
|
||||
private const val EPOCH_SECONDS_MAX_LENGTH = 10
|
||||
private const val RECENT_ISO_TIME_GRACE_MILLIS = 10 * 60_000L
|
||||
|
||||
Reference in New Issue
Block a user