Skip to main content

grain_id/
time.rs

1use std::time::{SystemTime, UNIX_EPOCH};
2
3use super::GrainId;
4
5impl GrainId {
6    /// Creates a `GrainId` from a [`Duration`](std::time::Duration) from [`UNIX_EPOCH`] to `timestamp` based on decisecond precision.
7    ///
8    /// See [`GrainId::from_duration`] and [`GrainId::from_timestamp_since`] for more details.
9    ///
10    /// # Examples
11    ///
12    /// ```
13    /// use jiff::Timestamp;
14    /// use std::time::SystemTime;
15    /// # use grain_id::*;
16    ///
17    /// let timestamp = Timestamp::now();
18    /// let id = GrainId::from_timestamp_unix(SystemTime::try_from(timestamp).unwrap());
19    /// assert_eq!(id.to_u64(), ((timestamp.as_millisecond() / 100) as u64));
20    /// ```
21    pub fn from_timestamp_unix<T>(timestamp: T) -> Self
22    where
23        T: Into<SystemTime>,
24    {
25        Self::from_timestamp_since(timestamp, UNIX_EPOCH)
26    }
27    /// Creates a `GrainId` from a [`Duration`](std::time::Duration) from `base_time` to `timestamp` based on decisecond precision.
28    ///
29    /// See [`GrainId::from_duration`] for more details.
30    ///
31    /// # Behavior when `timestamp` is earlier than `base_time`
32    ///
33    /// If `timestamp` is earlier than `base_time`, Returns the value obtained by wrapping substracting the absolute value of the duration from [`NIL`](GrainId::NIL)
34    ///
35    /// # Examples
36    ///
37    /// ```
38    /// use jiff::Timestamp;
39    /// use std::time::SystemTime;
40    /// # use grain_id::*;
41    ///
42    /// let base_time: Timestamp = "2025-01-01T00:00:00Z".parse().unwrap();
43    /// let timestamp: Timestamp = "2025-11-28T12:34:56.000789Z".parse().unwrap();
44    /// let id = GrainId::from_timestamp_since(
45    ///     SystemTime::try_from(timestamp).unwrap(),
46    ///     SystemTime::try_from(base_time).unwrap(),
47    /// );
48    /// assert_eq!(
49    ///     id.to_u64(),
50    ///     ((timestamp.duration_since(base_time).as_millis() / 100) as u64)
51    /// );
52    /// ```
53    pub fn from_timestamp_since<T, U>(timestamp: T, base_time: U) -> Self
54    where
55        T: Into<SystemTime>,
56        U: Into<SystemTime>,
57    {
58        match timestamp.into().duration_since(base_time.into()) {
59            Ok(x) => Self::from_duration(x),
60            Err(x) => Self::NIL.wrapping_sub(Self::from_duration(x.duration())),
61        }
62    }
63
64    /// Creates a `GrainId` from a [`Duration`](std::time::Duration) from [`UNIX_EPOCH`] to [`SystemTime::now()`] based on decisecond precision.
65    ///
66    /// See [`GrainId::from_duration`] and [`GrainId::from_timestamp_since`] for more details.
67    pub fn now_unix() -> Self {
68        Self::from_timestamp_unix(SystemTime::now())
69    }
70
71    /// Creates a `GrainId` from a [`Duration`](std::time::Duration) from `base_time` to [`SystemTime::now()`] based on decisecond precision.
72    ///
73    /// See [`GrainId::from_duration`] and [`GrainId::from_timestamp_since`] for more details.
74    pub fn now_since<T>(base_time: T) -> Self
75    where
76        T: Into<SystemTime>,
77    {
78        Self::from_timestamp_since(SystemTime::now(), base_time)
79    }
80}