-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweekday.kt
52 lines (45 loc) · 1.71 KB
/
weekday.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.Month
enum class Period {
Autumn,
Winter,
Spring
}
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 {
today.isBefore(februaryCutoff) -> Period.Winter
today.isAfter(septemberCutoff.minusDays(1)) -> Period.Autumn
else -> Period.Spring
}
}
fun calculateWeek(period: Period, date: LocalDate): String {
return when {
period == Period.Winter -> "Хороших праздников, удачной сессии!"
date.dayOfWeek == DayOfWeek.SUNDAY -> "Сегодня воскресенье, лучше иди домой"
else -> {
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")
}
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 LocalDate.getWeekOfYear(): Int {
return this.get(java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR)
}
fun main() {
val today = LocalDate.now()
val period = determinePeriod(today)
val weekInfo = calculateWeek(period, today)
println(weekInfo)
}