ref #34 time formatting and precision time access

This commit is contained in:
sergeych committed 2025-07-10 11:10:26 +03:00
1 parent 732d8f3877
commit 230cb0a067
8 files changed
+108 -14

No files matched your search

@@ -33,10 +33,6 @@ open class ObjClass(
return instance
}
// fun defaultInstance(): Obj = object : Obj() {
// override val objClass: ObjClass = this@ObjClass
// }
fun createField(
name: String,
initialValue: Obj,
@@ -56,6 +56,11 @@ class ObjDuration(val duration: Duration) : Obj() {
addFn("milliseconds") {
thisAs<ObjDuration>().duration.toDouble(DurationUnit.MILLISECONDS).toObj()
}
addFn("microseconds") {
thisAs<ObjDuration>().duration.toDouble(DurationUnit.MICROSECONDS).toObj()
}
// extensions
ObjInt.type.addFn("seconds") {
ObjDuration(thisAs<ObjInt>().value.seconds)
}
@@ -63,7 +68,6 @@ class ObjDuration(val duration: Duration) : Obj() {
ObjInt.type.addFn("second") {
ObjDuration(thisAs<ObjInt>().value.seconds)
}
ObjInt.type.addFn("milliseconds") {
ObjDuration(thisAs<ObjInt>().value.milliseconds)
}
@@ -71,7 +75,6 @@ class ObjDuration(val duration: Duration) : Obj() {
ObjInt.type.addFn("millisecond") {
ObjDuration(thisAs<ObjInt>().value.milliseconds)
}
ObjReal.type.addFn("seconds") {
ObjDuration(thisAs<ObjReal>().value.seconds)
}
@@ -34,6 +34,10 @@ class ObjInstant(val instant: Instant) : Obj() {
return super.compareTo(scope, other)
}
override suspend fun toKotlin(scope: Scope): Any {
return instant
}
companion object {
val distantFuture by lazy {
ObjInstant(Instant.DISTANT_FUTURE)
@@ -51,7 +55,7 @@ class ObjInstant(val instant: Instant) : Obj() {
when (a0) {
null -> {
val t = Clock.System.now()
Instant.fromEpochSeconds(t.epochSeconds, (t.nanosecondsOfSecond / 1_000_000).toLong()*1_000_000)
Instant.fromEpochSeconds(t.epochSeconds, t.nanosecondsOfSecond)
}
is ObjInt -> Instant.fromEpochSeconds(a0.value)
is ObjReal -> {
@@ -78,6 +82,14 @@ class ObjInstant(val instant: Instant) : Obj() {
addFn("isDistantPast") {
thisAs<ObjInstant>().instant.isDistantPast.toObj()
}
addFn("epochWholeSeconds") {
ObjInt(thisAs<ObjInstant>().instant.epochSeconds)
}
addFn("nanosecondsOfSecond") {
ObjInt(thisAs<ObjInstant>().instant.nanosecondsOfSecond.toLong())
}
// class members
addClassConst("distantFuture", distantFuture)
addClassConst("distantPast", distantPast)
// addFn("epochMilliseconds") {
@@ -2560,4 +2560,18 @@ class ScriptTest {
)
}
@Test
fun testInstantFormatting() = runTest {
eval(
"""
import lyng.time
val now = Instant()
val unixEpoch = "%ts"(now)
println("current seconds is %s"(unixEpoch))
println("current time is %tT"(now))
assertEquals( unixEpoch.toInt(), now.epochSeconds.toInt() )
""".trimIndent()
)
}
}
+13
View File
@@ -2,6 +2,7 @@ import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Source
import net.sergeych.lyng.eval
import net.sergeych.lyng.pacman.InlineSourcesImportProvider
import net.sergeych.lyng.toSource
import kotlin.test.Test
@@ -37,4 +38,16 @@ class OtherTests {
assertEquals("foo1 / bar1", scope.eval(src).toString())
}
@Test
fun testInstantTruncation() = runBlocking {
eval("""
import lyng.time
val t1 = Instant()
val t2 = Instant()
// assert( t1 != t2 )
println(t1 - t2)
""".trimIndent())
Unit
}
}