mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-25 21:23:55 +08:00
Implement TimeUtils
This commit is contained in:
parent
716220b28c
commit
9fa0b78d8c
mirai-core-utils/src
nativeMain/kotlin
Base64.ktClock.ktCollections.ktCoroutineUtils.ktCrypto.ktMiraiFile.ktTimeUtils.ktloadServiceOrNull.kt
nativeTest/kotlin
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import io.ktor.utils.io.core.*
|
||||
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
public actual inline fun measureTimeMillis(block: () -> Unit): Long {
|
||||
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
public actual suspend inline fun <R> runBIO(noinline block: () -> R): R {
|
||||
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
|
||||
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import io.ktor.utils.io.core.*
|
||||
|
@ -7,28 +7,37 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import kotlinx.cinterop.cValue
|
||||
import kotlinx.cinterop.memScoped
|
||||
import kotlinx.cinterop.pointed
|
||||
import platform.posix.CLOCK_REALTIME
|
||||
import platform.posix.clock_gettime
|
||||
import platform.posix.timespec
|
||||
import kotlinx.cinterop.*
|
||||
import platform.posix.*
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*
|
||||
* @see System.currentTimeMillis
|
||||
*/
|
||||
actual fun currentTimeMillis(): Long {
|
||||
public actual fun currentTimeMillis(): Long {
|
||||
memScoped {
|
||||
val s = cValue<timespec>()
|
||||
clock_gettime(CLOCK_REALTIME, s.ptr)
|
||||
return s.ptr.pointed.tv_nsec / 1000
|
||||
val timeT = alloc<time_tVar>()
|
||||
time(timeT.ptr)
|
||||
return timeT.value
|
||||
}
|
||||
}
|
||||
|
||||
actual fun currentTimeFormatted(format: String?): String {
|
||||
TODO("Not yet implemented")
|
||||
public actual fun currentTimeFormatted(format: String?): String {
|
||||
memScoped {
|
||||
val timeT = alloc<time_tVar>()
|
||||
time(timeT.ptr)
|
||||
val tm = localtime(timeT.ptr)
|
||||
try {
|
||||
val bb = allocArray<ByteVar>(40)
|
||||
strftime(bb, 40, "%Y-%M-%d %H:%M:%S", tm);
|
||||
return bb.toKString()
|
||||
} finally {
|
||||
free(tm)
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("RedundantVisibilityModifier")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
@ -16,6 +16,13 @@ internal class TimeUtilsTest {
|
||||
|
||||
@Test
|
||||
fun `can get currentTimeMillis`() {
|
||||
assertTrue { currentTimeMillis() > 1000 }
|
||||
val time = currentTimeMillis()
|
||||
assertTrue(time.toString()) { time > 1642549113 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can get currentTimeFormatted`() {
|
||||
// 2022-28-26 18:28:28
|
||||
assertTrue { currentTimeFormatted().matches(Regex("""\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}""")) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user