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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use super::Tick;
use super::TumblingWindow;
use core::{cmp::Ordering, fmt, hash::Hash, time::Duration};
use time::{OffsetDateTime, UtcOffset};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Period kind.
#[derive(Debug, Clone, Copy)]
pub enum PeriodKind {
    /// A year.
    Year,
    /// A month.
    Month,
    /// Duration.
    Duration(Duration),
}

impl PartialEq for PeriodKind {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (PeriodKind::Year, PeriodKind::Year) => true,
            (PeriodKind::Month, PeriodKind::Month) => true,
            (PeriodKind::Duration(lhs), PeriodKind::Duration(rhs)) => lhs.eq(rhs),
            _ => false,
        }
    }
}

impl Hash for PeriodKind {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        match self {
            Self::Year => {
                state.write(&[0x00]);
                state.finish();
            }
            Self::Month => {
                state.write(&[0x01]);
                state.finish();
            }
            Self::Duration(d) => {
                state.write(&[0x02]);
                d.hash(state);
            }
        }
    }
}

impl Eq for PeriodKind {}

const YEAD_SECS_LOWER: u64 = 31_536_000;
const YEAD_SECS_UPPER: u64 = 31_622_400;
const MONTH_SECS_LOWER: u64 = 2_419_200;
const MONTH_SECS_UPPER: u64 = 2_678_400;
const DAY_SECS: u64 = 86_400;
const WEEK_SECS: u64 = 604_800;
const HOUR_SECS: u64 = 3_600;
const MINUTE_SECS: u64 = 60;

impl PartialOrd for PeriodKind {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (PeriodKind::Year, PeriodKind::Year) => Some(Ordering::Equal),
            (PeriodKind::Month, PeriodKind::Month) => Some(Ordering::Equal),
            (PeriodKind::Duration(lhs), PeriodKind::Duration(rhs)) => lhs.partial_cmp(rhs),
            (PeriodKind::Year, PeriodKind::Duration(d)) => {
                if d.as_secs() < YEAD_SECS_LOWER {
                    Some(Ordering::Greater)
                } else if d.as_secs() > YEAD_SECS_UPPER {
                    Some(Ordering::Less)
                } else if d.as_secs() == YEAD_SECS_UPPER {
                    if d.subsec_micros() > 0 {
                        Some(Ordering::Less)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            (PeriodKind::Duration(d), PeriodKind::Year) => {
                if d.as_secs() < YEAD_SECS_LOWER {
                    Some(Ordering::Less)
                } else if d.as_secs() > YEAD_SECS_UPPER {
                    Some(Ordering::Greater)
                } else if d.as_secs() == YEAD_SECS_UPPER {
                    if d.subsec_micros() > 0 {
                        Some(Ordering::Greater)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            (PeriodKind::Month, PeriodKind::Duration(d)) => {
                if d.as_secs() < MONTH_SECS_LOWER {
                    Some(Ordering::Greater)
                } else if d.as_secs() > MONTH_SECS_UPPER {
                    Some(Ordering::Less)
                } else if d.as_secs() == MONTH_SECS_UPPER {
                    if d.subsec_micros() > 0 {
                        Some(Ordering::Less)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            (PeriodKind::Duration(d), PeriodKind::Month) => {
                if d.as_secs() < MONTH_SECS_LOWER {
                    Some(Ordering::Less)
                } else if d.as_secs() > MONTH_SECS_UPPER {
                    Some(Ordering::Greater)
                } else if d.as_secs() == MONTH_SECS_UPPER {
                    if d.subsec_micros() > 0 {
                        Some(Ordering::Greater)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            (PeriodKind::Month, PeriodKind::Year) => Some(Ordering::Less),
            (PeriodKind::Year, PeriodKind::Month) => Some(Ordering::Greater),
        }
    }
}

#[cfg(feature = "serde")]
impl Serialize for PeriodKind {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            Self::Year => serializer.serialize_str("year"),
            Self::Month => serializer.serialize_str("month"),
            Self::Duration(dur) => {
                let formatted = humantime::format_duration(*dur);
                serializer.collect_str(&formatted)
            }
        }
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for PeriodKind {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct Visitor;

        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = PeriodKind;

            fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                write!(f, "\"year\", \"month\" or any `str` that is parsable by `humantime::parse_duration`")
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                let kind = match v {
                    "year" => PeriodKind::Year,
                    "month" => PeriodKind::Month,
                    v => {
                        let dur = humantime::parse_duration(v)
                            .map_err(|_| E::invalid_value(serde::de::Unexpected::Str(v), &self))?;
                        PeriodKind::Duration(dur)
                    }
                };
                Ok(kind)
            }
        }

        deserializer.deserialize_str(Visitor)
    }
}

#[cfg(feature = "serde")]
fn default_offset() -> UtcOffset {
    UtcOffset::UTC
}

/// Period mode (A tumbling window).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Period {
    /// UTC offset.
    #[cfg_attr(feature = "serde", serde(default = "default_offset"))]
    pub offset: UtcOffset,
    /// Period kind.
    #[cfg_attr(feature = "serde", serde(rename = "period"))]
    pub kind: PeriodKind,
}

impl PartialOrd for Period {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if self.offset.eq(&other.offset) {
            self.kind.partial_cmp(&other.kind)
        } else {
            None
        }
    }
}

impl Period {
    /// Zero period.
    ///
    /// Directly compare the two timestamp.
    pub fn zero() -> Self {
        Self {
            offset: UtcOffset::UTC,
            kind: PeriodKind::Duration(Duration::ZERO),
        }
    }

    /// A year.
    pub fn year(offset: UtcOffset) -> Self {
        Self {
            offset,
            kind: PeriodKind::Year,
        }
    }

    /// A Month.
    pub fn month(offset: UtcOffset) -> Self {
        Self {
            offset,
            kind: PeriodKind::Month,
        }
    }

    /// A Day.
    pub fn day(offset: UtcOffset) -> Self {
        Self {
            offset,
            kind: PeriodKind::Duration(Duration::from_secs(DAY_SECS)),
        }
    }

    /// Weeks.
    pub fn weeks(offset: UtcOffset, weeks: u32) -> Self {
        if weeks == 0 {
            Self::zero()
        } else {
            Self {
                offset,
                kind: PeriodKind::Duration(Duration::from_secs(weeks as u64 * WEEK_SECS)),
            }
        }
    }

    /// Days.
    pub fn days(offset: UtcOffset, days: u32) -> Self {
        match days {
            0 => Self::zero(),
            1 => Self::day(offset),
            days => Self {
                offset,
                kind: PeriodKind::Duration(Duration::from_secs(days as u64 * DAY_SECS)),
            },
        }
    }

    /// Hours.
    pub fn hours(offset: UtcOffset, hours: u32) -> Self {
        if hours == 0 {
            Self::zero()
        } else {
            Self {
                offset,
                kind: PeriodKind::Duration(Duration::from_secs(hours as u64 * HOUR_SECS)),
            }
        }
    }

    /// Minutes
    pub fn minutes(offset: UtcOffset, minutes: u32) -> Self {
        if minutes == 0 {
            Self::zero()
        } else {
            Self {
                offset,
                kind: PeriodKind::Duration(Duration::from_secs(minutes as u64 * MINUTE_SECS)),
            }
        }
    }

    /// Seconds
    pub fn seconds(offset: UtcOffset, seconds: u64) -> Self {
        if seconds == 0 {
            Self::zero()
        } else {
            Self {
                offset,
                kind: PeriodKind::Duration(Duration::from_secs(seconds)),
            }
        }
    }

    /// Seconds.
    pub fn secs(seconds: u64) -> Self {
        if seconds == 0 {
            Self::zero()
        } else {
            Self {
                offset: UtcOffset::UTC,
                kind: PeriodKind::Duration(Duration::from_secs(seconds)),
            }
        }
    }

    /// Convert period to [`Duration`].
    ///
    /// Return `None` if period is a year or a month.
    pub fn to_std_duration(&self) -> Option<Duration> {
        match self.kind {
            PeriodKind::Year | PeriodKind::Month => None,
            PeriodKind::Duration(d) => Some(d),
        }
    }

    /// Convert period to [`time::Duration`].
    ///
    /// Return `None` if period is a year or a month.
    pub fn to_duration(&self) -> Option<time::Duration> {
        match self.kind {
            PeriodKind::Year | PeriodKind::Month => None,
            PeriodKind::Duration(d) => time::Duration::try_from(d).ok(),
        }
    }

    /// Get the utc offset of this period.
    pub fn utc_offset(&self) -> UtcOffset {
        self.offset
    }

    /// Get period kind.
    pub fn kind(&self) -> PeriodKind {
        self.kind
    }

    /// Change the utc offset.
    /// # Example
    /// ```
    /// use indicator::Period;
    /// use time::macros::offset;
    ///
    /// let period = Period::day(offset!(+0));
    /// assert_eq!(period.to_offset(offset!(+8)), Period::day(offset!(+8)));
    /// ```
    pub fn to_offset(&self, offset: UtcOffset) -> Self {
        Self {
            offset,
            kind: self.kind,
        }
    }
}

const WEEK_OFFSET: Duration = Duration::from_secs(4 * DAY_SECS);

impl TumblingWindow for Period {
    fn same_window(&self, lhs: &Tick, rhs: &Tick) -> bool {
        let lhs = lhs.ts().map(|t| t.to_offset(self.offset));
        let rhs = rhs.ts().map(|t| t.to_offset(self.offset));
        match (lhs, rhs) {
            (Some(lhs), Some(rhs)) => match self.kind {
                PeriodKind::Year => lhs.year() == rhs.year(),
                PeriodKind::Month => lhs.year() == rhs.year() && lhs.month() == rhs.month(),
                PeriodKind::Duration(d) => {
                    let d = d.as_secs() as i128;
                    if d == 0 {
                        return lhs == rhs;
                    }
                    let base = OffsetDateTime::UNIX_EPOCH.replace_offset(self.offset) + WEEK_OFFSET;
                    let lhs = (lhs - base).whole_seconds() as i128 / d;
                    let rhs = (rhs - base).whole_seconds() as i128 / d;
                    lhs == rhs
                }
            },
            _ => false,
        }
    }
}

#[cfg(feature = "humantime")]
use humantime::format_duration;

impl fmt::Display for PeriodKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Year => {
                write!(f, "year")
            }
            Self::Month => {
                write!(f, "month")
            }
            Self::Duration(d) => {
                #[cfg(not(feature = "humantime"))]
                {
                    write!(f, "{}s", d.as_secs())
                }
                #[cfg(feature = "humantime")]
                {
                    write!(f, "{}", format_duration(*d))
                }
            }
        }
    }
}

impl fmt::Display for Period {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {})", self.kind, self.offset.whole_hours())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use time::macros::{datetime, offset};
    use time::UtcOffset;

    #[test]
    fn zero_period() {
        let mode = Period::zero();
        let lhs = datetime!(2021-11-1 00:00:00 +08).into();
        let rhs = datetime!(2021-11-1 00:00:00 +08).into();
        assert!(mode.same_window(&lhs, &rhs));
    }

    #[test]
    fn week_different_utc_offset() {
        let lhs = datetime!(2021-11-1 00:00:00 +08).into();
        let rhs = datetime!(2021-11-1 00:00:00 UTC).into();
        let mode = Period::weeks(UtcOffset::UTC, 1);
        assert!(!mode.same_window(&lhs, &rhs));
        let mode = Period::weeks(offset!(+8), 1);
        assert!(mode.same_window(&lhs, &rhs));
    }

    #[test]
    fn week() {
        let mode = Period::weeks(offset!(+8), 1);
        let lhs = datetime!(2021-11-1 07:00:12 +08).into();
        let rhs = datetime!(2021-11-7 12:00:21 +08).into();
        assert!(mode.same_window(&lhs, &rhs));
        let rhs = datetime!(2021-11-8 1:00:12 + 08).into();
        assert!(!mode.same_window(&lhs, &rhs));
    }

    #[test]
    fn hours() {
        let mode = Period::hours(offset!(+8), 2);
        let lhs = datetime!(2021-11-1 00:00:00 +08).into();
        let rhs = datetime!(2021-11-1 01:29:31 +08).into();
        assert!(mode.same_window(&lhs, &rhs));
        let rhs = datetime!(2021-11-1 02:00:00 +08).into();
        assert!(!mode.same_window(&lhs, &rhs));
        let rhs = datetime!(2021-10-31 23:59:59 +08).into();
        assert!(!mode.same_window(&lhs, &rhs));
    }

    #[cfg(feature = "std")]
    #[test]
    fn to_string() {
        let mode = Period::hours(offset!(+8), 2);
        println!("{mode}");
    }

    #[cfg(feature = "serde")]
    #[test]
    fn serialize_period_kind() {
        let kind = PeriodKind::Year;
        assert_eq!(serde_json::to_string(&kind).unwrap(), r#""year""#);

        let kind = PeriodKind::Month;
        assert_eq!(serde_json::to_string(&kind).unwrap(), r#""month""#);

        let dur = Duration::from_secs(12346789);
        let kind = PeriodKind::Duration(dur);
        assert_eq!(
            serde_json::to_string(&kind).unwrap(),
            r#""4months 21days 3h 25m 25s""#,
        );
    }

    #[cfg(feature = "serde")]
    #[test]
    fn deserialize_period_kind() {
        let kind = serde_json::from_str::<PeriodKind>(r#""year""#).unwrap();
        assert_eq!(kind, PeriodKind::Year);

        let kind = serde_json::from_str::<PeriodKind>(r#""month""#).unwrap();
        assert_eq!(kind, PeriodKind::Month);

        let kind = serde_json::from_str::<PeriodKind>(r#""3m 4s""#).unwrap();
        assert_eq!(kind, PeriodKind::Duration(Duration::from_secs(184)),);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn serialize_period() {
        let period = Period::year(UtcOffset::UTC);
        assert_eq!(
            serde_json::to_string(&period).unwrap(),
            r#"{"offset":"+00:00:00","period":"year"}"#
        );

        let period = Period::month(offset!(-01:23:45));
        assert_eq!(
            serde_json::to_string(&period).unwrap(),
            r#"{"offset":"-01:23:45","period":"month"}"#
        );

        let period = Period::hours(UtcOffset::UTC, 3);
        assert_eq!(
            serde_json::to_string(&period).unwrap(),
            r#"{"offset":"+00:00:00","period":"3h"}"#
        );

        let period = Period::minutes(UtcOffset::UTC, 42);
        assert_eq!(
            serde_json::to_string(&period).unwrap(),
            r#"{"offset":"+00:00:00","period":"42m"}"#
        );
    }

    #[cfg(feature = "serde")]
    #[test]
    fn deserialize_period() {
        let period =
            serde_json::from_str::<Period>(r#"{"offset":"+00:00:00","period":"year"}"#).unwrap();
        assert_eq!(period, Period::year(UtcOffset::UTC),);

        let period =
            serde_json::from_str::<Period>(r#"{"offset":"-01:23:45","period":"month"}"#).unwrap();
        assert_eq!(period, Period::month(offset!(-01:23:45)),);

        let period =
            serde_json::from_str::<Period>(r#"{"offset":"+00:00:00","period":"3h"}"#).unwrap();
        assert_eq!(period, Period::hours(UtcOffset::UTC, 3),);

        let period =
            serde_json::from_str::<Period>(r#"{"offset":"+00:00:00","period":"42m"}"#).unwrap();
        assert_eq!(period, Period::minutes(UtcOffset::UTC, 42),);
    }
}