From 7f49e825f1df5d15e686f9364192a39270371bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Alejandro=20Montoya=20Corte=CC=81s?= Date: Thu, 18 Jan 2024 09:50:35 -0500 Subject: [PATCH] Allows to convert into/from micros_since_epoch:u64 for TimeStamp --- crates/bindings/src/timestamp.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/bindings/src/timestamp.rs b/crates/bindings/src/timestamp.rs index a238593faa8..d7aabe5d77e 100644 --- a/crates/bindings/src/timestamp.rs +++ b/crates/bindings/src/timestamp.rs @@ -69,6 +69,16 @@ impl Timestamp { let micros_since_epoch = self.micros_since_epoch.checked_sub(micros)?; Some(Self { micros_since_epoch }) } + + /// Converts the timestamp into the number of microseconds since the UNIX epoch. + pub fn into_micros_since_epoch(self) -> u64 { + self.micros_since_epoch + } + + /// Creates a new timestamp from the given number of microseconds since the UNIX epoch. + pub fn from_micros_since_epoch(micros_since_epoch: u64) -> Self { + Self { micros_since_epoch } + } } impl Add for Timestamp { @@ -90,5 +100,5 @@ impl Sub for Timestamp { } impl_st!([] Timestamp, _ts => spacetimedb_lib::AlgebraicType::U64); -impl_deserialize!([] Timestamp, de => u64::deserialize(de).map(|m| Self { micros_since_epoch: m })); -impl_serialize!([] Timestamp, (self, ser) => self.micros_since_epoch.serialize(ser)); +impl_deserialize!([] Timestamp, de => u64::deserialize(de).map(Self::from_micros_since_epoch)); +impl_serialize!([] Timestamp, (self, ser) => self.into_micros_since_epoch().serialize(ser));