Initial commit

This commit is contained in:
2025-07-30 15:45:43 +08:00
commit b1b205b9b4
25 changed files with 1850 additions and 0 deletions

94
datetime/build.gradle.kts Normal file
View File

@@ -0,0 +1,94 @@
import java.util.*
plugins {
alias(libs.plugins.dokka)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.maven.publish)
}
repositories {
mavenCentral()
}
dependencies {
implementation(libs.mongo.bson)
}
kotlin {
jvmToolchain(11)
}
val javadocJar by tasks.register<Jar>("dokkaJavadocJar") {
dependsOn(tasks.dokkaJavadoc)
from(tasks.dokkaJavadoc.flatMap { it.outputDirectory })
archiveClassifier.set("javadoc")
}
val sourcesJar by tasks.register<Jar>("sourcesJar") {
from(sourceSets.main.get().allSource)
archiveClassifier.set("sources")
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = rootProject.group.toString()
artifactId = "bson-datetime"
version = rootProject.version.toString()
from(components["java"])
artifact(javadocJar)
artifact(sourcesJar)
}
}
repositories {
val secretFile = rootProject.file("secret.properties")
if (secretFile.exists()) {
val secret = Properties()
secretFile.inputStream().also { secret.load(it) }
val mavenUrl = secret["neuon.maven.url"] as? String
val mavenUsername = secret["neuon.maven.username"] as? String
val mavenPassword = secret["neuon.maven.password"] as? String
if (mavenUrl != null && mavenUsername != null && mavenPassword != null) {
maven {
requireNotNull(mavenUrl) {
"neuon.maven.url is missing from secret.properties"
}
requireNotNull(mavenUsername) {
"neuon.maven.username is missing from secret.properties"
}
requireNotNull(mavenPassword) {
"neuon.maven.password is missing from secret.properties"
}
name = "neuon"
url = uri(mavenUrl)
credentials(PasswordCredentials::class) {
username = mavenUsername
password = mavenPassword
}
}
} else {
val list = listOfNotNull(
"neuon.maven.url".takeIf { mavenUrl == null },
"neuon.maven.username".takeIf { mavenUsername == null },
"neuon.maven.password".takeIf { mavenPassword == null },
)
val message = buildString {
append("Missing Maven publication in secret.properties: ")
append(list.joinToString())
append(". Ignore this if you don't plan on publishing this library")
}
logger.error(message)
}
} else {
logger.error(
buildString {
append("Missing secret.properties. Ignore this if you don't plan on publishing this library")
},
)
}
}
}

View File

@@ -0,0 +1,33 @@
/*
* MIT License
*
* Copyright (c) 2025 Neuon AI
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
@file:OptIn(ExperimentalTime::class)
package ai.neuon.utility.bson.datetime
import org.bson.BsonArray
import org.bson.BsonDateTime
import org.bson.BsonDocument
import kotlin.time.ExperimentalTime
import kotlin.time.Instant
fun Instant.toBsonDateTime(): BsonDateTime = BsonDateTime(toEpochMilliseconds())
fun BsonDateTime.toInstant(): Instant = Instant.fromEpochMilliseconds(value)
fun BsonDocument.put(key: String, value: Instant) {
put(key = key, value.toBsonDateTime())
}
fun BsonArray.add(instant: Instant) {
add(instant.toBsonDateTime())
}