Skip to content

Commit ce643fd

Browse files
committed
Provide a constructor of LocalDateTime from date and time parts
1 parent f82df29 commit ce643fd

File tree

7 files changed

+24
-4
lines changed

7 files changed

+24
-4
lines changed

core/common/src/LocalDate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond:
119119
* For finding an instant that corresponds to the start of a date in a particular time zone consider using
120120
* [LocalDate.atStartOfDayIn] function because a day does not always start at the fixed time 0:00:00.
121121
*/
122-
public fun LocalDate.atTime(time: LocalTime): LocalDateTime = atTime(time.hour, time.minute, time.second, time.nanosecond)
122+
public fun LocalDate.atTime(time: LocalTime): LocalDateTime = LocalDateTime(this, time)
123123

124124

125125
/**

core/common/src/LocalDateTime.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
8383
*/
8484
public constructor(year: Int, month: Month, dayOfMonth: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)
8585

86+
/**
87+
* Constructs a [LocalDateTime] instance by combining the given [date] and [time] parts.
88+
*/
89+
public constructor(date: LocalDate, time: LocalTime)
90+
8691
/** Returns the year component of the date. */
8792
public val year: Int
8893
/** Returns the number-of-month (1..12) component of the date. */

core/common/src/LocalTime.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,4 @@ public fun LocalTime.atDate(year: Int, month: Month, dayOfMonth: Int = 0): Local
110110
/**
111111
* Combines this time's components with the specified [LocalDate] components into a [LocalDateTime] value.
112112
*/
113-
public fun LocalTime.atDate(date: LocalDate): LocalDateTime = atDate(date.year, date.monthNumber, date.dayOfMonth)
113+
public fun LocalTime.atDate(date: LocalDate): LocalDateTime = LocalDateTime(date, this)

core/common/test/LocalDateTimeTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ class LocalDateTimeTest {
8686
println(localFixed.dayOfWeek)
8787
}
8888

89+
@Test
90+
fun constructFromParts() {
91+
val dt = Clock.System.now().toLocalDateTime(TimeZone.UTC)
92+
val dt2 = LocalDateTime(dt.date, dt.time)
93+
assertEquals(dt2, dt)
94+
assertEquals(dt2.date, dt.date)
95+
assertEquals(dt2.time, dt.time)
96+
}
97+
8998
/* Based on the ThreeTenBp project.
9099
* Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
91100
*/

core/js/src/LocalDateTime.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
2222
public actual constructor(year: Int, month: Month, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
2323
this(year, month.number, dayOfMonth, hour, minute, second, nanosecond)
2424

25+
public actual constructor(date: LocalDate, time: LocalTime) :
26+
this(jtLocalDateTime.of(date.value, time.value))
27+
2528
public actual val year: Int get() = value.year().toInt()
2629
public actual val monthNumber: Int get() = value.monthValue().toInt()
2730
public actual val month: Month get() = value.month().toMonth()

core/jvm/src/LocalDateTime.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
2727
public actual constructor(year: Int, month: Month, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
2828
this(year, month.number, dayOfMonth, hour, minute, second, nanosecond)
2929

30+
public actual constructor(date: LocalDate, time: LocalTime) :
31+
this(jtLocalDateTime.of(date.value, time.value))
32+
3033
public actual val year: Int get() = value.year
3134
public actual val monthNumber: Int get() = value.monthValue
3235
public actual val month: Month get() = value.month

core/native/src/LocalDateTime.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ internal val localDateTimeParser: Parser<LocalDateTime>
2222
}
2323

2424
@Serializable(with = LocalDateTimeIso8601Serializer::class)
25-
public actual class LocalDateTime internal constructor(
26-
public actual val date: LocalDate, public actual val time: LocalTime) : Comparable<LocalDateTime> {
25+
public actual class LocalDateTime
26+
public actual constructor(public actual val date: LocalDate, public actual val time: LocalTime) : Comparable<LocalDateTime> {
2727
public actual companion object {
2828
public actual fun parse(isoString: String): LocalDateTime =
2929
localDateTimeParser.parse(isoString)

0 commit comments

Comments
 (0)