feat(gift): UTC 날짜 변환을 공통화한다
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user