From b8684e8920363c54758651dd0a8dbd3f55884c2e Mon Sep 17 00:00:00 2001 From: Gregory Bednov Date: Fri, 24 Jan 2025 20:34:51 +0300 Subject: [PATCH] Create weekday.kt - port from haskell --- weekday.kt | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 weekday.kt diff --git a/weekday.kt b/weekday.kt new file mode 100644 index 0000000..f7d3319 --- /dev/null +++ b/weekday.kt @@ -0,0 +1,57 @@ +import java.time.LocalDate +import java.time.DayOfWeek +import java.time.Month +import java.time.format.DateTimeFormatter + +enum class Period { + Autumn, + Winter, + Spring +} + +fun period(thisYear: (Month) -> LocalDate, today: LocalDate): Period { + return when { + today.isBefore(thisYear(Month.FEBRUARY).plusDays(9)) -> Period.Winter + today.isAfter(thisYear(Month.SEPTEMBER).minusDays(1)) -> Period.Autumn + else -> Period.Spring + } +} + +fun week(p: Period, d: LocalDate): String { + return when (p) { + Period.Winter -> "Хороших праздников, удачной сессии!" + else -> { + if (d.dayOfWeek == DayOfWeek.SUNDAY) { + "Сегодня воскресенье, лучше иди домой" + } else { + val (y, x0) = toWeekDate(d) + val limit = if (p == Period.Spring) thisYear(Month.FEBRUARY).plusDays(9) else thisYear(Month.SEPTEMBER).plusDays(1) + val (_, x1) = toWeekDate(limit) + val limitIsSunday = limit.dayOfWeek == DayOfWeek.SUNDAY + val x = 1 + x0 - x1 - if (limitIsSunday) 1 else 0 + "$x неделя" + } + } + } +} + +fun toWeekDate(date: LocalDate): Pair { + val weekOfYear = date.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() { + val today = LocalDate.now() + val (year, _, _) = today.toGregorian() + println(week(period(::thisYear, today), today)) +} + +fun LocalDate.toGregorian(): Triple { + return Triple(this.year, this.monthValue, this.dayOfMonth) +}