| 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 |
|
| 5 |
use std::time::Duration; |
| 6 |
|
| 7 |
use malloc_size_of_derive::MallocSizeOf; |
| 8 |
use serde::{Deserialize, Serialize}; |
| 9 |
|
| 10 |
use crate::error::{Error, ErrorKind}; |
| 11 |
|
| 12 |
/// Different resolutions supported by the time related |
| 13 |
/// metric types (e.g. DatetimeMetric). |
| 14 |
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq, MallocSizeOf)] |
| 15 |
#[serde(rename_all = "lowercase")] |
| 16 |
#[repr(i32)] // use i32 to be compatible with our JNA definition |
| 17 |
pub enum TimeUnit { |
| 18 |
/// Truncate to nanosecond precision. |
| 19 |
Nanosecond, |
| 20 |
/// Truncate to microsecond precision. |
| 21 |
Microsecond, |
| 22 |
/// Truncate to millisecond precision. |
| 23 |
Millisecond, |
| 24 |
/// Truncate to second precision. |
| 25 |
Second, |
| 26 |
/// Truncate to minute precision. |
| 27 |
Minute, |
| 28 |
/// Truncate to hour precision. |
| 29 |
Hour, |
| 30 |
/// Truncate to day precision. |
| 31 |
Day, |
| 32 |
} |
| 33 |
|
| 34 |
impl TimeUnit { |
| 35 |
/// Formats the given time unit, truncating the time if needed. |
| 36 |
pub fn format_pattern(self) -> &'static str { |
| 37 |
use TimeUnit::*; |
| 38 |
match self { |
| 39 |
Nanosecond => "%Y-%m-%dT%H:%M:%S%.f%:z", |
| 40 |
Microsecond => "%Y-%m-%dT%H:%M:%S%.6f%:z", |
| 41 |
Millisecond => "%Y-%m-%dT%H:%M:%S%.3f%:z", |
| 42 |
Second => "%Y-%m-%dT%H:%M:%S%:z", |
| 43 |
Minute => "%Y-%m-%dT%H:%M%:z", |
| 44 |
Hour => "%Y-%m-%dT%H%:z", |
| 45 |
Day => "%Y-%m-%d%:z", |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// Converts a duration to the requested time unit. |
| 50 |
/// |
| 51 |
/// # Arguments |
| 52 |
/// |
| 53 |
/// * `duration` - the duration to convert. |
| 54 |
/// |
| 55 |
/// # Returns |
| 56 |
/// |
| 57 |
/// The integer representation of the converted duration. |
| 58 |
pub fn duration_convert(self, duration: Duration) -> u64 { |
| 59 |
use TimeUnit::*; |
| 60 |
match self { |
| 61 |
Nanosecond => duration.as_nanos() as u64, |
| 62 |
Microsecond => duration.as_micros() as u64, |
| 63 |
Millisecond => duration.as_millis() as u64, |
| 64 |
Second => duration.as_secs(), |
| 65 |
Minute => duration.as_secs() / 60, |
| 66 |
Hour => duration.as_secs() / 60 / 60, |
| 67 |
Day => duration.as_secs() / 60 / 60 / 24, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/// Converts a duration in the given unit to nanoseconds. |
| 72 |
/// |
| 73 |
/// # Arguments |
| 74 |
/// |
| 75 |
/// * `duration` - the duration to convert. |
| 76 |
/// |
| 77 |
/// # Returns |
| 78 |
/// |
| 79 |
/// The integer representation of the nanosecond duration. |
| 80 |
pub fn as_nanos(self, duration: u64) -> u64 { |
| 81 |
use TimeUnit::*; |
| 82 |
let duration = match self { |
| 83 |
Nanosecond => Duration::from_nanos(duration), |
| 84 |
Microsecond => Duration::from_micros(duration), |
| 85 |
Millisecond => Duration::from_millis(duration), |
| 86 |
Second => Duration::from_secs(duration), |
| 87 |
Minute => Duration::from_secs(duration * 60), |
| 88 |
Hour => Duration::from_secs(duration * 60 * 60), |
| 89 |
Day => Duration::from_secs(duration * 60 * 60 * 24), |
| 90 |
}; |
| 91 |
|
| 92 |
duration.as_nanos() as u64 |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/// Trait implementation for converting an integer value to a TimeUnit. |
| 97 |
/// |
| 98 |
/// This is used in the FFI code. |
| 99 |
/// |
| 100 |
/// Please note that values should match the ordering of the |
| 101 |
/// platform specific side of things (e.g. Kotlin implementation). |
| 102 |
impl TryFrom<i32> for TimeUnit { |
| 103 |
type Error = Error; |
| 104 |
|
| 105 |
fn try_from(value: i32) -> Result<TimeUnit, Self::Error> { |
| 106 |
match value { |
| 107 |
0 => Ok(TimeUnit::Nanosecond), |
| 108 |
1 => Ok(TimeUnit::Microsecond), |
| 109 |
2 => Ok(TimeUnit::Millisecond), |
| 110 |
3 => Ok(TimeUnit::Second), |
| 111 |
4 => Ok(TimeUnit::Minute), |
| 112 |
5 => Ok(TimeUnit::Hour), |
| 113 |
6 => Ok(TimeUnit::Day), |
| 114 |
e => Err(ErrorKind::TimeUnit(e).into()), |
| 115 |
} |
| 116 |
} |
| 117 |
} |