Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to convert into/from micros_since_epoch:u64 for TimeStamp #731

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions crates/bindings/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Duration> for Timestamp {
Expand All @@ -90,5 +100,5 @@ impl Sub<Duration> 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));