Skip to content

Commit

Permalink
switch from time to systemtime
Browse files Browse the repository at this point in the history
fix compile failure, clippy lints, check of cli in ci

handle SystemTimes before UNIX_EPOCH by truncating
  • Loading branch information
esheppa committed Jul 29, 2022
1 parent 96aa617 commit 7a45e51
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 62 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: cargo check --package ulid-cli
- run: cargo test
- run: cargo test --all-features
- run: cargo test --no-default-features --features=std
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ repository = "https://github.com/dylanhart/ulid-rs"

[features]
default = ["std"]
std = ["rand", "time"]
std = ["rand"]

[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }
rand = { version = "0.8", optional = true }
# -- Sync versions with the WASM target below --
time = { version = "0.3", optional = true }
uuid = { version = "1.1", optional = true }

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bencher::{benchmark_group, benchmark_main, Bencher};
use time::OffsetDateTime;
use std::time::SystemTime;
use ulid::{Generator, Ulid, ULID_LEN};

fn bench_new(b: &mut Bencher) {
b.iter(|| Ulid::new());
b.iter(Ulid::new);
}

fn bench_generator_generate(b: &mut Bencher) {
Expand All @@ -12,7 +12,7 @@ fn bench_generator_generate(b: &mut Bencher) {
}

fn bench_from_time(b: &mut Bencher) {
let time = OffsetDateTime::now_utc();
let time = SystemTime::now();
b.iter(|| Ulid::from_datetime(time));
}

Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ edition = "2018"
[dependencies]
structopt = "0.2"
ulid = { version = "*", path = ".." }
time = "0.3.11"
4 changes: 2 additions & 2 deletions cli/src/bin/ulid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn generate(count: u32, monotonic: bool) {

fn inspect(values: &[String]) {
for val in values {
let ulid = Ulid::from_string(&val);
let ulid = Ulid::from_string(val);
match ulid {
Ok(ulid) => {
let upper_hex = format!("{:X}", ulid.0);
Expand All @@ -81,7 +81,7 @@ COMPONENTS:
",
ulid.to_string(),
upper_hex,
ulid.datetime(),
time::OffsetDateTime::from(ulid.datetime()),
ulid.timestamp_ms(),
upper_hex.chars().skip(6).collect::<String>()
);
Expand Down
38 changes: 20 additions & 18 deletions src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use time::OffsetDateTime;
use std::time::{Duration, SystemTime};

use std::fmt;

Expand Down Expand Up @@ -42,7 +42,7 @@ impl Generator {
/// assert!(ulid1 < ulid2);
/// ```
pub fn generate(&mut self) -> Result<Ulid, MonotonicError> {
let now = OffsetDateTime::now_utc();
let now = SystemTime::now();
self.generate_from_datetime(now)
}

Expand All @@ -53,9 +53,9 @@ impl Generator {
/// # Example
/// ```rust
/// use ulid::Generator;
/// use time::OffsetDateTime;
/// use std::time::SystemTime;
///
/// let dt = OffsetDateTime::now_utc();
/// let dt = SystemTime::now();
/// let mut gen = Generator::new();
///
/// let ulid1 = gen.generate_from_datetime(dt).unwrap();
Expand All @@ -64,10 +64,7 @@ impl Generator {
/// assert_eq!(ulid1.datetime(), ulid2.datetime());
/// assert!(ulid1 < ulid2);
/// ```
pub fn generate_from_datetime(
&mut self,
datetime: OffsetDateTime,
) -> Result<Ulid, MonotonicError> {
pub fn generate_from_datetime(&mut self, datetime: SystemTime) -> Result<Ulid, MonotonicError> {
self.generate_from_datetime_with_source(datetime, &mut rand::thread_rng())
}

Expand All @@ -79,7 +76,7 @@ impl Generator {
/// ```rust
/// use ulid::Generator;
/// use ulid::Ulid;
/// use time::OffsetDateTime;
/// use std::time::SystemTime;
/// use rand::prelude::*;
///
/// let mut rng = StdRng::from_entropy();
Expand All @@ -94,7 +91,7 @@ impl Generator {
where
R: rand::Rng,
{
let now = OffsetDateTime::now_utc();
let now = SystemTime::now();
self.generate_from_datetime_with_source(now, source)
}

Expand All @@ -105,10 +102,10 @@ impl Generator {
/// # Example
/// ```rust
/// use ulid::Generator;
/// use time::OffsetDateTime;
/// use std::time::SystemTime;
/// use rand::prelude::*;
///
/// let dt = OffsetDateTime::now_utc();
/// let dt = SystemTime::now();
/// let mut rng = StdRng::from_entropy();
/// let mut gen = Generator::new();
///
Expand All @@ -120,16 +117,21 @@ impl Generator {
/// ```
pub fn generate_from_datetime_with_source<R>(
&mut self,
datetime: OffsetDateTime,
datetime: SystemTime,
source: &mut R,
) -> Result<Ulid, MonotonicError>
where
R: rand::Rng,
{
let last_ms = self.previous.timestamp_ms() as i128;
let last_ms = self.previous.timestamp_ms();
// maybe time went backward, or it is the same ms.
// increment instead of generating a new random so that it is monotonic
if (datetime.unix_timestamp_nanos() / 1_000_000) <= last_ms {
if datetime
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or(Duration::ZERO)
.as_millis()
<= u128::from(last_ms)
{
if let Some(next) = self.previous.increment() {
self.previous = next;
return Ok(next);
Expand Down Expand Up @@ -170,15 +172,15 @@ impl fmt::Display for MonotonicError {
#[cfg(test)]
mod tests {
use super::*;
use ::time::Duration;
use std::time::Duration;

#[test]
fn test_order_monotonic() {
let dt = OffsetDateTime::now_utc();
let dt = SystemTime::now();
let mut gen = Generator::new();
let ulid1 = gen.generate_from_datetime(dt).unwrap();
let ulid2 = gen.generate_from_datetime(dt).unwrap();
let ulid3 = Ulid::from_datetime(dt + Duration::milliseconds(1));
let ulid3 = Ulid::from_datetime(dt + Duration::from_millis(1));
assert_eq!(ulid1.0 + 1, ulid2.0);
assert!(ulid2 < ulid3);
assert!(ulid2.timestamp_ms() < ulid3.timestamp_ms())
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ struct ReadMeDoctest;

mod base32;
#[cfg(feature = "std")]
mod time;
#[cfg(feature = "std")]
mod generator;
#[cfg(feature = "serde")]
pub mod serde;
#[cfg(feature = "std")]
mod time;
#[cfg(feature = "uuid")]
mod uuid;

Expand Down Expand Up @@ -145,13 +145,13 @@ impl Ulid {
/// # Example
/// ```rust
/// # #[cfg(feature = "std")] {
/// use ::time::OffsetDateTime;
/// use std::time::{SystemTime, Duration};
/// use ulid::Ulid;
///
/// let dt = OffsetDateTime::now_utc();
/// let dt = SystemTime::now();
/// let ulid = Ulid::from_datetime(dt);
///
/// assert_eq!(ulid.timestamp_ms(), (dt.unix_timestamp_nanos() / 1_000_000) as u64);
/// assert_eq!(u128::from(ulid.timestamp_ms()), dt.duration_since(SystemTime::UNIX_EPOCH).unwrap_or(Duration::ZERO).as_millis());
/// # }
/// ```
pub const fn timestamp_ms(&self) -> u64 {
Expand Down
86 changes: 54 additions & 32 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use ::time::OffsetDateTime;

use crate::{bitmask, Ulid};
use std::time::{Duration, SystemTime};

impl Ulid {
/// Creates a new Ulid with the current time (UTC)
Expand All @@ -12,7 +11,7 @@ impl Ulid {
/// let my_ulid = Ulid::new();
/// ```
pub fn new() -> Ulid {
Ulid::from_datetime(OffsetDateTime::now_utc())
Ulid::from_datetime(SystemTime::now())
}

/// Creates a new Ulid using data from the given random number generator
Expand All @@ -26,40 +25,49 @@ impl Ulid {
/// let ulid = Ulid::with_source(&mut rng);
/// ```
pub fn with_source<R: rand::Rng>(source: &mut R) -> Ulid {
Ulid::from_datetime_with_source(OffsetDateTime::now_utc(), source)
Ulid::from_datetime_with_source(SystemTime::now(), source)
}

/// Creates a new Ulid with the given datetime
///
/// This can be useful when migrating data to use Ulid identifiers
/// This can be useful when migrating data to use Ulid identifiers.
///
/// This will take the maximum of the `[SystemTime]` argument and `[SystemTime::UNIX_EPOCH]`
/// as earlier times are not valid for a Ulid timestamp
///
/// # Example
/// ```rust
/// use time::OffsetDateTime;
/// use std::time::{SystemTime, Duration};
/// use ulid::Ulid;
///
/// let ulid = Ulid::from_datetime(OffsetDateTime::now_utc());
/// let ulid = Ulid::from_datetime(SystemTime::now());
/// ```
pub fn from_datetime(datetime: OffsetDateTime) -> Ulid {
pub fn from_datetime(datetime: SystemTime) -> Ulid {
Ulid::from_datetime_with_source(datetime, &mut rand::thread_rng())
}

/// Creates a new Ulid with the given datetime and random number generator
///
/// This will take the maximum of the `[SystemTime]` argument and `[SystemTime::UNIX_EPOCH]`
/// as earlier times are not valid for a Ulid timestamp
///
/// # Example
/// ```rust
/// use time::OffsetDateTime;
/// use std::time::{SystemTime, Duration};
/// use rand::prelude::*;
/// use ulid::Ulid;
///
/// let mut rng = StdRng::from_entropy();
/// let ulid = Ulid::from_datetime_with_source(OffsetDateTime::now_utc(), &mut rng);
/// let ulid = Ulid::from_datetime_with_source(SystemTime::now(), &mut rng);
/// ```
pub fn from_datetime_with_source<R>(datetime: OffsetDateTime, source: &mut R) -> Ulid
pub fn from_datetime_with_source<R>(datetime: SystemTime, source: &mut R) -> Ulid
where
R: rand::Rng,
{
let timestamp = datetime.unix_timestamp_nanos() / 1_000_000;
let timestamp = datetime
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or(Duration::ZERO)
.as_millis();
let timebits = (timestamp & bitmask!(Self::TIME_BITS)) as u64;

let msb = timebits << 16 | u64::from(source.gen::<u16>());
Expand All @@ -71,30 +79,26 @@ impl Ulid {
///
/// # Example
/// ```rust
/// use time::Duration;
/// use time::OffsetDateTime;
/// use std::time::{SystemTime, Duration};
/// use ulid::Ulid;
///
/// let dt = OffsetDateTime::now_utc();
/// let dt = SystemTime::now();
/// let ulid = Ulid::from_datetime(dt);
///
/// assert!((dt - ulid.datetime()) < Duration::milliseconds(1));
/// assert!(
/// dt + Duration::from_millis(1) >= ulid.datetime()
/// && dt - Duration::from_millis(1) <= ulid.datetime()
/// );
/// ```
pub fn datetime(&self) -> OffsetDateTime {
pub fn datetime(&self) -> SystemTime {
let stamp = self.timestamp_ms();
let secs = stamp / 1000;
let millis = stamp % 1000;
OffsetDateTime::from_unix_timestamp_nanos(
((secs * 1_000_000_000) + (millis * 1_000_000)) as i128,
)
.expect("Seconds and Milliseconds are out of range")
SystemTime::UNIX_EPOCH + Duration::from_millis(stamp)
}
}

#[cfg(test)]
mod tests {
use super::*;
use ::time::Duration;

#[test]
fn test_dynamic() {
Expand All @@ -114,7 +118,7 @@ mod tests {
let mut source = StepRng::new(123, 0);

let u1 = Ulid::with_source(&mut source);
let dt = OffsetDateTime::now_utc() + Duration::milliseconds(1);
let dt = SystemTime::now() + Duration::from_millis(1);
let u2 = Ulid::from_datetime_with_source(dt, &mut source);
let u3 = Ulid::from_datetime_with_source(dt, &mut source);

Expand All @@ -124,33 +128,51 @@ mod tests {

#[test]
fn test_order() {
let dt = OffsetDateTime::now_utc();
let dt = SystemTime::now();
let ulid1 = Ulid::from_datetime(dt);
let ulid2 = Ulid::from_datetime(dt + Duration::milliseconds(1));
let ulid2 = Ulid::from_datetime(dt + Duration::from_millis(1));
assert!(ulid1 < ulid2);
}

#[test]
fn test_datetime() {
let dt = OffsetDateTime::now_utc();
let dt = SystemTime::now();
let ulid = Ulid::from_datetime(dt);

println!("{:?}, {:?}", dt, ulid.datetime());
assert!(ulid.datetime() <= dt);
assert!(ulid.datetime() + Duration::milliseconds(1) >= dt);
assert!(ulid.datetime() + Duration::from_millis(1) >= dt);
}

#[test]
fn test_timestamp() {
let dt = OffsetDateTime::now_utc();
let dt = SystemTime::now();
let ulid = Ulid::from_datetime(dt);
let ts = dt.unix_timestamp() as u64 * 1000 + dt.millisecond() as u64;
let ts = dt
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis();

assert_eq!(ulid.timestamp_ms(), ts);
assert_eq!(u128::from(ulid.timestamp_ms()), ts);
}

#[test]
fn default_is_nil() {
assert_eq!(Ulid::default(), Ulid::nil());
}

#[test]
fn nil_is_at_unix_epoch() {
assert_eq!(Ulid::nil().datetime(), SystemTime::UNIX_EPOCH);
}

#[test]
fn truncates_at_unix_epoch() {
let before_epoch = SystemTime::UNIX_EPOCH - Duration::from_secs(100);
assert!(before_epoch < SystemTime::UNIX_EPOCH);
assert_eq!(
Ulid::from_datetime(before_epoch).datetime(),
SystemTime::UNIX_EPOCH
);
}
}

0 comments on commit 7a45e51

Please sign in to comment.