Skip to content

Commit 7da608e

Browse files
committed
LocalTime#toSecondOfDay & LocalTime#ofSecondOfDay.
1 parent 4e39fbc commit 7da608e

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

core/common/src/LocalTime.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public expect class LocalTime : Comparable<LocalTime> {
4040
*/
4141
public fun parse(isoString: String): LocalTime
4242

43+
/**
44+
* Returns a LocalTime with the specified [secondOfDay]. The nanosecond field will be set to zero.
45+
*/
46+
public fun ofSecondOfDay(secondOfDay: Int): LocalTime
47+
4348
internal val MIN: LocalTime
4449
internal val MAX: LocalTime
4550
}
@@ -84,6 +89,9 @@ public expect class LocalTime : Comparable<LocalTime> {
8489
* @see LocalDateTime.parse
8590
*/
8691
public override fun toString(): String
92+
93+
/** Returns the time as seconds of day, from 0 to 24 * 60 * 60 - 1. */
94+
public fun toSecondOfDay(): Int
8795
}
8896

8997
/**
@@ -110,4 +118,4 @@ public fun LocalTime.atDate(year: Int, month: Month, dayOfMonth: Int = 0): Local
110118
/**
111119
* Combines this time's components with the specified [LocalDate] components into a [LocalDateTime] value.
112120
*/
113-
public fun LocalTime.atDate(date: LocalDate): LocalDateTime = LocalDateTime(date, this)
121+
public fun LocalTime.atDate(date: LocalDate): LocalDateTime = LocalDateTime(date, this)

core/common/test/LocalTimeTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,29 @@ class LocalTimeTest {
8585
assertFailsWith<IllegalArgumentException> { LocalTime(0, 0, 0, 1_000_000_000) }
8686
}
8787

88+
@Test
89+
fun toSecondOfDay() {
90+
val data = mapOf(
91+
0 to LocalTime(0, 0),
92+
5 to LocalTime(0, 0, 5),
93+
44105 to LocalTime(12, 15, 5),
94+
86399 to LocalTime(23, 59, 59),
95+
)
96+
97+
data.forEach { (secondOfDay, localTime) ->
98+
assertEquals(secondOfDay, localTime.toSecondOfDay())
99+
assertEquals(localTime, LocalTime.ofSecondOfDay(secondOfDay))
100+
}
101+
}
102+
103+
@Test
104+
fun toSecondOfDayIgnoredNanosecond() {
105+
assertEquals(
106+
0,
107+
LocalTime(0, 0, 0, 100).toSecondOfDay(),
108+
)
109+
}
110+
88111
@Test
89112
fun atDate() {
90113
val time = LocalTime(12, 1, 59)

core/js/src/LocalTime.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public actual class LocalTime internal constructor(internal val value: jtLocalTi
3434

3535
actual override fun toString(): String = value.toString()
3636

37+
public actual fun toSecondOfDay(): Int = value.toSecondOfDay().toInt()
38+
3739
actual override fun compareTo(other: LocalTime): Int = this.value.compareTo(other.value).toInt()
3840

3941
public actual companion object {
@@ -44,7 +46,9 @@ public actual class LocalTime internal constructor(internal val value: jtLocalTi
4446
throw e
4547
}
4648

49+
public actual fun ofSecondOfDay(secondOfDay: Int): LocalTime = jtLocalTime.ofSecondOfDay(secondOfDay, 0).let(::LocalTime)
50+
4751
internal actual val MIN: LocalTime = LocalTime(jtLocalTime.MIN)
4852
internal actual val MAX: LocalTime = LocalTime(jtLocalTime.MAX)
4953
}
50-
}
54+
}

core/jvm/src/LocalTime.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ public actual class LocalTime internal constructor(internal val value: jtLocalTi
3939

4040
actual override fun compareTo(other: LocalTime): Int = this.value.compareTo(other.value)
4141

42+
public actual fun toSecondOfDay(): Int = value.toSecondOfDay()
43+
4244
public actual companion object {
4345
public actual fun parse(isoString: String): LocalTime = try {
4446
jtLocalTime.parse(isoString).let(::LocalTime)
4547
} catch (e: DateTimeParseException) {
4648
throw DateTimeFormatException(e)
4749
}
4850

51+
public actual fun ofSecondOfDay(secondOfDay: Int): LocalTime = jtLocalTime.ofSecondOfDay(secondOfDay.toLong()).let(::LocalTime)
52+
4953
internal actual val MIN: LocalTime = LocalTime(jtLocalTime.MIN)
5054
internal actual val MAX: LocalTime = LocalTime(jtLocalTime.MAX)
5155
}
52-
}
56+
}

core/native/src/LocalTime.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public actual class LocalTime actual constructor(
6262
public actual fun parse(isoString: String): LocalTime =
6363
localTimeParser.parse(isoString)
6464

65+
public actual fun ofSecondOfDay(secondOfDay: Int): LocalTime =
66+
ofSecondOfDay(secondOfDay, 0)
67+
6568
// org.threeten.bp.LocalTime#ofSecondOfDay(long, int)
6669
internal fun ofSecondOfDay(secondOfDay: Int, nanoOfSecond: Int): LocalTime {
6770
// Unidiomatic code due to https://github.com/Kotlin/kotlinx-datetime/issues/5
@@ -127,7 +130,7 @@ public actual class LocalTime actual constructor(
127130
}
128131

129132
// org.threeten.bp.LocalTime#toSecondOfDay
130-
internal fun toSecondOfDay(): Int {
133+
public actual fun toSecondOfDay(): Int {
131134
var total: Int = hour * SECONDS_PER_HOUR
132135
total += minute * SECONDS_PER_MINUTE
133136
total += second

0 commit comments

Comments
 (0)