From dc877f7da74899dafd96404a749d163490bd02f2 Mon Sep 17 00:00:00 2001 From: yiwonnn <64483614+YeeWen13@users.noreply.github.com> Date: Mon, 11 Aug 2025 10:59:50 +0800 Subject: [PATCH] add instant function. Previously using Kotlinx libs now change to java --- .../main/kotlin/ai/neuon/utility/time/Instant.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/src/main/kotlin/ai/neuon/utility/time/Instant.kt b/core/src/main/kotlin/ai/neuon/utility/time/Instant.kt index 68d52ea..f842fc9 100644 --- a/core/src/main/kotlin/ai/neuon/utility/time/Instant.kt +++ b/core/src/main/kotlin/ai/neuon/utility/time/Instant.kt @@ -2,5 +2,21 @@ package ai.neuon.utility.time +import java.time.ZoneId import kotlin.time.ExperimentalTime +import kotlin.time.Instant +import java.time.Instant as JavaInstant +fun Instant.atStartOfMonth(timezone: ZoneId): Instant { + val javaInstant = JavaInstant.ofEpochSecond(epochSeconds, nanosecondsOfSecond.toLong()) + val zoned = javaInstant.atZone(timezone) + .withDayOfMonth(1) + .withHour(0).withMinute(0).withSecond(0).withNano(0) + return Instant.fromEpochSeconds(zoned.toEpochSecond(), zoned.nano) +} + +fun Instant.plusMonths(months: Long, timezone: ZoneId): Instant { + val javaInstant = JavaInstant.ofEpochSecond(epochSeconds, nanosecondsOfSecond.toLong()) + val zoned = javaInstant.atZone(timezone).plusMonths(months) + return Instant.fromEpochSeconds(zoned.toEpochSecond(), zoned.nano) +}