1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use lazy_regex::regex;
use std::{fmt::Display, ops::Add, time::Instant};
use syn::{
    parse::{Parse, ParseStream},
    LitInt,
};

use crate::{DurationError, DurationHumanValidator};

type StdDuration = std::time::Duration;

pub(crate) const SEC: u64 = 1_000;
const MINUTE: u64 = 60 * SEC;
const HOUR: u64 = 60 * MINUTE;
const DAY: u64 = 24 * HOUR;
const WEEK: u64 = 7 * DAY;
const MONTH: u64 = 30 * DAY;
const YEAR: u64 = 365 * DAY;
const CENTURY: u64 = 100 * YEAR;

/// Define a Duration in human readable form
///
/// ## Examples
/// ```
/// # use duration_human::DurationHuman;
/// let duration = DurationHuman::try_from("80h").unwrap();
/// assert_eq!(format!("{:#}", duration), "3 day 8h".to_string());
/// assert_eq!(format!("{}", duration), "80h".to_string());
/// let duration = DurationHuman::try_from("72h").unwrap();
/// assert_eq!(format!("{:#}", duration), "3 day".to_string());
/// assert_eq!(format!("{}", duration), "3 day".to_string());
/// let duration = DurationHuman::try_from("18446744073709551615ns").unwrap();
/// assert_eq!(format!("{:#}", duration), "5 century 84 year 11 month 1 week 6 day 23h 34min 33s 709ms".to_string());
/// let duration = DurationHuman::try_from("18446744073709551615ms").unwrap();
/// assert_eq!(format!("{:#}", duration), "5849424 century 17 year 4 month 1 week 2 day 14h 25min 51s 615ms".to_string());
/// // precision is ms
/// let duration = DurationHuman::try_from("604800μs").unwrap();
/// assert_eq!(format!("{:#}", duration), "604ms".to_string());
/// assert_eq!(duration.to_string(), "604ms".to_string());
/// let duration = DurationHuman::try_from("604800ms").unwrap();
/// assert_eq!(format!("{:#}", duration), "10min 4s 800ms".to_string());
/// assert_eq!(duration.to_string(), "604800ms".to_string());
/// let duration = DurationHuman::try_from("604800s").unwrap();
/// assert_eq!(format!("{:#}", duration), "1 week".to_string());
/// let duration = DurationHuman::try_from("604800s").unwrap();
/// assert_eq!(format!("{:#}", duration), "1 week".to_string(),"A");
/// assert_eq!(format!("{}", duration), "1 week".to_string(),"B");
/// let duration = DurationHuman::try_from("608430s").unwrap();
/// assert_eq!(format!("{:#}", duration), "1 week 1h 30s".to_string());
/// assert_eq!(format!("{}", duration), "608430s".to_string(),"C");
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Copy)]
pub struct DurationHuman {
    inner: StdDuration,
}

impl DurationHuman {
    pub const ONE_SECOND: Self = Self::new(SEC);
    pub const ONE_MILLISECOND: Self = Self::new(1);

    #[must_use]
    pub const fn new(ms: u64) -> Self {
        Self {
            inner: std::time::Duration::from_millis(ms),
        }
    }

    #[must_use]
    pub fn is_in(&self, range: &DurationHumanValidator) -> bool {
        range.contains(self)
    }
}

impl Default for DurationHuman {
    fn default() -> Self {
        Self {
            inner: StdDuration::from_millis(MINUTE),
        }
    }
}

impl Parse for DurationHuman {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let duration_with_unit = input.parse::<LitInt>()?.to_string();
        match TryInto::<Self>::try_into(duration_with_unit.as_str()) {
            Ok(duration_human) => Ok(duration_human),
            Err(e) => Err(input.error(e.to_string())),
        }
    }
}

impl Add<Instant> for DurationHuman {
    type Output = Instant;

    fn add(self, rhs: Instant) -> Self::Output {
        rhs + self.inner
    }
}

impl From<&DurationHuman> for StdDuration {
    fn from(duration: &DurationHuman) -> Self {
        duration.inner
    }
}

impl From<u64> for DurationHuman {
    fn from(ms: u64) -> Self {
        Self::new(ms)
    }
}

impl TryFrom<&str> for DurationHuman {
    type Error = DurationError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let splitter = regex!(r"^(\d+)\s*(year|month|week|day|h|min|s|ms|μs|ns)$");

        splitter
            .captures(value)
            .map_or(Err(DurationError::InvalidSyntax), |group| {
                let value = group[1].parse::<u64>()?;
                if value == 0 {
                    Ok(std::time::Duration::ZERO)
                } else {
                    match &group[2] {
                        "year" => Ok(std::time::Duration::from_millis(value * YEAR)),
                        "month" => Ok(std::time::Duration::from_millis(value * MONTH)),
                        "week" => Ok(std::time::Duration::from_millis(value * WEEK)),
                        "day" => Ok(std::time::Duration::from_millis(value * DAY)),
                        "h" => Ok(std::time::Duration::from_millis(value * HOUR)),
                        "min" => Ok(std::time::Duration::from_millis(value * MINUTE)),
                        "s" => Ok(std::time::Duration::from_millis(value * SEC)),
                        "ms" => Ok(std::time::Duration::from_millis(value)),
                        "μs" => Ok(std::time::Duration::from_micros(value)),
                        "ns" => Ok(std::time::Duration::from_nanos(value)),

                        sym => Err(DurationError::UnsupportedSymbol {
                            sym: sym.to_string(),
                        }),
                    }
                }
            })
            .map(|inner| Self { inner })
    }
}

impl From<DurationHuman> for clap::builder::OsStr {
    fn from(duration: DurationHuman) -> Self {
        duration.to_string().into()
    }
}

impl From<&DurationHuman> for u64 {
    #[allow(clippy::cast_possible_truncation)] // cast is okay, as u64::MAX as milliseconds is more than 500 million years
    fn from(duration: &DurationHuman) -> Self {
        duration.inner.as_millis() as Self
    }
}

impl Display for DurationHuman {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut ms: u64 = self.into();
        if f.alternate() {
            let durations: Vec<String> = [
                (CENTURY, " century"),
                (YEAR, " year"),
                (MONTH, " month"),
                (WEEK, " week"),
                (DAY, " day"),
                (HOUR, "h"),
                (MINUTE, "min"),
                (SEC, "s"),
                (1, "ms"),
            ]
            .iter()
            .filter_map(|(part_ms, unit)| {
                let part = ms / part_ms;
                ms %= part_ms;
                if part > 0 {
                    Some(format!("{}{}", part, unit))
                } else {
                    None
                }
            })
            .collect();
            f.write_str(durations.join(" ").as_str())
        } else {
            f.write_str(
                match ms {
                    _ if ms < SEC || ms % SEC != 0 => format!("{}ms", ms),
                    _ if ms < MINUTE || ms % MINUTE != 0 => format!("{}s", ms / SEC),
                    _ if ms < HOUR || ms % HOUR != 0 => format!("{}min", ms / MINUTE),
                    _ if ms < DAY || ms % DAY != 0 => format!("{}h", ms / HOUR),
                    _ if ms < WEEK || ms % WEEK != 0 => format!("{} day", ms / DAY),
                    _ if ms < MONTH || ms % MONTH != 0 => format!("{} week", ms / WEEK),
                    _ if ms < YEAR || ms % YEAR != 0 => format!("{} month", ms / MONTH),
                    _ if ms < CENTURY || ms % CENTURY != 0 => format!("{} year", ms / YEAR),
                    _ => format!("{} century", ms / CENTURY),
                }
                .as_str(),
            )
        }
    }
}