Skip to content

Current time millis #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ object Versions {
}

object Libs {
object KotlinX {
const val datetime = "org.jetbrains.kotlinx:kotlinx-datetime:${Versions.kotlinxDatetime}"
}

object Kotest {
const val assertionsCore = "io.kotest:kotest-assertions-core:${Versions.kotest}"
const val frameworkEngine = "io.kotest:kotest-framework-engine:${Versions.kotest}"
Expand Down
14 changes: 12 additions & 2 deletions operations-stdlib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

kotlin {
jvm {
mavenPublication{ setFullModuleArtifactId() }
mavenPublication { setFullModuleArtifactId() }
compilations.all {
kotlinOptions.jvmTarget = JavaVersion.VERSION_11.majorVersion
}
Expand All @@ -22,7 +22,6 @@ kotlin {
dependencies {
implementation(project(Modules.operationsApi))
implementation(project(Modules.utils))
implementation(Libs.KotlinX.datetime)
}
}
val commonTest by getting {
Expand All @@ -35,19 +34,30 @@ kotlin {
implementation(project(Modules.utils))
}
}
val jvmMain by getting
val jvmTest by getting {
dependsOn(commonTest)
dependencies {
implementation(Libs.Kotest.jvmJunit5Runner)
}
}
val iosX64Main by getting
val iosX64Test by getting
val iosArm64Main by getting
val iosArm64Test by getting
val iosSimulatorArm64Main by getting
val iosSimulatorArm64Test by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
}
}
}
6 changes: 2 additions & 4 deletions operations-stdlib/src/commonMain/kotlin/CurrentTimeMillis.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import kotlinx.datetime.Clock
import operation.StandardLogicOperation

// TODO Discuss when implementing stdlib if we want to use this library for time.
object CurrentTimeMillis : StandardLogicOperation {
override fun evaluateLogic(expression: Any?, data: Any?): Any = Clock.System.now().toEpochMilliseconds()
expect object CurrentTimeMillis: StandardLogicOperation {
override fun evaluateLogic(expression: Any?, data: Any?): Any
}
18 changes: 18 additions & 0 deletions operations-stdlib/src/iosMain/kotlin/CurrentTimeMillis.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import operation.StandardLogicOperation
import platform.Foundation.NSDate
import platform.Foundation.timeIntervalSince1970

actual object CurrentTimeMillis : StandardLogicOperation {
private const val MILLIS_IN_SECOND: Int = 1_000

actual override fun evaluateLogic(expression: Any?, data: Any?): Any {
/*
`timeIntervalSince1970()` return Double value in seconds with fraction part.
We have to multiply this by 1000 to move fractions value
*/
val currentTimestampInMillis = { NSDate().timeIntervalSince1970() * MILLIS_IN_SECOND }

// We have to convert Double to Long to remove fraction part.
return currentTimestampInMillis().toLong()
}
}
19 changes: 19 additions & 0 deletions operations-stdlib/src/iosTest/kotlin/CurrentTimeMillisTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
import io.kotest.matchers.types.beInstanceOf

class CurrentTimeMillisTests : FunSpec() {
init {
val operatorName = "currentTime"
val logicEngine = JsonLogicEngine.Builder()
.addStandardOperation(operatorName, CurrentTimeMillis)
.build()

test("CurrentTimeMillis.evaluateLogic should be Long type") {
val result = logicEngine.evaluate(mapOf(operatorName to emptyList<Any>()), null)

result should beInstanceOf<JsonLogicResult.Success>()
(result as JsonLogicResult.Success).value should beInstanceOf<Long>()
}
}
}
5 changes: 5 additions & 0 deletions operations-stdlib/src/jvmMain/kotlin/CurrentTimeMillis.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import operation.StandardLogicOperation

actual object CurrentTimeMillis : StandardLogicOperation {
actual override fun evaluateLogic(expression: Any?, data: Any?): Any = System.currentTimeMillis()
}
19 changes: 19 additions & 0 deletions operations-stdlib/src/jvmTest/kotlin/CurrentTimeMillisTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
import io.kotest.matchers.types.beInstanceOf

class CurrentTimeMillisTests : FunSpec() {
init {
val operatorName = "currentTime"
val logicEngine = JsonLogicEngine.Builder()
.addStandardOperation(operatorName, CurrentTimeMillis)
.build()

test("CurrentTimeMillis.evaluateLogic should be Long type") {
val result = logicEngine.evaluate(mapOf(operatorName to emptyList<Any>()), null)

result should beInstanceOf<JsonLogicResult.Success>()
(result as JsonLogicResult.Success).value should beInstanceOf<Long>()
}
}
}