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) +}