time.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use std::time::UNIX_EPOCH;
  2. use chrono::{NaiveDateTime, Utc};
  3. use serde::{Deserialize, Serialize};
  4. use crate::{
  5. serial::{SerialDecodable, SerialEncodable},
  6. Result,
  7. };
  8. /// Wrapper struct to represent [`chrono`] UTC timestamps.
  9. #[derive(
  10. Clone,
  11. Copy,
  12. Debug,
  13. Serialize,
  14. Deserialize,
  15. SerialEncodable,
  16. SerialDecodable,
  17. PartialEq,
  18. PartialOrd,
  19. Eq,
  20. )]
  21. pub struct Timestamp(pub i64);
  22. impl Timestamp {
  23. /// Generate a `Timestamp` of the current time.
  24. pub fn current_time() -> Self {
  25. Self(Utc::now().timestamp())
  26. }
  27. /// Calculates elapsed time of a `Timestamp`.
  28. pub fn elapsed(&self) -> u64 {
  29. let start_time = NaiveDateTime::from_timestamp(self.0, 0);
  30. let end_time = NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0);
  31. let diff = end_time - start_time;
  32. diff.num_seconds() as u64
  33. }
  34. /// Increment a 'Timestamp'.
  35. pub fn add(&mut self, inc: i64) {
  36. self.0 += inc;
  37. }
  38. }
  39. impl std::fmt::Display for Timestamp {
  40. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  41. let date = timestamp_to_date(self.0, DateFormat::DateTime);
  42. write!(f, "{}", date)
  43. }
  44. }
  45. #[derive(
  46. Clone,
  47. Copy,
  48. Debug,
  49. Serialize,
  50. Deserialize,
  51. SerialEncodable,
  52. SerialDecodable,
  53. PartialEq,
  54. PartialOrd,
  55. Eq,
  56. )]
  57. pub struct NanoTimestamp(pub i64);
  58. impl NanoTimestamp {
  59. pub fn current_time() -> Self {
  60. Self(Utc::now().timestamp_nanos())
  61. }
  62. }
  63. impl std::fmt::Display for NanoTimestamp {
  64. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  65. let date = timestamp_to_date(self.0, DateFormat::Nanos);
  66. write!(f, "{}", date)
  67. }
  68. }
  69. pub enum DateFormat {
  70. Default,
  71. Date,
  72. DateTime,
  73. Nanos,
  74. }
  75. pub fn timestamp_to_date(timestamp: i64, format: DateFormat) -> String {
  76. if timestamp <= 0 {
  77. return "".to_string()
  78. }
  79. match format {
  80. DateFormat::Date => {
  81. NaiveDateTime::from_timestamp(timestamp, 0).date().format("%-d %b").to_string()
  82. }
  83. DateFormat::DateTime => {
  84. NaiveDateTime::from_timestamp(timestamp, 0).format("%H:%M:%S %A %-d %B").to_string()
  85. }
  86. DateFormat::Nanos => {
  87. const A_BILLION: i64 = 1_000_000_000;
  88. NaiveDateTime::from_timestamp(timestamp / A_BILLION, (timestamp % A_BILLION) as u32)
  89. .format("%H:%M:%S.%f")
  90. .to_string()
  91. }
  92. DateFormat::Default => "".to_string(),
  93. }
  94. }
  95. pub fn unix_timestamp() -> Result<u64> {
  96. Ok(UNIX_EPOCH.elapsed()?.as_secs())
  97. }