|
| 1 | +//! Custom timestamp type for UniFFI bindings. |
| 2 | +//! |
| 3 | +//! This wrapper allows custom type mapping per target language: |
| 4 | +//! - Kotlin: maps to `kotlinx.datetime.Instant` |
| 5 | +//! - Swift: maps to `Date` |
| 6 | +//! - WASM: not changed |
| 7 | +//! |
| 8 | +//! This unifies timestamp handling across JVM/Android and KMP bindings. |
| 9 | +//! This can be removed once we fully migrate to Kotlin Multiplatform and |
| 10 | +//! stop generating JVM/Android bindings. |
| 11 | +
|
| 12 | +#[cfg(not(target_family = "wasm"))] |
| 13 | +use std::time::{Duration, SystemTime}; |
| 14 | + |
| 15 | +/// A wrapper around `SystemTime` for FFI bindings with custom type mapping per language. |
| 16 | +#[cfg(not(target_family = "wasm"))] |
| 17 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 18 | +pub struct Timestamp(pub SystemTime); |
| 19 | + |
| 20 | +#[cfg(not(target_family = "wasm"))] |
| 21 | +impl Timestamp { |
| 22 | + /// Creates a new `Timestamp` from a `SystemTime`. |
| 23 | + pub fn new(time: SystemTime) -> Self { |
| 24 | + Self(time) |
| 25 | + } |
| 26 | + |
| 27 | + /// Creates a new `Timestamp` from seconds since the Unix epoch. |
| 28 | + pub fn from_epoch_secs(secs: u64) -> Self { |
| 29 | + Self(SystemTime::UNIX_EPOCH + Duration::from_secs(secs)) |
| 30 | + } |
| 31 | + |
| 32 | + /// Returns the inner `SystemTime`. |
| 33 | + pub fn into_inner(self) -> SystemTime { |
| 34 | + self.0 |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(not(target_family = "wasm"))] |
| 39 | +impl From<SystemTime> for Timestamp { |
| 40 | + fn from(time: SystemTime) -> Self { |
| 41 | + Self(time) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[cfg(not(target_family = "wasm"))] |
| 46 | +impl From<Timestamp> for SystemTime { |
| 47 | + fn from(timestamp: Timestamp) -> Self { |
| 48 | + timestamp.0 |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(not(target_family = "wasm"))] |
| 53 | +uniffi::custom_type!(Timestamp, SystemTime); |
0 commit comments