11package com.idiotfrogs.extension
22
3+ import kotlinx.datetime.DayOfWeek
34import kotlinx.datetime.LocalDateTime
45import kotlinx.datetime.TimeZone
56import kotlinx.datetime.number
67import kotlinx.datetime.todayIn
78import kotlin.time.Clock
89import kotlin.time.ExperimentalTime
910
10- fun LocalDateTime.toYearMonthDay (): String =
11- " ${year} . ${month.number.toString().padStart(2 , ' 0' )} . ${day.toString().padStart(2 , ' 0' )} "
11+ fun LocalDateTime?.toYearMonthDay (): String =
12+ " ${this ?.year} . ${this ?.month?.number.toString().padStart(2 , ' 0' )} . ${this ?.day.toString().padStart(2 , ' 0' )} "
13+
14+ fun LocalDateTime?.toYearMonthDayWeek (): String =
15+ " ${this ?.year} . ${this ?.month?.number.toString().padStart(2 , ' 0' )} . ${this ?.day.toString().padStart(2 , ' 0' )} . ${this ?.dayOfWeek?.toKoreanShort()} "
1216
1317@OptIn(ExperimentalTime ::class )
14- fun LocalDateTime.toDday (): String {
18+ fun LocalDateTime? .toDday (): String {
1519 val today = Clock .System .todayIn(TimeZone .currentSystemDefault())
16- val targetDate = this .date
17- val diff = targetDate.toEpochDays() - today.toEpochDays()
20+ val targetDate = this ?.date
21+ val diff = targetDate?.toEpochDays()?.minus(today.toEpochDays())
22+
23+ return " D-$diff "
24+ }
1825
19- return if (diff == 0L ) " D-day" else " D-$diff "
26+ private fun DayOfWeek.toKoreanShort (): String = when (this ) {
27+ DayOfWeek .MONDAY -> " (μ)"
28+ DayOfWeek .TUESDAY -> " (ν)"
29+ DayOfWeek .WEDNESDAY -> " (μ)"
30+ DayOfWeek .THURSDAY -> " (λͺ©)"
31+ DayOfWeek .FRIDAY -> " (κΈ)"
32+ DayOfWeek .SATURDAY -> " (ν )"
33+ DayOfWeek .SUNDAY -> " (μΌ)"
2034}
0 commit comments