mireadesktop/weekday.kt

53 lines
1.7 KiB
Kotlin
Raw Permalink Normal View History

2025-01-24 20:34:51 +03:00
import java.time.DayOfWeek
2025-01-27 01:59:45 +03:00
import java.time.LocalDate
2025-01-24 20:34:51 +03:00
import java.time.Month
enum class Period {
Autumn,
Winter,
Spring
}
2025-01-27 01:59:45 +03:00
fun determinePeriod(today: LocalDate): Period {
val februaryCutoff = LocalDate.of(today.year, Month.FEBRUARY, 9)
val septemberCutoff = LocalDate.of(today.year, Month.SEPTEMBER, 1)
2025-01-24 20:34:51 +03:00
return when {
2025-01-27 01:59:45 +03:00
today.isBefore(februaryCutoff) -> Period.Winter
today.isAfter(septemberCutoff.minusDays(1)) -> Period.Autumn
2025-01-24 20:34:51 +03:00
else -> Period.Spring
}
}
2025-01-27 01:59:45 +03:00
fun calculateWeek(period: Period, date: LocalDate): String {
return when {
period == Period.Winter -> "Хороших праздников, удачной сессии!"
date.dayOfWeek == DayOfWeek.SUNDAY -> "Сегодня воскресенье, лучше иди домой"
2025-01-24 20:34:51 +03:00
else -> {
2025-01-27 01:59:45 +03:00
val currentWeek = date.getWeekOfYear()
val periodLimit = when (period) {
Period.Spring -> LocalDate.of(date.year, Month.FEBRUARY, 9)
Period.Autumn -> LocalDate.of(date.year, Month.SEPTEMBER, 1)
else -> throw IllegalStateException("Unexpected period")
2025-01-24 20:34:51 +03:00
}
2025-01-27 01:59:45 +03:00
val limitWeek = periodLimit.getWeekOfYear()
val isLimitSunday = periodLimit.dayOfWeek == DayOfWeek.SUNDAY
val weekOffset = if (isLimitSunday) 1 else 0
val weekNumber = 1 + currentWeek - limitWeek - weekOffset
"$weekNumber неделя"
2025-01-24 20:34:51 +03:00
}
}
}
2025-01-27 01:59:45 +03:00
fun LocalDate.getWeekOfYear(): Int {
return this.get(java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR)
2025-01-24 20:34:51 +03:00
}
fun main() {
val today = LocalDate.now()
2025-01-27 01:59:45 +03:00
val period = determinePeriod(today)
val weekInfo = calculateWeek(period, today)
println(weekInfo)
2025-01-24 20:34:51 +03:00
}