-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from MyK00L/convertions
Created convert crate
- Loading branch information
Showing
6 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |