Shervin
05/10/2023, 2:45 PMtruncatedTo(ChronoUnit.SECONDS)
that is present in Java local datetime in KotlinX ?Dmitry Khalanskiy [JB]
05/12/2023, 12:50 PMShervin
05/12/2023, 12:53 PMtruncateTo()
Dmitry Khalanskiy [JB]
05/12/2023, 12:54 PMShervin
05/12/2023, 12:55 PMDmitry Khalanskiy [JB]
05/12/2023, 12:56 PMShervin
05/12/2023, 12:57 PMDmitry Khalanskiy [JB]
05/12/2023, 12:57 PMShervin
05/12/2023, 1:00 PMDmitry Khalanskiy [JB]
05/12/2023, 1:09 PMpublic fun LocalTime.truncateTo(unit: DateTimeUnit.TimeBased): LocalTime =
LocalTime.fromNanosecondOfDay(toNanosecondOfDay().let { it - it % unit.nanoseconds })
public fun LocalDateTime.truncateTo(unit: DateTimeUnit.TimeBased): LocalDateTime =
LocalDateTime(date, time.truncateTo(unit))
Here are some tests to make sure it works:
@Test
fun testTruncation() {
val localTime = LocalTime(1, 2, 3, 456789123)
assertEquals(LocalTime(1, 2, 3, 456789123), localTime.truncateTo(DateTimeUnit.NANOSECOND))
assertEquals(LocalTime(1, 2, 3, 456789000), localTime.truncateTo(DateTimeUnit.MICROSECOND))
assertEquals(LocalTime(1, 2, 3, 456000000), localTime.truncateTo(DateTimeUnit.MILLISECOND))
assertEquals(LocalTime(1, 2, 3, 0), localTime.truncateTo(DateTimeUnit.SECOND))
assertEquals(LocalTime(1, 2, 0, 0), localTime.truncateTo(DateTimeUnit.MINUTE))
assertEquals(LocalTime(1, 0, 0, 0), localTime.truncateTo(DateTimeUnit.HOUR))
}
The complexity is in making this consistent with the more general request of temporal adjusters: https://github.com/Kotlin/kotlinx-datetime/issues/235 This requires careful considerations and thorough design.Shervin
05/12/2023, 1:14 PMShervin
05/12/2023, 1:15 PM