1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 21:23:55 +08:00

Implement TimeUtils

This commit is contained in:
Him188 2022-05-26 18:42:37 +01:00
parent 716220b28c
commit 9fa0b78d8c
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
9 changed files with 43 additions and 13 deletions

View File

@ -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.*

View File

@ -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 {

View File

@ -7,6 +7,8 @@
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("RedundantVisibilityModifier")
package net.mamoe.mirai.utils
import kotlin.reflect.KClass

View File

@ -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 {

View File

@ -7,6 +7,8 @@
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("RedundantVisibilityModifier")
package net.mamoe.mirai.utils

View File

@ -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.*

View File

@ -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)
}
}
}

View File

@ -7,6 +7,8 @@
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("RedundantVisibilityModifier")
package net.mamoe.mirai.utils
import kotlin.reflect.KClass

View File

@ -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}""")) }
}
}