darwinMain.io.islandtime.clock.internal.SystemClockImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-metadata Show documentation
Show all versions of core-metadata Show documentation
A multiplatform library for working with dates and times
The newest version!
package io.islandtime.clock.internal
import io.islandtime.Instant
import io.islandtime.PlatformInstant
import io.islandtime.TimeZone
import io.islandtime.clock.SystemClock
import io.islandtime.measures.*
import kotlinx.cinterop.alloc
import kotlinx.cinterop.convert
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import platform.Foundation.NSDate
import platform.posix.gettimeofday
import platform.posix.timeval
internal actual fun createSystemClock(zone: TimeZone): SystemClock {
return object : SystemClock() {
override val zone: TimeZone = zone
override fun readMilliseconds(): LongMilliseconds {
return readSystemTime { seconds, microseconds -> seconds + microseconds.inMilliseconds }
}
override fun readInstant(): Instant {
return readSystemTime { seconds, microseconds -> Instant(seconds, microseconds.inNanoseconds) }
}
override fun readPlatformInstant(): PlatformInstant = NSDate()
}
}
private inline fun readSystemTime(action: (seconds: LongSeconds, microseconds: IntMicroseconds) -> T): T {
return memScoped {
val posixTime = alloc()
gettimeofday(posixTime.ptr, null)
action(posixTime.tv_sec.convert().seconds, posixTime.tv_usec.microseconds)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy