+ Intstant.truncateToMinute()
This commit is contained in:
1 parent
5dc2159024
commit
d0230c5b89
3 files changed
+54
-26
No files matched your search
@@ -17,10 +17,7 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.UtcOffset
|
||||
import kotlinx.datetime.asTimeZone
|
||||
import kotlinx.datetime.*
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
@@ -193,6 +190,18 @@ class ObjInstant(val instant: Instant,val truncateMode: LynonSettings.InstantTru
|
||||
moduleName = "lyng.time",
|
||||
getter = { ObjInt(thisAs<ObjInstant>().instant.nanosecondsOfSecond.toLong()) }
|
||||
)
|
||||
addFnDoc(
|
||||
name = "truncateToMinute",
|
||||
doc = "Truncate this instant to the nearest minute.",
|
||||
returns = type("lyng.Instant"),
|
||||
moduleName = "lyng.time"
|
||||
) {
|
||||
val t = thisAs<ObjInstant>().instant
|
||||
val tz = TimeZone.UTC
|
||||
val dt = t.toLocalDateTime(tz)
|
||||
val truncated = LocalDateTime(dt.year, dt.month, dt.dayOfMonth, dt.hour, dt.minute, 0, 0)
|
||||
ObjInstant(truncated.toInstant(tz), LynonSettings.InstantTruncateMode.Second)
|
||||
}
|
||||
addFnDoc(
|
||||
name = "truncateToSecond",
|
||||
doc = "Truncate this instant to the nearest second.",
|
||||
|
||||
@@ -3169,6 +3169,19 @@ class ScriptTest {
|
||||
assertEquals( n1 - now, 7.seconds )
|
||||
assertEquals( now - n1, -7.seconds )
|
||||
|
||||
val t3 = Instant("2024-01-01T12:00:00.123456Z")
|
||||
val t4 = t3.truncateToMinute
|
||||
assertEquals(t4.toRFC3339(), "2024-01-01T12:00:00Z")
|
||||
assertEquals(
|
||||
"2024-01-01T12:00:00Z",
|
||||
Instant("2024-01-01T12:00:59.999Z").truncateToMinute().toRFC3339()
|
||||
)
|
||||
|
||||
val t5 = t3.truncateToSecond
|
||||
assertEquals(t5.toRFC3339(), "2024-01-01T12:00:00Z")
|
||||
|
||||
val t6 = t3.truncateToMillisecond
|
||||
assertEquals(t6.toRFC3339(), "2024-01-01T12:00:00.123Z")
|
||||
""".trimIndent()
|
||||
)
|
||||
delay(1000)
|
||||
|
||||
Reference in new issue
Block a user