time.rs 729 B

12345678910111213141516171819202122
  1. use chrono::{NaiveDateTime, Utc};
  2. use crate::util::serial::{SerialDecodable, SerialEncodable};
  3. /// Wrapper struct to represent [`chrono`] UTC timestamps.
  4. #[derive(Debug, Copy, Clone, PartialEq, SerialDecodable, SerialEncodable)]
  5. pub struct Timestamp(pub i64);
  6. impl Timestamp {
  7. /// Generate a `Timestamp` of the current time.
  8. pub fn current_time() -> Self {
  9. Self(Utc::now().timestamp())
  10. }
  11. /// Calculates elapsed time of a `Timestamp`.
  12. pub fn elapsed(&self) -> u64 {
  13. let start_time = NaiveDateTime::from_timestamp(self.0, 0);
  14. let end_time = NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0);
  15. let diff = end_time - start_time;
  16. diff.num_seconds() as u64
  17. }
  18. }