From f1262d21fdad1939d858f2383984d0e669fd9f81 Mon Sep 17 00:00:00 2001 From: Vasyl Pidlisniak Date: Tue, 2 Jul 2024 13:34:20 +0200 Subject: [PATCH] improve Kaluga Date handling in JS --- base/src/commonTest/kotlin/utils/DateTest.kt | 15 +++++++++++++++ base/src/jsMain/kotlin/utils/KalugaDate.kt | 10 ++-------- base/src/jsMain/kotlin/utils/KalugaTimeZone.kt | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/base/src/commonTest/kotlin/utils/DateTest.kt b/base/src/commonTest/kotlin/utils/DateTest.kt index 9516ce9cd..39a370e8c 100644 --- a/base/src/commonTest/kotlin/utils/DateTest.kt +++ b/base/src/commonTest/kotlin/utils/DateTest.kt @@ -160,4 +160,19 @@ class DateTest { assertEquals(0, startOfDayAfterDLS.hour) assertEquals(29, startOfDayAfterDLS.day) } + + @Test + fun testCopy() { + val now = DefaultKalugaDate.now(locale = KalugaLocale.enUsPosix) + assertEquals(now, now.copy()) + } + + @Test + fun testCompareTo() { + val offset = 1616828400000.milliseconds + val timestamp = DefaultKalugaDate.epoch(offset, locale = KalugaLocale.enUsPosix) + assertEquals(0, timestamp.compareTo(DefaultKalugaDate.epoch(offset, locale = KalugaLocale.enUsPosix))) + assertTrue { timestamp < DefaultKalugaDate.epoch(offset + 5.seconds, locale = KalugaLocale.enUsPosix) } + assertTrue { DefaultKalugaDate.epoch(offset - 5.seconds, locale = KalugaLocale.enUsPosix) < timestamp } + } } diff --git a/base/src/jsMain/kotlin/utils/KalugaDate.kt b/base/src/jsMain/kotlin/utils/KalugaDate.kt index f7122c377..c8a7e9725 100644 --- a/base/src/jsMain/kotlin/utils/KalugaDate.kt +++ b/base/src/jsMain/kotlin/utils/KalugaDate.kt @@ -100,7 +100,7 @@ actual class DefaultKalugaDate internal constructor(actual override val date: Ka get() = date.getTime().milliseconds set(_) { } - actual override fun copy(): KalugaDate = DefaultKalugaDate(kotlin.js.Date(date.getMilliseconds())) + actual override fun copy(): KalugaDate = DefaultKalugaDate(kotlin.js.Date(date.getTime().toLong())) actual override fun equals(other: Any?): Boolean { return (other as? KalugaDate)?.let { @@ -108,13 +108,7 @@ actual class DefaultKalugaDate internal constructor(actual override val date: Ka } ?: false } - actual override fun compareTo(other: KalugaDate): Int { - return when { - date.getMilliseconds() < other.millisecond -> -1 - date.getMilliseconds() == other.millisecond -> 0 - else -> 1 - } - } + actual override fun compareTo(other: KalugaDate): Int = date.getTime().compareTo(other.date.getTime()) actual override fun hashCode(): Int { return date.hashCode() diff --git a/base/src/jsMain/kotlin/utils/KalugaTimeZone.kt b/base/src/jsMain/kotlin/utils/KalugaTimeZone.kt index 38f7b34da..326100e50 100644 --- a/base/src/jsMain/kotlin/utils/KalugaTimeZone.kt +++ b/base/src/jsMain/kotlin/utils/KalugaTimeZone.kt @@ -54,4 +54,5 @@ actual class KalugaTimeZone internal constructor() : BaseTimeZone() { actual override fun offsetFromGMTAtDate(date: KalugaDate): Duration = 0.milliseconds actual override fun usesDaylightSavingsTime(date: KalugaDate): Boolean = false actual override fun copy(): KalugaTimeZone = KalugaTimeZone() + override fun equals(other: Any?): Boolean = other is KalugaTimeZone && identifier == other.identifier }