glean_core/metrics/
time_unit.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::time::Duration;
6
7use serde::{Deserialize, Serialize};
8
9use crate::error::{Error, ErrorKind};
10
11/// Different resolutions supported by the time related
12/// metric types (e.g. DatetimeMetric).
13#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
14#[serde(rename_all = "lowercase")]
15#[repr(i32)] // use i32 to be compatible with our JNA definition
16pub enum TimeUnit {
17    /// Truncate to nanosecond precision.
18    Nanosecond,
19    /// Truncate to microsecond precision.
20    Microsecond,
21    /// Truncate to millisecond precision.
22    Millisecond,
23    /// Truncate to second precision.
24    Second,
25    /// Truncate to minute precision.
26    Minute,
27    /// Truncate to hour precision.
28    Hour,
29    /// Truncate to day precision.
30    Day,
31}
32
33impl TimeUnit {
34    /// Formats the given time unit, truncating the time if needed.
35    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    /// Converts a duration to the requested time unit.
49    ///
50    /// # Arguments
51    ///
52    /// * `duration` - the duration to convert.
53    ///
54    /// # Returns
55    ///
56    /// The integer representation of the converted duration.
57    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    /// Converts a duration in the given unit to nanoseconds.
71    ///
72    /// # Arguments
73    ///
74    /// * `duration` - the duration to convert.
75    ///
76    /// # Returns
77    ///
78    /// The integer representation of the nanosecond duration.
79    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
95/// Trait implementation for converting an integer value to a TimeUnit.
96///
97/// This is used in the FFI code.
98///
99/// Please note that values should match the ordering of the
100/// platform specific side of things (e.g. Kotlin implementation).
101impl 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}