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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*
* Copyright (C) 2023 NotEnoughUpdates contributors
*
* This file is part of NotEnoughUpdates.
*
* NotEnoughUpdates is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* NotEnoughUpdates is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github.moulberry.notenoughupdates.util
import java.time.Instant
data class SkyBlockTime(
val year: Int = 1,
val month: Int = 1,
val day: Int = 1,
val hour: Int = 0,
val minute: Int = 0,
val second: Int = 0,
) {
val monthName get() = monthName(month)
val dayName get() = "$day${daySuffix(day)}"
fun toInstant(): Instant? {
return Instant.ofEpochMilli(toMillis())
}
fun toMillis(): Long {
val skyBlockYear = 124 * 60 * 60.0
val skyBlockMonth = skyBlockYear / 12
val skyBlockDay = skyBlockMonth / 31
val skyBlockHour = skyBlockDay / 24
val skyBlockMinute = skyBlockHour / 60
val skyBlockSecond = skyBlockMinute / 60
var time = 0.0
time += year * skyBlockYear
time += (month - 1) * skyBlockMonth
time += (day - 1) * skyBlockDay
time += hour * skyBlockHour
time += minute * skyBlockMinute
time += second * skyBlockSecond
time += 1559829300
return time.toLong() * 1000
}
companion object {
fun fromInstant(instant: Instant): SkyBlockTime {
val skyBlockTimeZero = 1559829300000 // Day 1, Year 1
var realMillis = (instant.toEpochMilli() - skyBlockTimeZero)
val skyBlockYear = 124 * 60 * 60 * 1000
val skyBlockMonth = skyBlockYear / 12
val skyBlockDay = skyBlockMonth / 31
val skyBlockHour = skyBlockDay / 24
val skyBlockMinute = skyBlockHour / 60
val skyBlockSecond = skyBlockMinute / 60
fun getUnit(factor: Int): Int {
val result = realMillis / factor
realMillis %= factor
return result.toInt()
}
val year = getUnit(skyBlockYear)
val month = getUnit(skyBlockMonth) + 1
val day = getUnit(skyBlockDay) + 1
val hour = getUnit(skyBlockHour)
val minute = getUnit(skyBlockMinute)
val second = getUnit(skyBlockSecond)
return SkyBlockTime(year, month, day, hour, minute, second)
}
@JvmStatic
fun now(): SkyBlockTime {
return fromInstant(Instant.now())
}
fun monthName(month: Int): String {
val prefix = when ((month - 1) % 3) {
0 -> "Early "
1 -> ""
2 -> "Late "
else -> "Undefined!"
}
val name = when ((month - 1) / 3) {
0 -> "Spring"
1 -> "Summer"
2 -> "Autumn"
3 -> "Winter"
else -> "lol"
}
return prefix + name
}
fun monthNameToInt(month: String): Int? {
val superSeason = when {
month.endsWith("Spring") -> 2
month.endsWith("Summer") -> 5
month.endsWith("Autumn") -> 8
month.endsWith("Winter") -> 11
else -> return null
}
val subSeason = when {
month.startsWith("Early") -> -1
month.startsWith("Late") -> 1
else -> 0
}
return superSeason + subSeason
}
fun fromDayMonthYear(day: Int, month: String, year: Int): SkyBlockTime? {
return monthNameToInt(month)?.let {
SkyBlockTime(year = year, month = it, day = day)
}
}
fun daySuffix(n: Int): String {
return if (n in 11..13) {
"th"
} else when (n % 10) {
1 -> "st"
2 -> "nd"
3 -> "rd"
else -> "th"
}
}
}
}
|