Skip to content

Commit 7547d61

Browse files
authored
Merge pull request #22 from negezor/replace-chrono-deprecate
Replace deprecated timestamp_nanos_opt method
2 parents 2f2b9de + a9adc26 commit 7547d61

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ impl<'a> Builder<'a> {
6666
return Err(Error::StartTimeAheadOfCurrentTime(start_time));
6767
}
6868

69-
to_sonyflake_time(start_time)
69+
to_sonyflake_time(start_time)?
7070
} else {
71-
to_sonyflake_time(Utc.with_ymd_and_hms(2014, 9, 1, 0, 0, 0).unwrap())
71+
to_sonyflake_time(Utc.with_ymd_and_hms(2014, 9, 1, 0, 0, 0).unwrap())?
7272
};
7373

7474
let machine_id = if let Some(machine_id) = self.machine_id {

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ pub enum Error {
2020
NoPrivateIPv4,
2121
#[error("mutex is poisoned (i.e. a panic happened while it was locked)")]
2222
MutexPoisoned,
23+
#[error("failed to get current time")]
24+
FailedToGetCurrentTime,
2325
}

src/sonyflake.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Sonyflake {
5656
pub fn next_id(&self) -> Result<u64, Error> {
5757
let mut internals = self.0.internals.lock().map_err(|_| Error::MutexPoisoned)?;
5858

59-
let current = current_elapsed_time(self.0.start_time);
59+
let current = current_elapsed_time(self.0.start_time)?;
6060
if internals.elapsed_time < current {
6161
internals.elapsed_time = current;
6262
internals.sequence = 0;
@@ -66,7 +66,7 @@ impl Sonyflake {
6666
if internals.sequence == 0 {
6767
internals.elapsed_time += 1;
6868
let overtime = internals.elapsed_time - current;
69-
thread::sleep(sleep_time(overtime));
69+
thread::sleep(sleep_time(overtime)?);
7070
}
7171
}
7272

@@ -91,17 +91,25 @@ impl Clone for Sonyflake {
9191

9292
const SONYFLAKE_TIME_UNIT: i64 = 10_000_000; // nanoseconds, i.e. 10msec
9393

94-
pub(crate) fn to_sonyflake_time(time: DateTime<Utc>) -> i64 {
95-
time.timestamp_nanos() / SONYFLAKE_TIME_UNIT
94+
pub(crate) fn to_sonyflake_time(time: DateTime<Utc>) -> Result<i64, Error> {
95+
Ok(time
96+
.timestamp_nanos_opt()
97+
.ok_or(Error::FailedToGetCurrentTime)?
98+
/ SONYFLAKE_TIME_UNIT)
9699
}
97100

98-
fn current_elapsed_time(start_time: i64) -> i64 {
99-
to_sonyflake_time(Utc::now()) - start_time
101+
fn current_elapsed_time(start_time: i64) -> Result<i64, Error> {
102+
Ok(to_sonyflake_time(Utc::now())? - start_time)
100103
}
101104

102-
fn sleep_time(overtime: i64) -> Duration {
103-
Duration::from_millis(overtime as u64 * 10)
104-
- Duration::from_nanos((Utc::now().timestamp_nanos() % SONYFLAKE_TIME_UNIT) as u64)
105+
fn sleep_time(overtime: i64) -> Result<Duration, Error> {
106+
Ok(Duration::from_millis(overtime as u64 * 10)
107+
- Duration::from_nanos(
108+
(Utc::now()
109+
.timestamp_nanos_opt()
110+
.ok_or(Error::FailedToGetCurrentTime)?
111+
% SONYFLAKE_TIME_UNIT) as u64,
112+
))
105113
}
106114

107115
pub struct DecomposedSonyflake {

src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ fn test_once() -> Result<(), BoxDynError> {
5252
#[test]
5353
fn test_run_for_10s() -> Result<(), BoxDynError> {
5454
let now = Utc::now();
55-
let start_time = to_sonyflake_time(now);
55+
let start_time = to_sonyflake_time(now)?;
5656
let sf = Sonyflake::builder().start_time(now).finalize()?;
5757

5858
let mut last_id: u64 = 0;
5959
let mut max_sequence: u64 = 0;
6060

6161
let machine_id = lower_16_bit_private_ip()? as u64;
6262

63-
let initial = to_sonyflake_time(Utc::now());
63+
let initial = to_sonyflake_time(Utc::now())?;
6464
let mut current = initial.clone();
6565
while current - initial < 1000 {
6666
let id = sf.next_id()?;
@@ -71,7 +71,7 @@ fn test_run_for_10s() -> Result<(), BoxDynError> {
7171
}
7272
last_id = id;
7373

74-
current = to_sonyflake_time(Utc::now());
74+
current = to_sonyflake_time(Utc::now())?;
7575

7676
let actual_msb = parts.msb;
7777
if actual_msb != 0 {

0 commit comments

Comments
 (0)