Skip to content

Commit 1b1a59e

Browse files
committed
Add LocalDate and LocalDateTime constructors with Month
1 parent ee220be commit 1b1a59e

File tree

11 files changed

+54
-8
lines changed

11 files changed

+54
-8
lines changed

core/commonMain/src/LocalDate.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ public expect class LocalDate : Comparable<LocalDate> {
3737
*/
3838
public constructor(year: Int, monthNumber: Int, dayOfMonth: Int)
3939

40+
/**
41+
* Constructs a [LocalDate] instance from the given date components.
42+
*
43+
* The supported ranges of components:
44+
* - [year] the range is platform dependent, but at least is enough to represent dates of all instants between
45+
* [Instant.DISTANT_PAST] and [Instant.DISTANT_FUTURE]
46+
* - [month] all values of the [Month] enum
47+
* - [dayOfMonth] `1..31`, the upper bound can be less, depending on the month
48+
*
49+
* @throws IllegalArgumentException if any parameter is out of range, or if [dayOfMonth] is invalid for the given [month] and
50+
* [year].
51+
*/
52+
public constructor(year: Int, month: Month, dayOfMonth: Int)
53+
4054
/** Returns the year component of the date. */
4155
public val year: Int
4256
/** Returns the number-of-month (1..12) component of the date. */

core/commonMain/src/LocalDateTime.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
4949
*/
5050
public constructor(year: Int, monthNumber: Int, dayOfMonth: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)
5151

52+
/**
53+
* Constructs a [LocalDateTime] instance from the given date and time components.
54+
*
55+
* The supported ranges of components:
56+
* - [year] the range is platform dependent, but at least is enough to represent dates of all instants between
57+
* [Instant.DISTANT_PAST] and [Instant.DISTANT_FUTURE]
58+
* - [month] all values of the [Month] enum
59+
* - [dayOfMonth] `1..31`, the upper bound can be less, depending on the month
60+
* - [hour] `0..23`
61+
* - [minute] `0..59`
62+
* - [second] `0..59`
63+
* - [nanosecond] `0..999_999_999`
64+
*
65+
* @throws IllegalArgumentException if any parameter is out of range, or if [dayOfMonth] is invalid for the given [month] and
66+
* [year].
67+
*/
68+
public constructor(year: Int, month: Month, dayOfMonth: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)
69+
5270
/** Returns the year component of the date. */
5371
public val year: Int
5472
/** Returns the number-of-month (1..12) component of the date. */

core/commonTest/src/InstantTest.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class InstantTest {
103103
}
104104
}
105105

106-
val instant1 = LocalDateTime(2019, 10, 27, 2, 59).toInstant(zone)
106+
val instant1 = LocalDateTime(2019, Month.OCTOBER, 27, 2, 59).toInstant(zone)
107107
checkComponents(instant1.toLocalDateTime(zone), 2019, 10, 27, 2, 59)
108108

109109
val instant2 = instant1.plus(DateTimePeriod(hours = 24), zone)
@@ -232,9 +232,8 @@ class InstantTest {
232232
with(TimeZone.UTC) TZ@ {
233233
val date1 = Instant.fromEpochMilliseconds(millis1).toLocalDateTime().date
234234
val date2 = Instant.fromEpochMilliseconds(millis2).toLocalDateTime().date
235-
fun LocalDate.instantAtStartOfDay() = LocalDateTime(year, monthNumber, dayOfMonth, 0, 0, 0, 0).toInstant()
236-
val instant1 = date1.instantAtStartOfDay()
237-
val instant2 = date2.instantAtStartOfDay()
235+
val instant1 = date1.atStartOfDayIn(this@TZ)
236+
val instant2 = date2.atStartOfDayIn(this@TZ)
238237

239238
val diff1 = instant1.periodUntil(instant2, this@TZ)
240239
val diff2 = date1.periodUntil(date2)

core/commonTest/src/LocalDateTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ class LocalDateTest {
5454
@Test
5555
fun localDateTimePart() {
5656
val datetime = LocalDateTime.parse("2016-02-29T23:59")
57-
val date = LocalDate(2016, 2, 29)
57+
val date = LocalDate(2016, Month.FEBRUARY, 29)
5858
checkLocalDateTimePart(date, datetime)
5959
}
6060

6161
@Test
6262
fun atTime() {
63-
val date = LocalDate(2016, 2, 29)
63+
val date = LocalDate(2016, Month.FEBRUARY, 29)
6464
val datetime = date.atTime(12, 1, 59)
6565
checkComponents(datetime, 2016, 2, 29, 12, 1, 59)
6666
checkLocalDateTimePart(date, datetime)
@@ -231,4 +231,4 @@ fun checkInvalidDate(constructor: (year: Int, month: Int, day: Int) -> LocalDate
231231
assertFailsWith<IllegalArgumentException> { constructor(2007, 1, 32) }
232232
assertFailsWith<IllegalArgumentException> { constructor(2007, 0, 1) }
233233
assertFailsWith<IllegalArgumentException> { constructor(2007, 13, 1) }
234-
}
234+
}

core/commonTest/src/LocalDateTimeTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class LocalDateTimeTest {
106106
@Test
107107
fun constructInvalidTime() {
108108
fun localTime(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime =
109-
LocalDateTime(2020, 1, 1, hour, minute, second, nanosecond)
109+
LocalDateTime(2020, Month.JANUARY, 1, hour, minute, second, nanosecond)
110110
localTime(23, 59)
111111
assertFailsWith<IllegalArgumentException> { localTime(-1, 0) }
112112
assertFailsWith<IllegalArgumentException> { localTime(24, 0) }

core/jsMain/src/LocalDate.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
2929
throw e
3030
})
3131

32+
public actual constructor(year: Int, month: Month, dayOfMonth: Int) : this(year, month.number, dayOfMonth)
33+
3234
public actual val year: Int get() = value.year().toInt()
3335
public actual val monthNumber: Int get() = value.monthValue().toInt()
3436
public actual val month: Month get() = value.month().toMonth()

core/jsMain/src/LocalDateTime.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
1717
throw e
1818
})
1919

20+
public actual constructor(year: Int, month: Month, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
21+
this(year, month.number, dayOfMonth, hour, minute, second, nanosecond)
22+
2023
public actual val year: Int get() = value.year().toInt()
2124
public actual val monthNumber: Int get() = value.monthValue().toInt()
2225
public actual val month: Month get() = value.month().toMonth()

core/jvmMain/src/LocalDate.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
3030
throw IllegalArgumentException(e)
3131
})
3232

33+
public actual constructor(year: Int, month: Month, dayOfMonth: Int) : this(year, month.number, dayOfMonth)
34+
3335
public actual val year: Int get() = value.year
3436
public actual val monthNumber: Int get() = value.monthValue
3537
public actual val month: Month get() = value.month

core/jvmMain/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
throw IllegalArgumentException(e)
2323
})
2424

25+
public actual constructor(year: Int, month: Month, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
26+
this(year, month.number, dayOfMonth, hour, minute, second, nanosecond)
27+
2528
public actual val year: Int get() = value.year
2629
public actual val monthNumber: Int get() = value.monthValue
2730
public actual val month: Month get() = value.month

core/nativeMain/src/LocalDate.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public actual class LocalDate actual constructor(actual val year: Int, actual va
5050
}
5151
}
5252

53+
public actual constructor(year: Int, month: Month, dayOfMonth: Int) : this(year, month.number, dayOfMonth)
54+
5355
actual companion object {
5456
actual fun parse(isoString: String): LocalDate =
5557
localDateParser.parse(isoString)

core/nativeMain/src/LocalDateTime.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public actual class LocalDateTime internal constructor(
3131
actual constructor(year: Int, monthNumber: Int, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
3232
this(LocalDate(year, monthNumber, dayOfMonth), LocalTime.of(hour, minute, second, nanosecond))
3333

34+
public actual constructor(year: Int, month: Month, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
35+
this(LocalDate(year, month, dayOfMonth), LocalTime.of(hour, minute, second, nanosecond))
36+
3437
actual val year: Int get() = date.year
3538
actual val monthNumber: Int get() = date.monthNumber
3639
actual val month: Month get() = date.month

0 commit comments

Comments
 (0)