glean_core/metrics/
time_unit.rs1use std::time::Duration;
6
7use serde::{Deserialize, Serialize};
8
9use crate::error::{Error, ErrorKind};
10
11#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
14#[serde(rename_all = "lowercase")]
15#[repr(i32)] pub enum TimeUnit {
17 Nanosecond,
19 Microsecond,
21 Millisecond,
23 Second,
25 Minute,
27 Hour,
29 Day,
31}
32
33impl TimeUnit {
34 pub fn format_pattern(self) -> &'static str {
36 use TimeUnit::*;
37 match self {
38 Nanosecond => "%Y-%m-%dT%H:%M:%S%.f%:z",
39 Microsecond => "%Y-%m-%dT%H:%M:%S%.6f%:z",
40 Millisecond => "%Y-%m-%dT%H:%M:%S%.3f%:z",
41 Second => "%Y-%m-%dT%H:%M:%S%:z",
42 Minute => "%Y-%m-%dT%H:%M%:z",
43 Hour => "%Y-%m-%dT%H%:z",
44 Day => "%Y-%m-%d%:z",
45 }
46 }
47
48 pub fn duration_convert(self, duration: Duration) -> u64 {
58 use TimeUnit::*;
59 match self {
60 Nanosecond => duration.as_nanos() as u64,
61 Microsecond => duration.as_micros() as u64,
62 Millisecond => duration.as_millis() as u64,
63 Second => duration.as_secs(),
64 Minute => duration.as_secs() / 60,
65 Hour => duration.as_secs() / 60 / 60,
66 Day => duration.as_secs() / 60 / 60 / 24,
67 }
68 }
69
70 pub fn as_nanos(self, duration: u64) -> u64 {
80 use TimeUnit::*;
81 let duration = match self {
82 Nanosecond => Duration::from_nanos(duration),
83 Microsecond => Duration::from_micros(duration),
84 Millisecond => Duration::from_millis(duration),
85 Second => Duration::from_secs(duration),
86 Minute => Duration::from_secs(duration * 60),
87 Hour => Duration::from_secs(duration * 60 * 60),
88 Day => Duration::from_secs(duration * 60 * 60 * 24),
89 };
90
91 duration.as_nanos() as u64
92 }
93}
94
95impl TryFrom<i32> for TimeUnit {
102 type Error = Error;
103
104 fn try_from(value: i32) -> Result<TimeUnit, Self::Error> {
105 match value {
106 0 => Ok(TimeUnit::Nanosecond),
107 1 => Ok(TimeUnit::Microsecond),
108 2 => Ok(TimeUnit::Millisecond),
109 3 => Ok(TimeUnit::Second),
110 4 => Ok(TimeUnit::Minute),
111 5 => Ok(TimeUnit::Hour),
112 6 => Ok(TimeUnit::Day),
113 e => Err(ErrorKind::TimeUnit(e).into()),
114 }
115 }
116}