Skip to content

Commit d1ad140

Browse files
committed
Provide common LocalDate.atStartOfDateIn
1 parent 15a7d59 commit d1ad140

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

core/commonMain/src/LocalDate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public fun String.toLocalDate(): LocalDate = LocalDate.parse(this)
4040
public fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime =
4141
LocalDateTime(year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
4242

43+
public expect fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant
4344

4445
/**
4546
* @throws DateTimeArithmeticException if arithmetic overflow occurs or the boundaries of [LocalDate] are exceeded at

core/commonTest/src/LocalDateTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ class LocalDateTest {
6666
checkLocalDateTimePart(date, datetime)
6767
}
6868

69+
@Test
70+
fun atStartOfDay() {
71+
val paris = TimeZone.of("Europe/Paris")
72+
val parisDate = LocalDate(2008, 6, 30)
73+
assertEquals(parisDate.atTime(0, 0).toInstant(paris),
74+
parisDate.atStartOfDayIn(paris), "paris")
75+
76+
// TODO: Find another TZ transition that works in Windows
77+
// val gaza = TimeZone.of("Asia/Gaza")
78+
// val gazaDate = LocalDate(2007, 4, 1)
79+
// assertEquals(gazaDate.atTime(1, 0).toInstant(gaza),
80+
// gazaDate.atStartOfDayIn(gaza), "gaza")
81+
82+
val fixed = TimeZone.of("UTC+14")
83+
val fixedDate = LocalDate(2007, 4, 1)
84+
assertEquals(fixedDate.atTime(0, 0).toInstant(fixed),
85+
fixedDate.atStartOfDayIn(fixed), "fixed")
86+
}
87+
6988
@Test
7089
fun addComponents() {
7190
val startDate = LocalDate(2016, 2, 29)

core/jsMain/src/LocalDate.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
4646
actual override fun compareTo(other: LocalDate): Int = this.value.compareTo(other.value).toInt()
4747
}
4848

49+
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
50+
this.value.atStartOfDay(timeZone.zoneId).toInstant().let(::Instant)
51+
4952
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate = plusNumber(1, unit)
5053
public actual fun LocalDate.plus(value: Int, unit: DateTimeUnit.DateBased): LocalDate = plusNumber(value, unit)
5154
public actual fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): LocalDate = plusNumber(value, unit)

core/jvmMain/src/LocalDate.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
4747
actual override fun compareTo(other: LocalDate): Int = this.value.compareTo(other.value)
4848
}
4949

50+
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
51+
this.value.atStartOfDay(timeZone.zoneId).toInstant().let(::Instant)
5052

5153
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate =
5254
plus(1L, unit)

core/nativeMain/src/LocalDate.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ public actual class LocalDate actual constructor(actual val year: Int, actual va
242242
}
243243
}
244244

245+
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
246+
timeZone.atStartOfDay(this)
247+
245248
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate =
246249
plus(1, unit)
247250

core/nativeTest/src/ThreeTenBpTimeZoneTest.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,4 @@ class ThreeTenBpTimeZoneTest {
137137
zone, ZoneOffset.ofSeconds(2 * 3600)), with(zone) { t.atZone() })
138138
}
139139

140-
@Test
141-
fun atStartOfDay() {
142-
val paris = TimeZone.of("Europe/Paris")
143-
assertEquals(LocalDateTime(2008, 6, 30, 0, 0, 0, 0).toInstant(paris),
144-
paris.atStartOfDay(LocalDate(2008, 6, 30)), "paris")
145-
val gaza = TimeZone.of("Asia/Gaza")
146-
assertEquals(LocalDateTime(2007, 4, 1, 1, 0, 0, 0).toInstant(gaza),
147-
gaza.atStartOfDay(LocalDate(2007, 4, 1)), "gaza")
148-
val fixed = TimeZone.of("UTC+14")
149-
assertEquals(LocalDateTime(2007, 4, 1, 0, 0, 0, 0).toInstant(fixed),
150-
fixed.atStartOfDay(LocalDate(2007, 4, 1)), "fixed")
151-
}
152-
153140
}

0 commit comments

Comments
 (0)