Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/TimestampHelper.kt

raw
1 /**
2 * @suppress
3 */
4 public object FfiConverterTimestamp: FfiConverterRustBuffer<java.time.Instant> {
5 override fun read(buf: ByteBuffer): java.time.Instant {
6 val seconds = buf.getLong()
7 // Type mismatch (should be u32) but we check for overflow/underflow below
8 val nanoseconds = buf.getInt().toLong()
9 if (nanoseconds < 0) {
10 throw java.time.DateTimeException("Instant nanoseconds exceed minimum or maximum supported by uniffi")
11 }
12 if (seconds >= 0) {
13 return java.time.Instant.EPOCH.plus(java.time.Duration.ofSeconds(seconds, nanoseconds))
14 } else {
15 return java.time.Instant.EPOCH.minus(java.time.Duration.ofSeconds(-seconds, nanoseconds))
16 }
17 }
18
19 // 8 bytes for seconds, 4 bytes for nanoseconds
20 override fun allocationSize(value: java.time.Instant) = 12UL
21
22 override fun write(value: java.time.Instant, buf: ByteBuffer) {
23 var epochOffset = java.time.Duration.between(java.time.Instant.EPOCH, value)
24
25 var sign = 1
26 if (epochOffset.isNegative()) {
27 sign = -1
28 epochOffset = epochOffset.negated()
29 }
30
31 if (epochOffset.nano < 0) {
32 // Java docs provide guarantee that nano will always be positive, so this should be impossible
33 // See: https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
34 throw IllegalArgumentException("Invalid timestamp, nano value must be non-negative")
35 }
36
37 buf.putLong(sign * epochOffset.seconds)
38 // Type mismatch (should be u32) but since values will always be between 0 and 999,999,999 it should be OK
39 buf.putInt(epochOffset.nano)
40 }
41 }