summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-11-20 19:02:34 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-11-21 16:25:30 +0100
commitb251f9e8857fba284f515061ac62013519997e30 (patch)
tree8cb3501b8cb32e43496e56dd76446ba7559c7eed /store
parent13c437470cc7377d7b761b5bb9b8d4efb0ba385e (diff)
openpgp: Replace time crate with std::time.
- In sq and sqv, use chrono to interface with the user. - Fixes #341.
Diffstat (limited to 'store')
-rw-r--r--store/Cargo.toml1
-rw-r--r--store/src/backend/mod.rs22
-rw-r--r--store/src/backend/support.rs38
-rw-r--r--store/src/lib.rs39
4 files changed, 47 insertions, 53 deletions
diff --git a/store/Cargo.toml b/store/Cargo.toml
index 2389ea4a..d3eb0272 100644
--- a/store/Cargo.toml
+++ b/store/Cargo.toml
@@ -36,7 +36,6 @@ failure = "0.1.2"
futures = "0.1.17"
rand = { version = "0.7", default-features = false }
rusqlite = "0.19"
-time = "0.1.38"
tokio-core = "0.1.10"
tokio-io = "0.1.4"
diff --git a/store/src/backend/mod.rs b/store/src/backend/mod.rs
index f81e6eb0..1ed41288 100644
--- a/store/src/backend/mod.rs
+++ b/store/src/backend/mod.rs
@@ -5,7 +5,7 @@ use std::cmp;
use std::fmt;
use std::io;
use std::rc::Rc;
-use time::Duration;
+use std::time::Duration;
use capnp::capability::Promise;
use capnp;
@@ -47,21 +47,21 @@ mod log;
/// Minimum sleep time.
fn min_sleep_time() -> Duration {
- Duration::minutes(5)
+ Duration::new(5 * 60, 0)
}
/// Interval after which all keys should be refreshed once.
fn refresh_interval() -> Duration {
- Duration::weeks(1)
+ Duration::new(1 * 7 * 24 * 60 * 60, 0)
}
/// Returns a value from the uniform distribution over [0, 2*d).
///
/// This function is used to randomize key refresh times.
fn random_duration(d: Duration) -> Duration {
- let s = Uniform::from(0..2 * d.num_seconds())
+ let s = Uniform::from(0..2 * d.as_secs())
.sample(&mut thread_rng());
- Duration::seconds(s)
+ Duration::new(s, 0)
}
/* Entry point. */
@@ -873,7 +873,7 @@ impl KeyServer {
/// Returns the number of keys using the given policy.
fn need_update(c: &Rc<Connection>, network_policy: core::NetworkPolicy)
- -> Result<i32> {
+ -> Result<u32> {
let network_policy_u8 = u8::from(&network_policy);
let count: i64 = c.query_row(
@@ -883,7 +883,7 @@ impl KeyServer {
WHERE mappings.network_policy >= ?1",
&[&network_policy_u8], |row| row.get(0))?;
assert!(count >= 0);
- Ok(count as i32)
+ Ok(count as u32)
}
/// Helper for `update`.
@@ -953,7 +953,8 @@ impl KeyServer {
}))
} else {
assert!(at > now);
- Box::new(future::ok(cmp::max(min_sleep_time(), at - now)))
+ Box::new(future::ok(cmp::max(min_sleep_time(),
+ now.duration_since(at).unwrap())))
}
}
@@ -969,10 +970,7 @@ impl KeyServer {
Self::update(&c, network_policy)
.then(move |d| {
let d = d.unwrap_or(min_sleep_time());
- Timeout::new(
- ::std::time::Duration::new(random_duration(d)
- .num_seconds() as u64, 0),
- &h1)
+ Timeout::new(random_duration(d), &h1)
.unwrap() // XXX: May fail if the eventloop expired.
.then(move |timeout| {
if timeout.is_ok() {
diff --git a/store/src/backend/support.rs b/store/src/backend/support.rs
index f55bed40..5cd44ad5 100644
--- a/store/src/backend/support.rs
+++ b/store/src/backend/support.rs
@@ -3,8 +3,12 @@
use rusqlite;
use rusqlite::types::{ToSql, ToSqlOutput, FromSql, FromSqlResult, ValueRef};
use std::fmt;
-use std::ops::{Add, Sub};
-use time::{Timespec, Duration, now_utc};
+use std::ops::Add;
+use std::time::{Duration, SystemTime, UNIX_EPOCH};
+
+use crate::{
+ Result,
+};
/// Represents a row id.
///
@@ -52,29 +56,41 @@ impl FromSql for ID {
/// A serializable system time.
+///
+/// XXX: Drop this. Instead, use chrono::DateTime which implements
+/// ToSql and FromSql.
#[derive(Clone, Copy, PartialEq, PartialOrd)]
-pub struct Timestamp(Timespec);
+pub struct Timestamp(SystemTime);
impl Timestamp {
pub fn now() -> Self {
- Timestamp(now_utc().to_timespec())
+ Timestamp(SystemTime::now())
}
/// Converts to unix time.
pub fn unix(&self) -> i64 {
- self.0.sec
+ match self.0.duration_since(UNIX_EPOCH) {
+ Ok(d) if d.as_secs() < std::i64::MAX as u64 =>
+ d.as_secs() as i64,
+ _ => 0, // Not representable.
+ }
+ }
+
+ pub fn duration_since(&self, earlier: Timestamp) -> Result<Duration> {
+ Ok(self.0.duration_since(earlier.0)?)
}
}
impl ToSql for Timestamp {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> {
- Ok(ToSqlOutput::from(self.0.sec))
+ Ok(ToSqlOutput::from(self.unix()))
}
}
impl FromSql for Timestamp {
fn column_result(value: ValueRef) -> FromSqlResult<Self> {
- value.as_i64().map(|t| Timestamp(Timespec::new(t, 0)))
+ value.as_i64()
+ .map(|t| Timestamp(UNIX_EPOCH + Duration::new(t as u64, 0)))
}
}
@@ -85,11 +101,3 @@ impl Add<Duration> for Timestamp {
Timestamp(self.0 + other)
}
}
-
-impl Sub<Timestamp> for Timestamp {
- type Output = Duration;
-
- fn sub(self, other: Self) -> Self::Output {
- self.0 - other.0
- }
-}
diff --git a/store/src/lib.rs b/store/src/lib.rs
index 59e73063..c24cd0d3 100644
--- a/store/src/lib.rs
+++ b/store/src/lib.rs
@@ -58,7 +58,6 @@ extern crate failure;
extern crate futures;
extern crate rand;
extern crate rusqlite;
-extern crate time;
extern crate tokio_core;
extern crate tokio_io;
@@ -69,7 +68,7 @@ use std::rc::Rc;
use capnp::capability::Promise;
use capnp_rpc::rpc_twoparty_capnp::Side;
use futures::{Future};
-use time::Timespec;
+use std::time;
use tokio_core::reactor::Core;
extern crate sequoia_openpgp as openpgp;
@@ -924,12 +923,14 @@ impl Key {
}
-/// Returns `t` as Timespec.
-fn from_unix(t: i64) -> Option<Timespec> {
+/// Returns `t` as time::SystemTime.
+fn from_unix(t: i64) -> Option<time::SystemTime> {
if t == 0 {
None
} else {
- Some(Timespec::new(t, 0))
+ // XXX: Backend and frontend should really communicate
+ // unsigned timestamps.
+ Some(time::UNIX_EPOCH + time::Duration::new(t as u64, 0))
}
}
@@ -941,10 +942,10 @@ fn from_unix(t: i64) -> Option<Timespec> {
#[derive(Debug)]
pub struct Stats {
/// Records the time this item was created.
- pub created: Option<Timespec>,
+ pub created: Option<time::SystemTime>,
/// Records the time this item was last updated.
- pub updated: Option<Timespec>,
+ pub updated: Option<time::SystemTime>,
/// Records counters and timestamps of encryptions.
pub encryption: Stamps,
@@ -957,7 +958,7 @@ pub struct Stats {
#[derive(Debug)]
pub struct Log {
/// Records the time of the entry.
- pub timestamp: Timespec,
+ pub timestamp: time::SystemTime,
/// Relates the entry to a mapping.
pub mapping: Option<Mapping>,
@@ -1016,20 +1017,6 @@ impl Log {
Err((ref m, ref e)) => format!("{}: {}: {}", self.slug, m, e),
}
}
-
- /// Returns the message with timestamp and context.
- pub fn full(&self) -> String {
- let timestamp =
- time::strftime("%F %H:%M", &time::at(self.timestamp))
- .unwrap(); // Only parse errors can happen.
-
- match self.status {
- Ok(ref m) => format!(
- "{}: {}: {}", timestamp, self.slug, m),
- Err((ref m, ref e)) => format!(
- "{}: {}: {}: {}", timestamp, self.slug, m, e),
- }
- }
}
/// Counter and timestamps.
@@ -1039,14 +1026,16 @@ pub struct Stamps {
pub count: usize,
/// Records the time when this has been used first.
- pub first: Option<Timespec>,
+ pub first: Option<time::SystemTime>,
/// Records the time when this has been used last.
- pub last: Option<Timespec>,
+ pub last: Option<time::SystemTime>,
}
impl Stamps {
- fn new(count: i64, first: Option<Timespec>, last: Option<Timespec>) -> Self {
+ fn new(count: i64,
+ first: Option<time::SystemTime>,
+ last: Option<time::SystemTime>) -> Self {
Stamps {
count: count as usize,
first: first,