From 48e9d23b1091c9c1399e086dc02a399470a38aca Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Mon, 13 Jul 2020 17:25:09 -0400 Subject: [PATCH] Expose TimeZone through Clock This creates a single object which can be faked or replaced to control the current time and its geographical location for projecting into local dates and times. --- core/commonMain/src/Clock.kt | 5 ++++- core/commonMain/src/TimeZone.kt | 1 + core/commonTest/src/InstantTest.kt | 6 +++--- core/commonTest/src/LocalDateTest.kt | 2 +- core/commonTest/src/LocalDateTimeTest.kt | 2 +- core/commonTest/src/TimeZoneTest.kt | 4 ++-- 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/core/commonMain/src/Clock.kt b/core/commonMain/src/Clock.kt index 7a691fb..b56c355 100644 --- a/core/commonMain/src/Clock.kt +++ b/core/commonMain/src/Clock.kt @@ -5,6 +5,7 @@ package kotlinx.datetime +import kotlinx.datetime.TimeZone.Companion import kotlin.time.Duration import kotlin.time.ExperimentalTime import kotlin.time.TimeMark @@ -12,9 +13,11 @@ import kotlin.time.TimeSource public interface Clock { public fun now(): Instant + public fun timeZone(): TimeZone public object System : Clock { override fun now(): Instant = @Suppress("DEPRECATION_ERROR") Instant.now() + override fun timeZone(): TimeZone = @Suppress("DEPRECATION_ERROR") TimeZone.SYSTEM } public companion object { @@ -22,7 +25,7 @@ public interface Clock { } } -public fun Clock.todayAt(timeZone: TimeZone): LocalDate = +public fun Clock.today(timeZone: TimeZone = timeZone()): LocalDate = now().toLocalDateTime(timeZone).date diff --git a/core/commonMain/src/TimeZone.kt b/core/commonMain/src/TimeZone.kt index bdd34c0..648fffc 100644 --- a/core/commonMain/src/TimeZone.kt +++ b/core/commonMain/src/TimeZone.kt @@ -9,6 +9,7 @@ public expect open class TimeZone { public val id: String companion object { + @Deprecated("Use Clock.System.timeZone() instead", ReplaceWith("Clock.System.timeZone()", "kotlinx.datetime.Clock"), level = DeprecationLevel.ERROR) val SYSTEM: TimeZone val UTC: TimeZone fun of(zoneId: String): TimeZone diff --git a/core/commonTest/src/InstantTest.kt b/core/commonTest/src/InstantTest.kt index c26555e..bde9a57 100644 --- a/core/commonTest/src/InstantTest.kt +++ b/core/commonTest/src/InstantTest.kt @@ -50,7 +50,7 @@ class InstantTest { fun instantToLocalDTConversion() { val now = Clock.System.now() println(now.toLocalDateTime(TimeZone.UTC)) - println(now.toLocalDateTime(TimeZone.SYSTEM)) + println(now.toLocalDateTime(Clock.System.timeZone())) } /* Based on the ThreeTenBp project. @@ -145,8 +145,8 @@ class InstantTest { val instant1 = Instant.fromEpochMilliseconds(millis1) val instant2 = Instant.fromEpochMilliseconds(millis2) - val diff = instant1.periodUntil(instant2, TimeZone.SYSTEM) - val instant3 = instant1.plus(diff, TimeZone.SYSTEM) + val diff = instant1.periodUntil(instant2, Clock.System.timeZone()) + val instant3 = instant1.plus(diff, Clock.System.timeZone()) if (instant2 != instant3) println("start: $instant1, end: $instant2, start + diff: $instant3, diff: $diff") diff --git a/core/commonTest/src/LocalDateTest.kt b/core/commonTest/src/LocalDateTest.kt index a5bf0df..ad77b1c 100644 --- a/core/commonTest/src/LocalDateTest.kt +++ b/core/commonTest/src/LocalDateTest.kt @@ -71,7 +71,7 @@ class LocalDateTest { @Test fun tomorrow() { - val today = Clock.System.todayAt(TimeZone.SYSTEM) + val today = Clock.System.today() val nextMonthPlusDay1 = today + 1.calendarMonths + 1.calendarDays val nextMonthPlusDay2 = today + (1.calendarMonths + 1.calendarDays) diff --git a/core/commonTest/src/LocalDateTimeTest.kt b/core/commonTest/src/LocalDateTimeTest.kt index 966bbd2..ac35600 100644 --- a/core/commonTest/src/LocalDateTimeTest.kt +++ b/core/commonTest/src/LocalDateTimeTest.kt @@ -61,7 +61,7 @@ class LocalDateTimeTest { @Test fun getCurrentHMS() { - with(Clock.System.now().toLocalDateTime(TimeZone.SYSTEM)) { + with(Clock.System.now().toLocalDateTime(Clock.System.timeZone())) { println("${hour}h ${minute}m") } } diff --git a/core/commonTest/src/TimeZoneTest.kt b/core/commonTest/src/TimeZoneTest.kt index 403c525..45b00e0 100644 --- a/core/commonTest/src/TimeZoneTest.kt +++ b/core/commonTest/src/TimeZoneTest.kt @@ -20,7 +20,7 @@ class TimeZoneTest { @Test fun system() { - println(TimeZone.SYSTEM) + println(Clock.System.timeZone()) // TODO: decide how to assert system tz properties } @@ -31,7 +31,7 @@ class TimeZoneTest { allTzIds.forEach(::println) assertNotEquals(0, allTzIds.size) - assertTrue(TimeZone.SYSTEM.id in allTzIds) + assertTrue(Clock.System.timeZone().id in allTzIds) assertTrue("UTC" in allTzIds) } -- 2.27.0