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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
pub mod data;

pub use data::{
    Hour::{self, *},
    Minuter::{self, *},
    MonthDay::{self, *},
    Second::{self, *},
    WeekDay::{self, *},
};

use anyhow::{bail, Result};
use chrono::{Datelike, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime};
use data::{AsData, DateTime};
use log::{debug, trace};
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Add, AddAssign, BitAnd, BitOr, BitOrAssign, Bound, RangeBounds, Shl, Sub};
// use time::format_description::FormatItem;
// use time::macros::format_description;
// use time::{Duration, OffsetDateTime, PrimitiveDateTime};
//
// const TS_DASHES_BLANK_COLONS_DOT_BLANK: &[FormatItem<'static>] =
//     format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");

#[derive(Clone)]
pub struct MonthDays(u32);
#[derive(Clone)]
pub struct WeekDays(u8);
#[derive(Clone)]
pub struct Hours(u32);
#[derive(Clone)]
pub struct Minuters(u64);
#[derive(Clone)]
pub struct Seconds(u64);
impl Operator for Hours {
    const MIN: Self::ValTy = 0;
    const MAX: Self::ValTy = 23;
    const ONE: Self::ValTy = 1;
    const ZERO: Self::ValTy = 0;
    const DEFAULT_MAX: Self::ValTy = u32::MAX >> 8;
    type ValTy = u32;
    fn _default() -> Self {
        Self(0)
    }
    fn _val(&self) -> Self::ValTy {
        self.0
    }
    fn _mut_val(&mut self, val: Self::ValTy) {
        self.0 = val
    }
}
impl Operator for Seconds {
    const MIN: Self::ValTy = 0;
    const MAX: Self::ValTy = 59;
    const ONE: Self::ValTy = 1;
    const ZERO: Self::ValTy = 0;
    const DEFAULT_MAX: Self::ValTy = u64::MAX >> 4;
    type ValTy = u64;
    fn _default() -> Self {
        Self(0)
    }
    fn _val(&self) -> Self::ValTy {
        self.0
    }
    fn _mut_val(&mut self, val: Self::ValTy) {
        self.0 = val
    }
}
impl Operator for Minuters {
    const MIN: Self::ValTy = 0;
    const MAX: Self::ValTy = 59;
    const ONE: Self::ValTy = 1;
    const ZERO: Self::ValTy = 0;
    const DEFAULT_MAX: Self::ValTy = u64::MAX >> 4;
    type ValTy = u64;
    fn _default() -> Self {
        Self(0)
    }
    fn _val(&self) -> Self::ValTy {
        self.0
    }
    fn _mut_val(&mut self, val: Self::ValTy) {
        self.0 = val
    }
}

impl Operator for MonthDays {
    const MIN: Self::ValTy = 1;
    const MAX: Self::ValTy = 31;
    const ONE: Self::ValTy = 1;
    const ZERO: Self::ValTy = 0;
    const DEFAULT_MAX: Self::ValTy = u32::MAX << 1;
    type ValTy = u32;
    fn _default() -> Self {
        Self(0)
    }

    fn _val(&self) -> Self::ValTy {
        self.0
    }
    fn _mut_val(&mut self, val: Self::ValTy) {
        self.0 = val
    }
}
impl Operator for WeekDays {
    const MIN: Self::ValTy = 1;
    const MAX: Self::ValTy = 7;
    const ONE: Self::ValTy = 1;
    const ZERO: Self::ValTy = 0;
    const DEFAULT_MAX: Self::ValTy = u8::MAX << 1;
    type ValTy = u8;

    fn _default() -> Self {
        Self(0)
    }

    fn _val(&self) -> Self::ValTy {
        self.0
    }
    fn _mut_val(&mut self, val: Self::ValTy) {
        self.0 = val
    }
}

pub trait Operator: Sized {
    const MIN: Self::ValTy;
    const MAX: Self::ValTy;
    const ONE: Self::ValTy;
    const ZERO: Self::ValTy;
    const DEFAULT_MAX: Self::ValTy;
    type ValTy: BitOr<Output = Self::ValTy>
        + Shl<Output = Self::ValTy>
        + Copy
        + BitOrAssign
        + Add<Output = Self::ValTy>
        + Sub<Output = Self::ValTy>
        + PartialOrd
        + AddAssign
        + BitAnd<Output = Self::ValTy>
        + Display;

    fn _default() -> Self;
    #[inline]
    fn default_value(val: impl AsData<Self::ValTy>) -> Self {
        let ins = Self::_default();
        ins.add(val)
    }
    #[inline]
    fn default_range<A: AsData<Self::ValTy>>(range: impl RangeBounds<A>) -> Result<Self> {
        let ins = Self::_default();
        ins.add_range(range)
    }
    #[inline]
    fn default_all() -> Self {
        let mut ins = Self::_default();
        ins._mut_val(Self::DEFAULT_MAX);
        ins
    }
    fn default_array(vals: &[impl AsData<Self::ValTy>]) -> Self {
        let ins = Self::_default();
        ins.add_array(vals)
    }
    fn add_array(mut self, vals: &[impl AsData<Self::ValTy>]) -> Self {
        let mut val = self._val();
        for i in vals {
            val |= Self::ONE << i.as_data();
        }
        self._mut_val(val);
        self
    }
    fn add(mut self, index: impl AsData<Self::ValTy>) -> Self {
        let index = index.as_data();
        self._mut_val(self._val() | (Self::ONE << index));
        self
    }
    fn add_range<A: AsData<Self::ValTy>>(mut self, range: impl RangeBounds<A>) -> Result<Self> {
        let mut first = match range.start_bound() {
            Bound::Unbounded => Self::MIN,
            Bound::Included(first) => first.as_data(),
            Bound::Excluded(first) => first.as_data() + Self::ONE,
        };
        let end = match range.end_bound() {
            Bound::Unbounded => Self::MAX,
            Bound::Included(end) => end.as_data(),
            Bound::Excluded(end) => end.as_data() - Self::ONE,
        };
        if first > end {
            bail!("error:{} > {}", first, end);
        }
        let mut val = self._val();
        while first <= end {
            val |= Self::ONE << first;
            first += Self::ONE;
        }
        self._mut_val(val);
        Ok(self)
    }

    fn to_vec(&self) -> Vec<Self::ValTy> {
        let mut res = Vec::new();
        let val = self._val();
        let mut first = Self::MIN;
        while first <= Self::MAX {
            if (val & (Self::ONE << first)) > Self::ZERO {
                res.push(first);
            }
            first += Self::ONE;
        }
        res
    }
    fn contain<D: AsData<Self::ValTy>>(&self, index: D) -> bool {
        let index = index.as_data();
        let val = self._val();
        val & (Self::ONE << index) > Self::ZERO
    }
    /// 取下一个持有值
    fn next<D: AsData<Self::ValTy>>(&self, index: D) -> Option<Self::ValTy> {
        let mut first = index.as_data() + Self::ONE;
        let val = self._val();
        while first <= Self::MAX {
            if (val & (Self::ONE << first)) > Self::ZERO {
                return Some(first);
            }
            first += Self::ONE;
        }
        None
    }
    /// 取最小的持有值
    fn min_val(&self) -> Self::ValTy {
        let mut first = Self::MIN;
        let val = self._val();
        while first <= Self::MAX {
            if (val & (Self::ONE << first)) > Self::ZERO {
                return first;
            }
            first += Self::ONE;
        }
        unreachable!("it is a bug");
    }
    fn _val(&self) -> Self::ValTy;
    fn _mut_val(&mut self, val: Self::ValTy);
}

pub struct DayConfBuilder {
    month_days: Option<MonthDays>,
    week_days: Option<WeekDays>,
}
impl DayConfBuilder {
    pub fn conf_month_days(self, month_days: MonthDays) -> Self {
        DayConfBuilder {
            month_days: Some(month_days),
            week_days: self.week_days,
        }
    }
    pub fn conf_week_days(self, week_days: WeekDays) -> Self {
        DayConfBuilder {
            month_days: self.month_days,
            week_days: Some(week_days),
        }
    }
    pub fn build_with_hours(self, hours: Hours) -> DayHourConfBuilder {
        DayHourConfBuilder {
            month_days: self.month_days,
            week_days: self.week_days,
            hours,
        }
    }
}
pub struct DayHourConfBuilder {
    month_days: Option<MonthDays>,
    week_days: Option<WeekDays>,
    hours: Hours,
}
impl DayHourConfBuilder {
    /// config minuter
    pub fn build_with_minuter(self, minuters: Minuters) -> DayHourMinuterConfBuilder {
        DayHourMinuterConfBuilder {
            month_days: self.month_days,
            week_days: self.week_days,
            hours: self.hours,
            minuters,
        }
    }
}
pub struct DayHourMinuterConfBuilder {
    month_days: Option<MonthDays>,
    week_days: Option<WeekDays>,
    hours: Hours,
    minuters: Minuters,
}
impl DayHourMinuterConfBuilder {
    pub fn build_with_second(self, seconds: Seconds) -> DayHourMinuterSecondConf {
        DayHourMinuterSecondConf {
            month_days: self.month_days,
            week_days: self.week_days,
            hours: self.hours,
            minuters: self.minuters,
            seconds,
        }
    }
}
#[derive(Debug, Clone)]
pub struct DayHourMinuterSecondConf {
    pub(crate) month_days: Option<MonthDays>,
    pub(crate) week_days: Option<WeekDays>,
    pub(crate) hours: Hours,
    pub(crate) minuters: Minuters,
    pub(crate) seconds: Seconds,
}

impl DayHourMinuterSecondConf {
    pub fn default_month_days(month_days: MonthDays) -> DayConfBuilder {
        DayConfBuilder {
            month_days: Some(month_days),
            week_days: None,
        }
    }
    pub fn default_week_days(week_days: WeekDays) -> DayConfBuilder {
        DayConfBuilder {
            month_days: None,
            week_days: Some(week_days),
        }
    }
    pub fn next(&self) -> Result<u64> {
        let now_local = Local::now().naive_local();
        let datetime = now_local.clone().into();
        // let offset = now_local.clone().offset();
        let next_local = self._next(datetime)?;
        let times = (next_local.timestamp() - now_local.timestamp()) as u64;
        let next_time = NextTime::init(times);
        debug!(
            "now: {}, next: {}, next time is after {:?} = {}s",
            now_local, next_local, next_time, times
        );
        Ok(times)
    }
    fn _next(&self, datetime: DateTime) -> Result<NaiveDateTime> {
        let day_self = self
            .month_days
            .as_ref()
            .map_or(false, |x| x.contain(datetime.month_day))
            || self
                .week_days
                .as_ref()
                .map_or(false, |x| x.contain(datetime.week_day));

        let hour_self = self.hours.contain(datetime.hour);
        let minuter_self = self.minuters.contain(datetime.minuter);

        let (mut day_possible, mut hour_possible, mut minuter_possible, mut second_possible) =
            if day_self {
                if hour_self {
                    if minuter_self {
                        (
                            Possible::Oneself,
                            Possible::Oneself,
                            Possible::Oneself,
                            Possible::Next,
                        )
                    } else {
                        (
                            Possible::Oneself,
                            Possible::Oneself,
                            Possible::Next,
                            Possible::Min,
                        )
                    }
                } else {
                    (
                        Possible::Oneself,
                        Possible::Next,
                        Possible::Min,
                        Possible::Min,
                    )
                }
            } else {
                (Possible::Next, Possible::Min, Possible::Min, Possible::Min)
            };
        let (second, second_recount) = get_val(second_possible, &self.seconds, datetime.second);
        if second_recount {
            second_possible = Possible::Min;
            minuter_possible = Possible::Next;
        }
        let (minuter, minuter_recount) =
            get_val(minuter_possible, &self.minuters, datetime.minuter);
        if minuter_recount {
            minuter_possible = Possible::Min;
            hour_possible = Possible::Next;
        }
        let (hour, hour_recount) = get_val(hour_possible, &self.hours, datetime.hour);
        if hour_recount {
            hour_possible = Possible::Min;
            day_possible = Possible::Next;
        }
        trace!(
            "{:?} {:?} {:?} {:?}",
            day_possible,
            hour_possible,
            minuter_possible,
            second_possible
        );
        let day_week_possible = day_possible;
        let time_next = NaiveTime::from_hms(hour, minuter as u32, second as u32);
        //
        let date_month = if let Some(month_days) = &self.month_days {
            // 计算月日期的下个日期
            let (mut month_day, mut month_day_recount) =
                get_val(day_possible, month_days, datetime.month_day);
            let year = datetime.date.year();
            let month = datetime.date.month();
            trace!(
                "{:?} {:?} {:?} {:?}",
                month_day,
                month_day_recount,
                day_possible,
                datetime.month_day
            );
            if !month_day_recount {
                // 这个月的日期:
                if let Some(date) = NaiveDate::from_ymd_opt(year, month, month_day) {
                    Some(date)
                } else {
                    day_possible = Possible::Min;
                    month_day = month_days.min_val();
                    // 下个月:月数+1,年也许也要加+1
                    Some(add_month(year, month, month_day)?)
                }
            } else {
                // 下个月:月数+1,年也许也要加+1
                Some(add_month(year, month, month_day)?)
            }
        } else {
            None
        };
        let date_week = if let Some(week_days) = &self.week_days {
            let (week_day, week_day_recount) =
                get_val(day_week_possible, week_days, datetime.week_day);
            trace!(
                "week: {:?} {:?} {:?} {:?}",
                week_day,
                day_week_possible,
                datetime.week_day,
                week_day_recount
            );
            if week_day_recount {
                let mut date = datetime.date.clone();
                date += Duration::days((week_day + 7 - datetime.week_day.as_data()) as i64);
                Some(date)
            } else {
                let mut date = datetime.date.clone();
                date += Duration::days((week_day - datetime.week_day.as_data()) as i64);
                Some(date)
            }
        } else {
            None
        };
        trace!("{:?} {:?}", date_month, date_week);
        let date = if let Some(date_month) = date_month {
            if let Some(date_week) = date_week {
                if date_month > date_week {
                    date_week
                } else {
                    date_month
                }
            } else {
                date_month
            }
        } else {
            date_week.unwrap()
        };
        Ok(NaiveDateTime::new(date, time_next))
    }
}
///
/// 依据Possible,获取对应的值
/// return( 获取的值,是否重新开始)
fn get_val<D: Operator>(
    possible: Possible,
    d: &D,
    oneself: impl AsData<D::ValTy>,
) -> (D::ValTy, bool) {
    let mut re_count = false;
    let data = match possible {
        Possible::Min => d.min_val(),
        Possible::Oneself => oneself.as_data(),
        Possible::Next => {
            if let Some(data) = d.next(oneself) {
                data
            } else {
                re_count = true;
                d.min_val()
            }
        }
    };
    (data, re_count)
}

fn add_month(mut year: i32, mut month: u32, day: u32) -> Result<NaiveDate> {
    let mut add_month_times = 0;
    loop {
        month += 1;
        if month > 12 {
            month = 1;
            year += 1;
        }
        if let Some(date) = NaiveDate::from_ymd_opt(year, month, day) {
            return Ok(date);
        }
        add_month_times += 1;
        if add_month_times > 12 {
            bail!("todo")
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Possible {
    Min,
    Oneself,
    Next,
}
#[derive(Debug)]
pub struct NextTime {
    hours: u64,
    minuters: u64,
    seconds: u64,
}
impl NextTime {
    fn init(mut times: u64) -> Self {
        let seconds = times % 60;
        times = times / 60;
        let minuters = times % 60;
        times = times / 60;
        let hours = times % 60;
        Self {
            seconds,
            minuters,
            hours,
        }
    }
}

impl From<MonthDays> for DayConfBuilder {
    fn from(builder: MonthDays) -> Self {
        DayHourMinuterSecondConf::default_month_days(builder)
    }
}
impl From<WeekDays> for DayConfBuilder {
    fn from(builder: WeekDays) -> Self {
        DayHourMinuterSecondConf::default_week_days(builder)
    }
}
impl Debug for Seconds {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.0 == u64::MAX >> 4 {
            write!(f, "all seconds.")
        } else {
            write!(f, "seconds: {:?}.", self.to_vec())
        }
    }
}
impl Debug for Minuters {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.0 == u64::MAX >> 4 {
            write!(f, "all minuters.")
        } else {
            write!(f, "minuters: {:?}.", self.to_vec())
        }
    }
}
impl Debug for Hours {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.0 == u32::MAX >> 8 {
            write!(f, "all hours.")
        } else {
            write!(f, "hours: {:?}.", self.to_vec())
        }
    }
}
impl Debug for MonthDays {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.0 == u32::MAX << 1 {
            write!(f, "all month days.")
        } else {
            write!(f, "month days: {:?}.", self.to_vec())
        }
    }
}
impl Debug for WeekDays {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.0 == u8::MAX << 1 {
            write!(f, "all week days.")
        } else {
            write!(f, "week days: {:?}.", self.to_vec())
        }
    }
}
#[cfg(test)]
mod test {
    use super::{get_val, DayHourMinuterSecondConf, Possible};
    use super::{Hours, Minuters, MonthDays, Operator, Seconds, WeekDays};
    use crate::data::{
        DateTime, Hour, InnerHour, InnerMinuter, InnerMonthDay, InnerSecond, InnerWeekDay, Minuter,
        MonthDay, Second, WeekDay,
    };
    use crate::{D31, H12};
    use anyhow::Result;
    use chrono::{Datelike, NaiveDate};
    use log::LevelFilter;

    #[test]
    fn test_get_val() -> Result<()> {
        let some_seconds = Seconds::default_range(Second::S10..Second::S30)?
            .add_range(Second::S40..=Second::S50)?;
        assert_eq!(
            (10, false),
            get_val(Possible::Min, &some_seconds, Second::S40)
        );
        assert_eq!(
            (40, false),
            get_val(Possible::Oneself, &some_seconds, Second::S40)
        );
        assert_eq!(
            (41, false),
            get_val(Possible::Next, &some_seconds, Second::S40)
        );
        assert_eq!(
            (40, false),
            get_val(Possible::Next, &some_seconds, Second::S29)
        );
        assert_eq!(
            (10, true),
            get_val(Possible::Next, &some_seconds, Second::S50)
        );

        Ok(())
    }

    #[test]
    fn test() -> Result<()> {
        custom_utils::logger::logger_default("timers", LevelFilter::Trace).unwrap();
        let conf = DayHourMinuterSecondConf::default_week_days(WeekDays::default_array(&[
            WeekDay::W5,
            WeekDay::W3,
        ]))
        .conf_month_days(MonthDays::default_array(&[
            MonthDay::D5,
            MonthDay::D15,
            MonthDay::D24,
        ]))
        .build_with_hours(Hours::default_array(&[Hour::H5, Hour::H10, Hour::H15]))
        .build_with_minuter(Minuters::default_array(&[
            Minuter::M15,
            Minuter::M30,
            Minuter::M45,
        ]))
        .build_with_second(Seconds::default_array(&[
            Second::S15,
            Second::S30,
            Second::S45,
        ]));

        let mut dt0 = DateTime {
            date: NaiveDate::from_ymd_opt(2022, 5, 15).unwrap(),
            month_day: InnerMonthDay(15),
            week_day: InnerWeekDay(7),
            hour: InnerHour(10),
            minuter: InnerMinuter(30),
            second: InnerSecond(30),
        };

        {
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dt0.clone();
            dt0_dist.second = InnerSecond(45);
            assert!(dist == dt0_dist);
            dt0_dist.second = InnerSecond(31);
            assert!(dist != dt0_dist);
        }
        //
        {
            dt0.second = InnerSecond(45);
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dt0.clone();
            dt0_dist.second = InnerSecond(15);
            dt0_dist.minuter = InnerMinuter(45);
            assert!(dist == dt0_dist);
        }
        {
            dt0.second = InnerSecond(45);
            dt0.minuter = InnerMinuter(45);
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dt0.clone();
            dt0_dist.second = InnerSecond(15);
            dt0_dist.minuter = InnerMinuter(15);
            dt0_dist.hour = InnerHour(15);
            assert!(dist == dt0_dist);
        }
        {
            dt0.second = InnerSecond(45);
            dt0.minuter = InnerMinuter(45);
            dt0.hour = InnerHour(15);
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dt0.clone();
            dt0_dist.second = InnerSecond(15);
            dt0_dist.minuter = InnerMinuter(15);
            dt0_dist.hour = InnerHour(5);
            dt0_dist.week_day = InnerWeekDay(3);
            dt0_dist.month_day = InnerMonthDay(18);
            dt0_dist.date = NaiveDate::from_ymd_opt(2022, 5, 18).unwrap();
            assert_eq!(dist, dt0_dist);
        }
        // -------------------------------
        let dt0 = DateTime {
            date: NaiveDate::from_ymd_opt(2022, 5, 20).unwrap(),
            month_day: InnerMonthDay(20),
            week_day: InnerWeekDay(5),
            hour: InnerHour(15),
            minuter: InnerMinuter(45),
            second: InnerSecond(45),
        };
        {
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dt0.clone();
            dt0_dist.week_day = InnerWeekDay(2);
            dt0_dist.month_day = InnerMonthDay(24);
            dt0_dist.second = InnerSecond(15);
            dt0_dist.minuter = InnerMinuter(15);
            dt0_dist.hour = InnerHour(5);
            dt0_dist.date = NaiveDate::from_ymd_opt(2022, 5, 24).unwrap();
            assert_eq!(dist, dt0_dist);
        }
        // -------------------------------
        let conf = DayHourMinuterSecondConf::default_week_days(WeekDays::default_array(&[
            WeekDay::W5,
            WeekDay::W3,
        ]))
        .conf_month_days(MonthDays::default_array(&[
            MonthDay::D5,
            MonthDay::D15,
            MonthDay::D31,
        ]))
        .build_with_hours(Hours::default_array(&[Hour::H5, Hour::H10, Hour::H15]))
        .build_with_minuter(Minuters::default_array(&[
            Minuter::M15,
            Minuter::M30,
            Minuter::M45,
        ]))
        .build_with_second(Seconds::default_array(&[
            Second::S15,
            Second::S30,
            Second::S45,
        ]));
        let dt0 = DateTime {
            date: NaiveDate::from_ymd_opt(2022, 4, 29).unwrap(),
            month_day: InnerMonthDay(29),
            week_day: InnerWeekDay(5),
            hour: InnerHour(15),
            minuter: InnerMinuter(45),
            second: InnerSecond(45),
        };
        {
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dt0.clone();
            dt0_dist.week_day = InnerWeekDay(3);
            dt0_dist.month_day = InnerMonthDay(4);
            dt0_dist.second = InnerSecond(15);
            dt0_dist.minuter = InnerMinuter(15);
            dt0_dist.hour = InnerHour(5);
            dt0_dist.date = NaiveDate::from_ymd_opt(2022, 5, 4).unwrap();
            assert_eq!(dist, dt0_dist);
        }
        Ok(())
    }

    #[test]
    fn test_year() -> Result<()> {
        custom_utils::logger::logger_default("timers", LevelFilter::Trace).unwrap();
        let conf = DayHourMinuterSecondConf::default_month_days(MonthDays::default_value(D31))
            .build_with_hours(Hours::default_array(&[H12]))
            .build_with_minuter(Minuters::default_array(&[Minuter::M30]))
            .build_with_second(Seconds::default_array(&[Second::S0]));
        let dt0 = DateTime {
            date: NaiveDate::from_ymd_opt(2021, 12, 31).unwrap(),
            month_day: InnerMonthDay(31),
            week_day: InnerWeekDay(5),
            hour: InnerHour(12),
            minuter: InnerMinuter(30),
            second: InnerSecond(30),
        };
        {
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dist.clone();
            dt0_dist.second = InnerSecond(0);
            dt0_dist.minuter = InnerMinuter(30);
            dt0_dist.hour = InnerHour(12);
            dt0_dist.week_day = InnerWeekDay(1);
            dt0_dist.month_day = InnerMonthDay(31);
            assert!(dist == dt0_dist, "{:?}", dist);
            assert!(dist.date.year() == 2022, "{:?}", dist.date);
            assert!(dist.date.month() == 1, "{:?}", dist.date);
        }
        Ok(())
    }
    #[test]
    fn test_month() -> Result<()> {
        custom_utils::logger::logger_default("timers", LevelFilter::Trace).unwrap();
        let conf = DayHourMinuterSecondConf::default_month_days(MonthDays::default_value(D31))
            .build_with_hours(Hours::default_array(&[H12]))
            .build_with_minuter(Minuters::default_array(&[Minuter::M30]))
            .build_with_second(Seconds::default_array(&[Second::S0]));
        let dt0 = DateTime {
            date: NaiveDate::from_ymd_opt(2022, 1, 31).unwrap(),
            month_day: InnerMonthDay(31),
            week_day: InnerWeekDay(5),
            hour: InnerHour(12),
            minuter: InnerMinuter(30),
            second: InnerSecond(30),
        };
        {
            let dist: DateTime = conf._next(dt0)?.into();
            let mut dt0_dist = dist.clone();
            dt0_dist.second = InnerSecond(0);
            dt0_dist.minuter = InnerMinuter(30);
            dt0_dist.hour = InnerHour(12);
            dt0_dist.week_day = InnerWeekDay(4);
            dt0_dist.month_day = InnerMonthDay(31);
            assert!(dist == dt0_dist, "{:?}", dist);
            assert!(dist.date.year() == 2022, "{:?}", dist.date);
            assert!(dist.date.month() == 3, "{:?}", dist.date);
        }
        Ok(())
    }
}