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