Straightforward code does not work, and I cannot find any better API to do this, but the problem seems to be quite simple, I am sure other people tried it.
The following code gives me segmentation fault:
val metadataTime: Long = 1523720871L
val mtRef: CValuesRef<time_tVar>? = metadataTime.toCPointer<time_tVar>()
val gmtRef = gmtime(mtRef)
Nothing in StableRef seems suitable, and I do not see other way to transform a Long into CPointer.
Long.toCPointer() is an unsafe cast between Long and any pointer type. If you use it on a value, that does contain the address of a valid memory location, you will create an invalid pointer. Using an invalid pointer usually leads to segmentation faults. You code is roughly equivalent to the following C code:
What I assume you want to do is convert your Long literal to a time_t and call gmtimeon the resulting time_t. The thing is that gmtime takes the argument by reference rather than by value. You should be able to do the following:
val gmtRef = gmtime(cValuesOf(1523720871L.convert()))
The convert call is mainly there for portability. Depending on your platform, you may not even need it.
/** The current value in Unix epoch */
var value: Long
get() = memScoped {
val tm = alloc<tm>()
getValue(tm.ptr)
mktime(tm.ptr)
}
set(value) = memScoped {
val time = alloc<time_tVar>()
time.value = value
setValue(localtime(time.ptr)!!)
}