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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use std::hash::Hash;
use std::str::FromStr;

use chrono::Duration;
use tea_error::{tbail, tensure, TError, TResult};

use crate::convert::*;

#[cfg(feature = "serde")]
#[serde_with::serde_as]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
/// Represents a duration of time with both months and a more precise duration.
///
/// This struct combines a number of months with a `chrono::Duration` to represent
/// time intervals that may include calendar-specific units (months) as well as
/// fixed-length durations.
///
/// # Fields
///
/// * `months`: The number of months in the time delta.
/// * `inner`: A `chrono::Duration` representing the precise duration beyond whole months.
///
/// # Serialization
///
/// When the "serde" feature is enabled, this struct can be serialized and deserialized.
pub struct TimeDelta {
    pub months: i32,
    // #[cfg_attr(feature = "serde", serde_as(as = "serde_with::DurationSeconds<i64>"))]
    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
    pub inner: Duration,
}

#[cfg(not(feature = "serde"))]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
/// Represents a duration of time with both months and a more precise duration.
///
/// This struct combines a number of months with a `chrono::Duration` to represent
/// time intervals that may include calendar-specific units (months) as well as
/// fixed-length durations.
///
/// # Fields
///
/// * `months`: The number of months in the time delta.
/// * `inner`: A `chrono::Duration` representing the precise duration beyond whole months.
///
/// # Serialization
///
/// When the "serde" feature is enabled, this struct can be serialized and deserialized.
pub struct TimeDelta {
    pub months: i32,
    pub inner: Duration,
}

impl FromStr for TimeDelta {
    type Err = TError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        TimeDelta::parse(s)
    }
}

impl From<&str> for TimeDelta {
    #[inline]
    fn from(s: &str) -> Self {
        TimeDelta::parse(s).unwrap_or_else(|e| panic!("{}", e))
    }
}

impl TimeDelta {
    /// Parse timedelta from string
    ///
    /// for example: "2y1mo-3d5h-2m3s"
    /// Parses a string representation of a duration into a `TimeDelta`.
    ///
    /// This function supports a variety of time units and can handle complex duration strings.
    ///
    /// # Supported Units
    ///
    /// - `ns`: nanoseconds
    ///
    /// - `us`: microseconds
    ///
    /// - `ms`: milliseconds
    ///
    /// - `s`: seconds
    ///
    /// - `m`: minutes
    ///
    /// - `h`: hours
    ///
    /// - `d`: days
    ///
    /// - `w`: weeks
    ///
    /// - `mo`: months
    ///
    /// - `y`: years
    ///
    /// # Format
    /// The duration string should be in the format of `<number><unit>`, and multiple such pairs can be combined.
    /// For example: "2y1mo-3d5h-2m3s" represents 2 years, 1 month, minus 3 days, 5 hours, minus 2 minutes, and 3 seconds.
    ///
    /// # Arguments
    /// * `duration` - A string slice that holds the duration to be parsed.
    ///
    /// # Returns
    /// * `TResult<Self>` - A Result containing the parsed `TimeDelta` if successful, or an error if parsing fails.
    ///
    /// # Examples
    /// ```
    /// use tea_time::TimeDelta;
    ///
    /// let td = TimeDelta::parse("1y2mo3d4h5m6s").unwrap();
    /// assert_eq!(td.months, 14); // 1 year and 2 months
    /// assert_eq!(td.inner, chrono::Duration::seconds(3 * 86400 + 4 * 3600 + 5 * 60 + 6));
    /// ```
    pub fn parse(duration: &str) -> TResult<Self> {
        let mut nsecs = 0;
        let mut secs = 0;
        let mut months = 0;
        let mut iter = duration.char_indices();
        let mut start = 0;
        let mut unit = String::with_capacity(2);
        while let Some((i, mut ch)) = iter.next() {
            if !ch.is_ascii_digit() && i != 0 {
                let n = duration[start..i].parse::<i64>().unwrap();
                loop {
                    if ch.is_ascii_alphabetic() {
                        unit.push(ch)
                    } else {
                        break;
                    }
                    match iter.next() {
                        Some((i, ch_)) => {
                            ch = ch_;
                            start = i
                        },
                        None => {
                            break;
                        },
                    }
                }
                tensure!(!unit.is_empty(), ParseError:"expected a unit in the duration string");

                match unit.as_str() {
                    "ns" => nsecs += n,
                    "us" => nsecs += n * NANOS_PER_MICRO,
                    "ms" => nsecs += n * NANOS_PER_MILLI,
                    "s" => secs += n,
                    "m" => secs += n * SECS_PER_MINUTE,
                    "h" => secs += n * SECS_PER_HOUR,
                    "d" => secs += n * SECS_PER_DAY,
                    "w" => secs += n * SECS_PER_WEEK,
                    "mo" => months += n as i32,
                    "y" => months += n as i32 * 12,
                    unit => tbail!(ParseError:"unit: '{}' not supported", unit),
                }
                unit.clear();
            }
        }
        let duration = Duration::seconds(secs) + Duration::nanoseconds(nsecs);
        Ok(TimeDelta {
            months,
            inner: duration,
        })
    }

    #[inline(always)]
    pub const fn nat() -> Self {
        Self {
            months: i32::MIN,
            inner: Duration::seconds(0),
        }
    }

    #[allow(dead_code)]
    #[inline(always)]
    pub const fn is_nat(&self) -> bool {
        self.months == i32::MIN
    }

    #[inline(always)]
    pub const fn is_not_nat(&self) -> bool {
        self.months != i32::MIN
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_timedelta() {
        let cases = vec![
            (
                "1d",
                TimeDelta {
                    months: 0,
                    inner: Duration::days(1),
                },
            ),
            (
                "2w",
                TimeDelta {
                    months: 0,
                    inner: Duration::weeks(2),
                },
            ),
            (
                "3mo",
                TimeDelta {
                    months: 3,
                    inner: Duration::seconds(0),
                },
            ),
            (
                "1y2mo",
                TimeDelta {
                    months: 14,
                    inner: Duration::seconds(0),
                },
            ),
            (
                "1d12h30m",
                TimeDelta {
                    months: 0,
                    inner: Duration::days(1) + Duration::hours(12) + Duration::minutes(30),
                },
            ),
            (
                "1h30m45s",
                TimeDelta {
                    months: 0,
                    inner: Duration::hours(1) + Duration::minutes(30) + Duration::seconds(45),
                },
            ),
            (
                "500ms",
                TimeDelta {
                    months: 0,
                    inner: Duration::milliseconds(500),
                },
            ),
            (
                "1us",
                TimeDelta {
                    months: 0,
                    inner: Duration::microseconds(1),
                },
            ),
            (
                "100ns",
                TimeDelta {
                    months: 0,
                    inner: Duration::nanoseconds(100),
                },
            ),
        ];

        for (input, expected) in cases {
            let result = TimeDelta::parse(input).unwrap();
            assert_eq!(result, expected, "Failed for input: {}", input);
        }
    }

    #[test]
    fn test_parse_timedelta_errors() {
        let error_cases = vec![
            "1x",   // Invalid unit
            "1.5d", // Fractional values not supported
        ];

        for input in error_cases {
            assert!(
                TimeDelta::parse(input).is_err(),
                "Expected error for input: {}",
                input
            );
        }
    }

    #[test]
    fn test_nat_timedelta() {
        let nat = TimeDelta::nat();
        assert!(nat.is_nat());
        assert!(!nat.is_not_nat());

        let non_nat = TimeDelta::parse("1d").unwrap();
        assert!(!non_nat.is_nat());
        assert!(non_nat.is_not_nat());
    }
}