weekday.kt rewritten and tested

This commit is contained in:
Gregory Bednov 2025-01-27 01:59:45 +03:00 committed by GitHub
commit d763718671
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,6 @@
import java.time.LocalDate
import java.time.DayOfWeek import java.time.DayOfWeek
import java.time.LocalDate
import java.time.Month import java.time.Month
import java.time.format.DateTimeFormatter
enum class Period { enum class Period {
Autumn, Autumn,
@ -9,49 +8,45 @@ enum class Period {
Spring Spring
} }
fun period(thisYear: (Month) -> LocalDate, today: LocalDate): Period { fun determinePeriod(today: LocalDate): Period {
val februaryCutoff = LocalDate.of(today.year, Month.FEBRUARY, 9)
val septemberCutoff = LocalDate.of(today.year, Month.SEPTEMBER, 1)
return when { return when {
today.isBefore(thisYear(Month.FEBRUARY).plusDays(9)) -> Period.Winter today.isBefore(februaryCutoff) -> Period.Winter
today.isAfter(thisYear(Month.SEPTEMBER).minusDays(1)) -> Period.Autumn today.isAfter(septemberCutoff.minusDays(1)) -> Period.Autumn
else -> Period.Spring else -> Period.Spring
} }
} }
fun week(p: Period, d: LocalDate): String { fun calculateWeek(period: Period, date: LocalDate): String {
return when (p) { return when {
Period.Winter -> "Хороших праздников, удачной сессии!" period == Period.Winter -> "Хороших праздников, удачной сессии!"
date.dayOfWeek == DayOfWeek.SUNDAY -> "Сегодня воскресенье, лучше иди домой"
else -> { else -> {
if (d.dayOfWeek == DayOfWeek.SUNDAY) { val currentWeek = date.getWeekOfYear()
"Сегодня воскресенье, лучше иди домой" val periodLimit = when (period) {
} else { Period.Spring -> LocalDate.of(date.year, Month.FEBRUARY, 9)
val (y, x0) = toWeekDate(d) Period.Autumn -> LocalDate.of(date.year, Month.SEPTEMBER, 1)
val limit = if (p == Period.Spring) thisYear(Month.FEBRUARY).plusDays(9) else thisYear(Month.SEPTEMBER).plusDays(1) else -> throw IllegalStateException("Unexpected period")
val (_, x1) = toWeekDate(limit)
val limitIsSunday = limit.dayOfWeek == DayOfWeek.SUNDAY
val x = 1 + x0 - x1 - if (limitIsSunday) 1 else 0
"$x неделя"
} }
val limitWeek = periodLimit.getWeekOfYear()
val isLimitSunday = periodLimit.dayOfWeek == DayOfWeek.SUNDAY
val weekOffset = if (isLimitSunday) 1 else 0
val weekNumber = 1 + currentWeek - limitWeek - weekOffset
"$weekNumber неделя"
} }
} }
} }
fun toWeekDate(date: LocalDate): Pair<Int, Int> { fun LocalDate.getWeekOfYear(): Int {
val weekOfYear = date.get(java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR) return this.get(java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR)
val year = date.year
return Pair(year, weekOfYear.toInt())
}
fun thisYear(month: Month): LocalDate {
val now = LocalDate.now()
return LocalDate.of(now.year, month, 1)
} }
fun main() { fun main() {
val today = LocalDate.now() val today = LocalDate.now()
val (year, _, _) = today.toGregorian() val period = determinePeriod(today)
println(week(period(::thisYear, today), today)) val weekInfo = calculateWeek(period, today)
} println(weekInfo)
fun LocalDate.toGregorian(): Triple<Int, Int, Int> {
return Triple(this.year, this.monthValue, this.dayOfMonth)
} }