Skip to content

Commit

Permalink
improve Kaluga Date handling in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
vpodlesnyak committed Jul 2, 2024
1 parent a548049 commit f1262d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
15 changes: 15 additions & 0 deletions base/src/commonTest/kotlin/utils/DateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
10 changes: 2 additions & 8 deletions base/src/jsMain/kotlin/utils/KalugaDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,15 @@ 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 {
timeZone == other.timeZone && durationSinceEpoch == other.durationSinceEpoch
} ?: 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()
Expand Down
1 change: 1 addition & 0 deletions base/src/jsMain/kotlin/utils/KalugaTimeZone.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f1262d2

Please sign in to comment.