diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/gift/GiftDateTime.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/gift/GiftDateTime.kt new file mode 100644 index 00000000..439c24dc --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/gift/GiftDateTime.kt @@ -0,0 +1,44 @@ +package kr.co.vividnext.sodalive.v2.gift + +import java.text.ParsePosition +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import java.util.TimeZone + +internal object GiftDateTime { + private const val DAY_MILLIS = 86_400_000L + private val utcPattern = Regex( + "([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(?:\\.([0-9]{1,9}))?(?:Z|\\+00:00)" + ) + + fun formatDate(value: String?, pattern: String): String? = + parseUtc(value)?.let { SimpleDateFormat(pattern, Locale.US).format(Date(it.timeMillis)) } + + fun remainingDays(deadlineAt: String?, nowMillis: Long): Int? { + val deadline = parseUtc(deadlineAt) ?: return null + val remainingMillis = deadline.timeMillis - nowMillis + if (remainingMillis < 0 || remainingMillis == 0L && !deadline.hasSubMillisecondRemainder) return null + val days = remainingMillis / DAY_MILLIS + + if (remainingMillis % DAY_MILLIS == 0L && !deadline.hasSubMillisecondRemainder) 0 else 1 + return days.toInt() + } + + private fun parseUtc(value: String?): ParsedUtc? { + val match = value?.let(utcPattern::matchEntire) ?: return null + val seconds = match.groupValues[1] + val fraction = match.groupValues[2] + val milliseconds = fraction.take(3).padEnd(3, '0').toLong() + return SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).apply { + isLenient = false + timeZone = TimeZone.getTimeZone("UTC") + }.parse(seconds, ParsePosition(0))?.let { + ParsedUtc(it.time + milliseconds, fraction.drop(3).any { digit -> digit != '0' }) + } + } + + private data class ParsedUtc( + val timeMillis: Long, + val hasSubMillisecondRemainder: Boolean + ) +} diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/gift/GiftDetailPresenter.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/gift/GiftDetailPresenter.kt index c6ab2ead..9d7ce135 100644 --- a/app/src/main/java/kr/co/vividnext/sodalive/v2/gift/GiftDetailPresenter.kt +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/gift/GiftDetailPresenter.kt @@ -6,10 +6,6 @@ import kr.co.vividnext.sodalive.v2.gift.GiftDetailText.Raw import kr.co.vividnext.sodalive.v2.gift.data.GiftAddressResponse import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse -import java.text.ParsePosition -import java.text.SimpleDateFormat -import java.util.Locale -import java.util.TimeZone object GiftDetailPresenter { private val normalStates = linkedMapOf( @@ -25,8 +21,6 @@ object GiftDetailPresenter { "ARRIVED_AT_MAILBOX", "INSPECTION_COMPLETED" ) - private val utcPattern = Regex("([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(?:\\.[0-9]{1,9})?(?:Z|\\+00:00)") - fun present(detail: GiftDetailResponse, cancel: GiftCancelResponse? = null): GiftDetailUiModel { if (detail.direction == "RECEIVED") return presentReceived(detail) @@ -214,19 +208,9 @@ object GiftDetailPresenter { return model.cancellation == null && GiftDetailAction.TRACKING in model.actions } - fun formatProgressDate(occurredAt: String?): String? = formatDate(occurredAt, "MM.dd") + fun formatProgressDate(occurredAt: String?): String? = GiftDateTime.formatDate(occurredAt, "MM.dd") - fun formatCardDate(occurredAt: String?): String? = formatDate(occurredAt, "yyyy.MM.dd") - - private fun formatDate(occurredAt: String?, pattern: String): String? { - val seconds = occurredAt?.let { utcPattern.matchEntire(it)?.groupValues?.get(1) } ?: return null - val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).apply { - isLenient = false - timeZone = TimeZone.getTimeZone("UTC") - } - val date = parser.parse(seconds, ParsePosition(0)) ?: return null - return SimpleDateFormat(pattern, Locale.US).format(date) - } + fun formatCardDate(occurredAt: String?): String? = GiftDateTime.formatDate(occurredAt, "yyyy.MM.dd") private fun GiftAddressResponse.toUiModel() = GiftDetailAddress(name, phoneNumber, address) diff --git a/app/src/test/java/kr/co/vividnext/sodalive/v2/gift/GiftDateTimeTest.kt b/app/src/test/java/kr/co/vividnext/sodalive/v2/gift/GiftDateTimeTest.kt new file mode 100644 index 00000000..e40eb258 --- /dev/null +++ b/app/src/test/java/kr/co/vividnext/sodalive/v2/gift/GiftDateTimeTest.kt @@ -0,0 +1,87 @@ +package kr.co.vividnext.sodalive.v2.gift + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test +import java.util.GregorianCalendar +import java.util.TimeZone + +class GiftDateTimeTest { + @Test + fun `UTC 날짜 경계는 기기 시간대 날짜로 표시한다`() { + val previousTimeZone = TimeZone.getDefault() + try { + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul")) + + assertEquals( + "2026.10.12", + GiftDateTime.formatDate("2026-10-11T23:30:00Z", "yyyy.MM.dd") + ) + } finally { + TimeZone.setDefault(previousTimeZone) + } + } + + @Test + fun `엄격한 UTC 형식만 기기 시간대로 표시한다`() { + val previousTimeZone = TimeZone.getDefault() + try { + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul")) + + listOf( + "2026-10-01T18:30:00Z", + "2026-10-01T18:30:00.123456Z", + "2026-10-01T18:30:00+00:00" + ).forEach { value -> + assertEquals("2026.10.02", GiftDateTime.formatDate(value, "yyyy.MM.dd")) + } + listOf( + null, + "", + "bad", + "2026-02-30T00:00:00Z", + "2026-10-01", + "2026-10-01T25:00:00Z", + "2026-10-01T18:30:00Z junk" + ).forEach { value -> + assertNull(GiftDateTime.formatDate(value, "yyyy.MM.dd")) + } + } finally { + TimeZone.setDefault(previousTimeZone) + } + } + + @Test + fun `마감일 남은 시간은 24시간 단위로 올림한다`() { + val nowMillis = utcMillis(2026, 10, 1, 0, 0, 0) + + assertEquals(1, GiftDateTime.remainingDays("2026-10-02T00:00:00Z", nowMillis)) + assertEquals(1, GiftDateTime.remainingDays("2026-10-01T23:59:59Z", nowMillis)) + assertEquals(2, GiftDateTime.remainingDays("2026-10-02T00:00:01Z", nowMillis)) + assertEquals(2, GiftDateTime.remainingDays("2026-10-02T00:00:00.500Z", nowMillis)) + assertEquals(1, GiftDateTime.remainingDays("2026-10-01T00:00:00.500Z", nowMillis)) + assertEquals(1, GiftDateTime.remainingDays("2026-10-01T00:00:00.0001Z", nowMillis)) + assertEquals(2, GiftDateTime.remainingDays("2026-10-02T00:00:00.0001Z", nowMillis)) + } + + @Test + fun `없는 잘못된 또는 지난 마감일은 남은 일수를 만들지 않는다`() { + val nowMillis = utcMillis(2026, 10, 1, 0, 0, 0) + + listOf(null, "bad", "2026-10-01T00:00:00Z", "2026-09-30T23:59:59Z").forEach { value -> + assertNull(GiftDateTime.remainingDays(value, nowMillis)) + } + } + + private fun utcMillis( + year: Int, + month: Int, + dayOfMonth: Int, + hourOfDay: Int, + minute: Int, + second: Int + ): Long = GregorianCalendar(TimeZone.getTimeZone("UTC")).apply { + clear() + set(year, month - 1, dayOfMonth, hourOfDay, minute, second) + }.timeInMillis +}