Skip to content

Commit 1beb5ea

Browse files
committed
Implement DateTimeUnit.toString and add tests
1 parent 545f8a4 commit 1beb5ea

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

core/commonMain/src/DateTimeUnit.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import kotlin.time.Duration
99
import kotlin.time.ExperimentalTime
1010
import kotlin.time.nanoseconds
1111

12-
// TODO: toString
1312
sealed class DateTimeUnit {
1413

1514
abstract operator fun times(scalar: Int): DateTimeUnit
@@ -60,6 +59,8 @@ sealed class DateTimeUnit {
6059
this === other || (other is TimeBased && this.nanoseconds == other.nanoseconds)
6160

6261
override fun hashCode(): Int = nanoseconds.toInt() xor (nanoseconds shr Int.SIZE_BITS).toInt()
62+
63+
override fun toString(): String = formatToString(calendarScale, calendarUnit.toString())
6364
}
6465

6566
sealed class DateBased : DateTimeUnit() {
@@ -78,6 +79,11 @@ sealed class DateTimeUnit {
7879
this === other || (other is DayBased && this.days == other.days)
7980

8081
override fun hashCode(): Int = days xor 0x10000
82+
83+
override fun toString(): String = if (days % 7 == 0)
84+
formatToString(days / 7, "WEEK")
85+
else
86+
formatToString(days, "DAY")
8187
}
8288
class MonthBased(val months: Int) : DateBased() {
8389
init {
@@ -93,9 +99,18 @@ sealed class DateTimeUnit {
9399
this === other || (other is MonthBased && this.months == other.months)
94100

95101
override fun hashCode(): Int = months xor 0x20000
102+
103+
override fun toString(): String = when {
104+
months % 12_00 == 0 -> formatToString(months / 12_00, "CENTURY")
105+
months % 12 == 0 -> formatToString(months / 12, "YEAR")
106+
months % 3 == 0 -> formatToString(months / 3, "QUARTER")
107+
else -> formatToString(months, "MONTH")
108+
}
96109
}
97110
}
98111

112+
protected fun formatToString(value: Int, unit: String): String = if (value == 1) unit else "$value-$unit"
113+
protected fun formatToString(value: Long, unit: String): String = if (value == 1L) unit else "$value-$unit"
99114

100115
companion object {
101116
val NANOSECOND = TimeBased(nanoseconds = 1)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019-2020 JetBrains s.r.o.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test
7+
8+
import kotlinx.datetime.*
9+
import kotlin.test.*
10+
11+
class DateTimeUnitTest {
12+
val U = DateTimeUnit // alias
13+
14+
@Test
15+
fun baseUnits() {
16+
val baseUnitProps = listOf(
17+
U::NANOSECOND, U::MICROSECOND, U::MILLISECOND, U::SECOND, U::MINUTE, U::HOUR,
18+
U::DAY, U::WEEK, U::MONTH, U::QUARTER, U::YEAR, U::CENTURY
19+
)
20+
for (unit in baseUnitProps) {
21+
assertEquals(unit.name, unit.get().toString())
22+
}
23+
24+
val allUnits = baseUnitProps.map { it.get() }
25+
26+
assertEquals(allUnits.size, allUnits.map { it.hashCode() }.distinct().size)
27+
28+
for (unit in allUnits) {
29+
assertSame(unit, allUnits.singleOrNull { it == unit }) // should be no not same, but equal
30+
}
31+
}
32+
33+
@Test
34+
fun productUnits() {
35+
ensureEquality(U.MICROSECOND, U.NANOSECOND * 1000, "MICROSECOND")
36+
ensureEquality(U.MICROSECOND * 2000, U.NANOSECOND * 2000_000, "2-MILLISECOND")
37+
38+
val twoDays = U.DAY * 2
39+
assertEquals("2-DAY", twoDays.toString())
40+
41+
val twoWeeks = U.WEEK * 2
42+
assertEquals("2-WEEK", twoWeeks.toString())
43+
assertNotEquals(twoDays, twoWeeks)
44+
45+
val fortnight = U.DAY * 14
46+
ensureEquality(twoWeeks, fortnight, "2-WEEK")
47+
48+
val fourQuarters = U.QUARTER * 4
49+
ensureEquality(U.YEAR, fourQuarters, "YEAR")
50+
51+
val twoFourMonths = U.MONTH * 24
52+
val twoYears = U.YEAR * 2
53+
ensureEquality(twoYears, twoFourMonths, "2-YEAR")
54+
}
55+
56+
private fun ensureEquality(v1: Any, v2: Any, expectToString: String) {
57+
assertEquals(v1, v2)
58+
assertEquals(v1.hashCode(), v2.hashCode())
59+
assertEquals(v1.toString(), v2.toString())
60+
assertEquals(expectToString, v2.toString())
61+
}
62+
63+
}

0 commit comments

Comments
 (0)