Skip to content

Commit

Permalink
Merge pull request #6 from MyK00L/convertions
Browse files Browse the repository at this point in the history
Created convert crate
  • Loading branch information
eutampieri authored Aug 22, 2021
2 parents f941903 + 295cea4 commit 6b9fca0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["protos", "contest_service", "rpctest-client", "rpctest-server"]
members = ["protos", "contest_service", "rpctest-client", "rpctest-server", "convert"]
1 change: 1 addition & 0 deletions contest_service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM rustlang/rust:nightly as base
WORKDIR /src
COPY protos protos
COPY convert convert
WORKDIR /src/server
COPY contest_service/dummy.rs .
COPY contest_service/Cargo.toml .
Expand Down
10 changes: 10 additions & 0 deletions convert/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "convert"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
prost-types = "0.8"
bson = "2.0.0-beta.3"
8 changes: 8 additions & 0 deletions convert/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(duration_constants)]

//! Convertion utilities between protobuf and mongodb
pub mod mongo;
//pub mod proto;

#[cfg(test)]
mod tests;
14 changes: 14 additions & 0 deletions convert/src/mongo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Convert MongoDB timestamps to Rust's SystemTime
pub fn timestamp_to_systime(ts: bson::Timestamp) -> std::time::SystemTime {
std::time::UNIX_EPOCH + std::time::Duration::from_secs(ts.time.into())
}

/// Convert a SystemTime to a MongoDB timestamp. Note that MongoDB stores unix timestamps in 32 bits
pub fn systime_to_timestamp(st: std::time::SystemTime) -> bson::Timestamp {
let nano_duration = st.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let seconds = (nano_duration / std::time::Duration::SECOND.as_nanos()) as u32; // This will overflow in 2106
bson::Timestamp {
time: seconds,
increment: 0,
}
}
37 changes: 37 additions & 0 deletions convert/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use super::*;

fn get_test_time() -> std::time::SystemTime {
std::time::UNIX_EPOCH
+ std::time::Duration::from_secs(10)
+ std::time::Duration::from_nanos(101)
}
#[test]
fn convert_to_mongo_timestamp_and_back_test() {
let now = std::time::UNIX_EPOCH
+ std::time::Duration::from_secs(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
); //This is needed because we need to discard anything below the second
let mongo_now = mongo::systime_to_timestamp(now);
assert_eq!(now, mongo::timestamp_to_systime(mongo_now));
}

#[test]
fn convert_to_mongo_timestamp_test() {
let test_time = get_test_time();
let mongo_test_time = mongo::systime_to_timestamp(test_time);
assert_eq!(mongo_test_time.time, 10);
assert_eq!(mongo_test_time.increment, 0);
}

#[test]
fn convert_from_mongo_timestamp_test() {
let test_time = get_test_time() - std::time::Duration::from_nanos(101);
let mongo_test_time = bson::Timestamp {
time: 10,
increment: 0,
};
assert_eq!(mongo::timestamp_to_systime(mongo_test_time), test_time);
}

0 comments on commit 6b9fca0

Please sign in to comment.