dir_meta/
utils.rs

1#[cfg(feature = "time")]
2use chrono::{DateTime, Utc};
3#[cfg(feature = "time")]
4use std::time::Duration;
5
6#[cfg(feature = "time")]
7use std::time::SystemTime;
8
9#[cfg(feature = "time")]
10use tai64::Tai64N;
11
12/// Reusable Clone-on-Write str with lifetime of `'a`
13pub type CowStr<'a> = std::borrow::Cow<'a, str>;
14
15/// A convenience struct to access utilities
16pub struct FsUtils;
17
18impl FsUtils {
19    /// Returns [Option::None] if time query is not supported
20    #[cfg(feature = "time")]
21    pub fn maybe_time(time_result: Option<SystemTime>) -> Option<Tai64N> {
22        time_result.map(|time| Tai64N::from_system_time(&time))
23    }
24
25    /// Calculate the size in bytes
26    #[cfg(feature = "size")]
27    pub fn size_to_bytes(bytes: usize) -> String {
28        byte_prefix::calc_bytes(bytes as f32)
29    }
30
31    /// Convert TAI64N to local time in 24 hour format
32    #[cfg(feature = "time")]
33    pub fn tai64_to_local_hrs<'a>(time: &Tai64N) -> DateTimeString<'a> {
34        let date_time: DateTime<Utc> = time.to_system_time().into();
35        let date = date_time
36            .date_naive()
37            .format("%A, %-d %B, %C%y")
38            .to_string();
39        let date = CowStr::Owned(date);
40        let time = date_time.format("%H:%M:%S").to_string();
41        let time = CowStr::Owned(time);
42
43        DateTimeString { date, time }
44    }
45
46    /// Convert TAI64N to local time in 12 hour format
47    #[cfg(feature = "time")]
48    pub fn tai64_to_local_am_pm<'a>(time: &Tai64N) -> DateTimeString<'a> {
49        let date_time: DateTime<Utc> = time.to_system_time().into();
50        let date = date_time
51            .date_naive()
52            .format("%A, %-d %B, %C%y")
53            .to_string();
54        let date = CowStr::Owned(date);
55        let time = date_time.format("%-I:%M %p").to_string();
56        let time = CowStr::Owned(time);
57
58        DateTimeString { date, time }
59    }
60
61    /// Convert duration since UNIX EPOCH to humantime
62    #[cfg(feature = "time")]
63    pub fn tai64_to_humantime_with_epoch(time: &Tai64N) -> Option<String> {
64        FsUtils::tai64_duration_since_epoch(time)
65            .map(|duration| humantime::format_duration(duration).to_string())
66    }
67
68    /// Convert duration since two TAI64N timestamps to humantime
69    #[cfg(feature = "time")]
70    pub fn tai64_to_humantime(earlier_time: &Tai64N, current_time: &Tai64N) -> Option<String> {
71        FsUtils::tai64_duration(earlier_time, current_time)
72            .map(|duration| humantime::format_duration(duration).to_string())
73    }
74
75    /// Convert duration between current time and earlier TAI64N timestamp to humantime
76    #[cfg(feature = "time")]
77    pub fn tai64_now_duration_to_humantime(earlier_time: &Tai64N) -> Option<String> {
78        FsUtils::tai64_duration_from_now(earlier_time)
79            .map(|duration| humantime::format_duration(duration).to_string())
80    }
81
82    /// Get the duration between two TAI64N timestamps
83    #[cfg(feature = "time")]
84    pub fn tai64_duration(earlier_time: &Tai64N, current_time: &Tai64N) -> Option<Duration> {
85        match earlier_time.duration_since(current_time) {
86            Ok(valid_time) => Some(valid_time),
87            Err(_) => Option::None,
88        }
89    }
90
91    /// Get the duration since UNIX EPOCH
92    #[cfg(feature = "time")]
93    pub fn tai64_duration_since_epoch(time: &Tai64N) -> Option<Duration> {
94        match time.duration_since(&Tai64N::UNIX_EPOCH) {
95            Ok(valid_time) => Some(valid_time),
96            Err(_) => Option::None,
97        }
98    }
99
100    /// Get the duration since UNIX EPOCH
101    #[cfg(feature = "time")]
102    pub fn tai64_duration_from_now(earlier_time: &Tai64N) -> Option<Duration> {
103        match Tai64N::now().duration_since(earlier_time) {
104            Ok(valid_time) => Some(valid_time),
105            Err(_) => Option::None,
106        }
107    }
108}
109
110/// The data and time in human readable [String]
111#[cfg(feature = "time")]
112#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Default)]
113pub struct DateTimeString<'a> {
114    /// The data without a timestamp
115    pub date: CowStr<'a>,
116    /// A timestamp without a date
117    pub time: CowStr<'a>,
118}