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

tendermint: remove tai64 crate #603

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ sha2 = { version = "0.9", default-features = false }
signature = "1.2"
subtle = "2"
subtle-encoding = { version = "0.5", features = ["bech32-preview"] }
tai64 = { version = "3", features = ["chrono"] }
thiserror = "1"
tendermint-proto = { path = "../proto" }
toml = { version = "0.5" }
Expand Down
29 changes: 8 additions & 21 deletions tendermint/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::error::{Error, Kind};

use chrono::{DateTime, SecondsFormat, Utc};
use serde::{Deserialize, Serialize};
use tai64::TAI64N;

use std::fmt;
use std::ops::{Add, Sub};
Expand All @@ -17,26 +16,26 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub struct Time(DateTime<Utc>);

impl Time {
/// Get a `Timestamp` representing the current wall clock time
/// Get [`Time`] value representing the current wall clock time
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed up some comments here too (the type used to be named [Timestamp] but was renamed to [Time] to better align with the Tendermint spec)

pub fn now() -> Self {
Time(Utc::now())
}

/// Get the `UNIX_EPOCH` time ("1970-01-01 00:00:00 UTC") as a `Timestamp`
/// Get the [`UNIX_EPOCH`] time ("1970-01-01 00:00:00 UTC") as a [`Time`]
pub fn unix_epoch() -> Self {
UNIX_EPOCH.into()
}

/// Calculate the amount of time which has passed since another `Timestamp`
/// as a `std::time::Duration`
/// Calculate the amount of time which has passed since another [`Time`]
/// as a [`std::time::Duration`]
pub fn duration_since(&self, other: Time) -> Result<Duration, Error> {
self.0
.signed_duration_since(other.0)
.to_std()
.map_err(|_| Kind::OutOfRange.into())
}

/// Parse a timestamp from an RFC 3339 date
/// Parse [`Time`] from an RFC 3339 date
pub fn parse_from_rfc3339(s: &str) -> Result<Time, Error> {
Ok(Time(DateTime::parse_from_rfc3339(s)?.with_timezone(&Utc)))
}
Expand All @@ -46,7 +45,7 @@ impl Time {
self.0.to_rfc3339_opts(SecondsFormat::Nanos, true)
}

/// Convert this timestamp to a `SystemTime`
/// Convert [`Time`] to [`SystemTime`]
pub fn to_system_time(&self) -> Result<SystemTime, Error> {
let duration_since_epoch = self.duration_since(Self::unix_epoch())?;
Ok(UNIX_EPOCH + duration_since_epoch)
Expand Down Expand Up @@ -91,18 +90,6 @@ impl From<Time> for SystemTime {
}
}

impl From<TAI64N> for Time {
fn from(t: TAI64N) -> Time {
Time(t.to_datetime_utc())
}
}

impl From<Time> for TAI64N {
fn from(t: Time) -> TAI64N {
TAI64N::from_datetime_utc(&t.0)
}
}

impl Add<Duration> for Time {
type Output = Self;

Expand All @@ -121,8 +108,8 @@ impl Sub<Duration> for Time {
}
}

/// Parse `Timestamp` from a type
/// Parse [`Time`] from a type
pub trait ParseTimestamp {
/// Parse `Timestamp`, or return an `Error` if parsing failed
/// Parse [`Time`], or return an [`Error`] if parsing failed
fn parse_timestamp(&self) -> Result<Time, Error>;
}