0
0
mirror of https://github.com/signalapp/libsignal.git synced 2024-09-19 19:42:19 +02:00

backup: Canonicalize Timestamps and Durations as milliseconds

This is closer to the raw value used for most protobuf fields, and
avoids printing as an object containing secs+nsecs.
This commit is contained in:
Jordan Rose 2024-08-09 16:37:08 -07:00
parent ea8aeb71fc
commit d44fdc92ca
2 changed files with 30 additions and 6 deletions

View File

@ -6,10 +6,10 @@
use std::ops::Add;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp(SystemTime);
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Duration(std::time::Duration);
impl Timestamp {
@ -34,6 +34,19 @@ impl Timestamp {
}
}
impl serde::Serialize for Timestamp {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let offset = self
.0
.duration_since(UNIX_EPOCH)
.expect("should not be possible to construct a Timestamp older than UNIX_EPOCH");
serde::Serialize::serialize(&Duration(offset), serializer)
}
}
impl Duration {
pub(super) const ZERO: Self = Self(std::time::Duration::ZERO);
pub(super) const TWELVE_HOURS: Self = Self(std::time::Duration::from_secs(60 * 60 * 12));
@ -43,6 +56,20 @@ impl Duration {
}
}
impl serde::Serialize for Duration {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
assert!(
self.0.as_millis() * 1_000_000 == self.0.as_nanos(),
"should not be possible to construct a Duration with sub-millisecond precision ({:?})",
self.0
);
self.0.as_millis().serialize(serializer)
}
}
impl Add<Duration> for Timestamp {
type Output = Timestamp;

View File

@ -31,10 +31,7 @@
"story_view_receipts_enabled": true,
"has_seen_group_story_education_sheet": true,
"has_completed_username_onboarding": true,
"universal_expire_timer": {
"secs": 3600,
"nanos": 0
},
"universal_expire_timer": 3600000,
"preferred_reaction_emoji": [
"🏎️"
],