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

added implementations for Duration to chrono #35

Merged
merged 4 commits into from
Apr 13, 2023
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
36 changes: 36 additions & 0 deletions wkt-types/src/pbtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ impl From<Timestamp> for DateTime<Utc> {
}
}

/// Converts proto duration to chrono's Duration
impl From<Duration> for chrono::Duration {
fn from(val: Duration) -> Self {
chrono::Duration::seconds(val.seconds) + chrono::Duration::nanoseconds(val.nanos as i64)
}
}

/// Converts chrono Duration to proto duration
impl From<chrono::Duration> for Duration {
fn from(val: chrono::Duration) -> Self {
Duration {
seconds: val.num_seconds(),
nanos: (val.num_nanoseconds().unwrap() % 1_000_000_000) as i32,
}
}
}

impl Serialize for Timestamp {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
Expand Down Expand Up @@ -120,4 +137,23 @@ mod tests {

println!("{datetime_utc:?}");
}

#[test]
fn test_duration_conversion_pb_to_chrono() {
let duration = Duration {
seconds: 10,
nanos: 100,
};
let chrono_duration: chrono::Duration = duration.into();
assert_eq!(chrono_duration.num_seconds(), 10);
assert_eq!((chrono_duration - chrono::Duration::seconds(10)).num_nanoseconds(), Some(100));
}

#[test]
fn test_duration_conversion_chrono_to_pb() {
let chrono_duration = chrono::Duration::seconds(10) + chrono::Duration::nanoseconds(100);
let duration: Duration = chrono_duration.into();
assert_eq!(duration.seconds, 10);
assert_eq!(duration.nanos, 100);
}
}