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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
// Copyright (c) 2023 Joining7943 <joining@posteo.de>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

//! A simple to use, fast and precise gnu relative time parser fully compatible with the [gnu
//! relative time
//! format](https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html)
//!
//! `fundu-gnu` can parse rust strings like
//!
//! `&str` | Duration |
//! -- | -- |
//! `"1hour"`| `Duration::positive(60 * 60, 0)` |
//! `"minute"`| `Duration::positive(60, 0)` |
//! `"2 hours"`| `Duration::positive(2 * 60 * 60, 0)` |
//! `"-3minutes"`| `Duration::negative(3 * 60, 0)` |
//! `"3 mins ago"`| `Duration::negative(3 * 60, 0)` |
//! `"999sec +1day"`| `Duration::positive(86_400 + 999, 0)` |
//! `"55secs500week"`| `Duration::positive(55 + 500 * 604_800, 0)` |
//! `"123456789"`| `Duration::positive(123_456_789, 0)` |
//! `"42fortnight"`| `Duration::positive(42 * 2 * 604_800, 0)` |
//! `"yesterday"`| `Duration::negative(24 * 60 * 60, 0)` |
//! `"now"`| `Duration::positive(0, 0)` |
//! `"today -10seconds"`| `Duration::negative(10, 0)` |
//!
//! `fundu` parses into its own [`Duration`] which is a superset of other `Durations` like
//! [`std::time::Duration`], [`chrono::Duration`] and [`time::Duration`]. See the
//! [documentation](https://docs.rs/fundu/latest/fundu/index.html#fundus-duration) how to easily
//! handle the conversion between these durations.
//!
//! # The Format
//! Supported time units:
//!
//! - `seconds`, `second`, `secs`, `sec`
//! - `minutes`, `minute`, `mins`, `min`
//! - `hours`, `hour`
//! - `days`, `day`
//! - `weeks`, `week`
//! - `fortnights`, `fortnight` (2 weeks)
//! - `months`, `month` (fuzzy)
//! - `years`, `year` (fuzzy)
//!
//! Fuzzy time units are not all of equal duration and depend on a given date. If no date is given
//! when parsing, the system time of `now` in UTC +0 is assumed.
//!
//! The special keywords `yesterday` worth `-1 day`, `tomorrow` worth `+1 day`, `today` and `now`
//! each worth a zero duration are allowed, too. These keywords count as a full duration and don't
//! accept a number, time unit or the `ago` time unit suffix.
//!
//! Summary of the rest of the format:
//!
//! - Only numbers like `"123 days"` and without exponent (like `"3e9 days"`) are allowed. Only
//! seconds time units allow a fraction (like in `"1.123456 secs"`)
//! - Multiple durations like `"1sec 2min"` or `"1week2secs"` in the source string accumulate
//! - Time units without a number (like in `"second"`) are allowed and a value of `1` is assumed.
//! - The parsed duration represents the value exactly (without rounding errors as would occur in
//! floating point calculations) as it is specified in the source string.
//! - The maximum supported duration (`Duration::MAX`) has `u64::MAX` seconds
//! (`18_446_744_073_709_551_615`) and `999_999_999` nano seconds
//! - parsed durations larger than the maximum duration saturate at the maximum duration
//! - Negative durations like `"-1min"` or `"1 week ago"` are allowed
//! - Any leading, trailing whitespace or whitespace between the number and the time unit (like in
//! `"1 \n sec"`) and multiple durations (like in `"1week \n 2minutes"`) is ignored and follows the
//! posix definition of whitespace which is:
//!   - Space (`' '`)
//!   - Horizontal Tab (`'\x09'`)
//!   - Line Feed (`'\x0A'`)
//!   - Vertical Tab (`'\x0B'`)
//!   - Form Feed (`'\x0C'`)
//!   - Carriage Return (`'\x0D'`)
//!
//! Please see also the gnu
//! [documentation](https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html)
//! for a description of their format.
//!
//! # Examples
//!
//! ```rust
//! use fundu_gnu::{Duration, RelativeTimeParser};
//!
//! let parser = RelativeTimeParser::new();
//! assert_eq!(parser.parse("1hour"), Ok(Duration::positive(60 * 60, 0)));
//! assert_eq!(parser.parse("minute"), Ok(Duration::positive(60, 0)));
//! assert_eq!(
//!     parser.parse("2 hours"),
//!     Ok(Duration::positive(2 * 60 * 60, 0))
//! );
//! assert_eq!(parser.parse("second"), Ok(Duration::positive(1, 0)));
//! assert_eq!(parser.parse("-3minutes"), Ok(Duration::negative(3 * 60, 0)));
//! assert_eq!(
//!     parser.parse("3 mins ago"),
//!     Ok(Duration::negative(3 * 60, 0))
//! );
//! assert_eq!(
//!     parser.parse("999sec +1day"),
//!     Ok(Duration::positive(86_400 + 999, 0))
//! );
//! assert_eq!(
//!     parser.parse("55secs500week"),
//!     Ok(Duration::positive(55 + 500 * 7 * 24 * 60 * 60, 0))
//! );
//! assert_eq!(
//!     parser.parse("300mins20secs 5hour"),
//!     Ok(Duration::positive(300 * 60 + 20 + 5 * 60 * 60, 0))
//! );
//! assert_eq!(
//!     parser.parse("123456789"),
//!     Ok(Duration::positive(123_456_789, 0))
//! );
//! assert_eq!(
//!     parser.parse("42fortnight"),
//!     Ok(Duration::positive(42 * 2 * 7 * 24 * 60 * 60, 0))
//! );
//! assert_eq!(
//!     parser.parse("yesterday"),
//!     Ok(Duration::negative(24 * 60 * 60, 0))
//! );
//! assert_eq!(parser.parse("now"), Ok(Duration::positive(0, 0)));
//! assert_eq!(
//!     parser.parse("today -10seconds"),
//!     Ok(Duration::negative(10, 0))
//! );
//! ```
//!
//! If parsing fuzzy units then the fuzz can cause different [`Duration`] based on the given
//! [`DateTime`]:
//!
//! ```rust
//! use fundu_gnu::{DateTime, Duration, RelativeTimeParser};
//!
//! let parser = RelativeTimeParser::new();
//! let date_time = DateTime::from_gregorian_date_time(1970, 1, 1, 0, 0, 0, 0);
//! assert_eq!(
//!     parser.parse_with_date("+1year", Some(date_time)),
//!     Ok(Duration::positive(365 * 86400, 0))
//! );
//! assert_eq!(
//!     parser.parse_with_date("+2month", Some(date_time)),
//!     Ok(Duration::positive((31 + 28) * 86400, 0))
//! );
//!
//! // 1972 is a leap year
//! let date_time = DateTime::from_gregorian_date_time(1972, 1, 1, 0, 0, 0, 0);
//! assert_eq!(
//!     parser.parse_with_date("+1year", Some(date_time)),
//!     Ok(Duration::positive(366 * 86400, 0))
//! );
//! assert_eq!(
//!     parser.parse_with_date("+2month", Some(date_time)),
//!     Ok(Duration::positive((31 + 29) * 86400, 0))
//! );
//! ```
//!
//! If parsing fuzzy units with [`RelativeTimeParser::parse`], the [`DateTime`] of `now` in UTC +0
//! is assumed.
//!
//! The global [`parse`] method does the same without the need to create a [`RelativeTimeParser`].
//!
//! ```rust
//! use fundu_gnu::{parse, Duration};
//!
//! assert_eq!(parse("123 sec"), Ok(Duration::positive(123, 0)));
//! assert_eq!(parse("1sec3min"), Ok(Duration::positive(1 + 3 * 60, 0)));
//! ```
//!
//! Convert fundu's `Duration` into a [`std::time::Duration`]. Converting to [`chrono::Duration`] or
//! [`time::Duration`] works the same but needs the `chrono` or `time` feature activated.
//!
//! ```rust
//! use fundu_gnu::{parse, SaturatingInto};
//!
//! let duration = parse("123 sec").unwrap();
//! assert_eq!(
//!     TryInto::<std::time::Duration>::try_into(duration),
//!     Ok(std::time::Duration::new(123, 0))
//! );
//!
//! // With saturating_into the duration will saturate at the minimum and maximum of
//! // std::time::Duration, so for negative values at std::time::Duration::ZERO and for positive values
//! // at std::time::Duration::MAX
//! assert_eq!(
//!     SaturatingInto::<std::time::Duration>::saturating_into(duration),
//!     std::time::Duration::new(123, 0)
//! );
//! ```
//!
//! [`chrono::Duration`]: https://docs.rs/chrono/latest/chrono/struct.Duration.html
//! [`time::Duration`]: https://docs.rs/time/latest/time/struct.Duration.html

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc(test(attr(warn(unused))))]
#![doc(test(attr(allow(unused_extern_crates))))]
#![warn(missing_docs)]
#![warn(clippy::pedantic)]
#![warn(clippy::default_numeric_fallback)]
#![warn(clippy::else_if_without_else)]
#![warn(clippy::fn_to_numeric_cast_any)]
#![warn(clippy::get_unwrap)]
#![warn(clippy::if_then_some_else_none)]
#![warn(clippy::mixed_read_write_in_expression)]
#![warn(clippy::partial_pub_fields)]
#![warn(clippy::rest_pat_in_fully_bound_structs)]
#![warn(clippy::str_to_string)]
#![warn(clippy::string_to_string)]
#![warn(clippy::todo)]
#![warn(clippy::try_err)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![warn(clippy::unneeded_field_pattern)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::return_self_not_must_use)]
#![allow(clippy::enum_glob_use)]
#![allow(clippy::module_name_repetitions)]

macro_rules! validate {
    ($id:ident, $min:expr, $max:expr) => {{
        #[allow(unused_comparisons)]
        if $id < $min || $id > $max {
            panic!(concat!(
                "Invalid ",
                stringify!($id),
                ": Valid range is ",
                stringify!($min),
                " <= ",
                stringify!($id),
                " <= ",
                stringify!($max)
            ));
        }
    }};

    ($id:ident <= $max:expr) => {{
        #[allow(unused_comparisons)]
        if $id > $max {
            panic!(concat!(
                "Invalid ",
                stringify!($id),
                ": Valid maximum ",
                stringify!($id),
                " is ",
                stringify!($max)
            ));
        }
    }};
}

mod datetime;
mod util;

use std::time::Duration as StdDuration;

pub use datetime::{DateTime, JulianDay};
use fundu_core::config::{Config, ConfigBuilder, Delimiter};
pub use fundu_core::error::{ParseError, TryFromDurationError};
use fundu_core::parse::{
    DurationRepr, Fract, Parser, ReprParserMultiple, ReprParserTemplate, Whole, ATTOS_PER_NANO,
    ATTOS_PER_SEC,
};
use fundu_core::time::TimeUnit::*;
pub use fundu_core::time::{Duration, SaturatingInto};
use fundu_core::time::{Multiplier, TimeUnit, TimeUnitsLike};
#[cfg(test)]
pub use rstest_reuse;
use util::trim_whitespace;

// whitespace definition of: b' ', b'\x09', b'\x0A', b'\x0B', b'\x0C', b'\x0D'
const DELIMITER: Delimiter = |byte| byte == b' ' || byte.wrapping_sub(9) < 5;

const CONFIG: Config = ConfigBuilder::new()
    .allow_delimiter(DELIMITER)
    .allow_ago(DELIMITER)
    .disable_exponent()
    .disable_infinity()
    .allow_negative()
    .number_is_optional()
    .parse_multiple(DELIMITER, None)
    .allow_sign_delimiter(DELIMITER)
    .build();

const TIME_UNITS: TimeUnits = TimeUnits {};
const TIME_KEYWORDS: TimeKeywords = TimeKeywords {};

const SECOND: (TimeUnit, Multiplier) = (Second, Multiplier(1, 0));
const MINUTE: (TimeUnit, Multiplier) = (Minute, Multiplier(1, 0));
const HOUR: (TimeUnit, Multiplier) = (Hour, Multiplier(1, 0));
const DAY: (TimeUnit, Multiplier) = (Day, Multiplier(1, 0));
const WEEK: (TimeUnit, Multiplier) = (Week, Multiplier(1, 0));
const FORTNIGHT: (TimeUnit, Multiplier) = (Week, Multiplier(2, 0));
const MONTH: (TimeUnit, Multiplier) = (Month, Multiplier(1, 0));
const YEAR: (TimeUnit, Multiplier) = (Year, Multiplier(1, 0));

const PARSER: RelativeTimeParser<'static> = RelativeTimeParser::new();

enum FuzzyUnit {
    Month,
    Year,
}

struct FuzzyTime {
    unit: FuzzyUnit,
    value: i64,
}

impl FuzzyTime {}

enum ParseFuzzyOutput {
    Duration(Duration),
    FuzzyTime(FuzzyTime),
}

struct DurationReprParser<'a>(DurationRepr<'a>);

impl<'a> DurationReprParser<'a> {
    fn parse(&mut self) -> Result<Duration, ParseError> {
        let is_negative = self.0.is_negative.unwrap_or_default();
        let time_unit = self.0.unit.unwrap_or(self.0.default_unit);

        let (whole, fract) = match (self.0.whole.take(), self.0.fract.take()) {
            (None, None) if self.0.number_is_optional => {
                let Multiplier(coefficient, _) = time_unit.multiplier() * self.0.multiplier;
                let duration_is_negative = is_negative ^ coefficient.is_negative();
                return Ok(Self::calculate_duration(
                    duration_is_negative,
                    1,
                    0,
                    coefficient,
                ));
            }
            (None, None) => unreachable!(), // cov:excl-line
            (None, Some(fract)) if time_unit == TimeUnit::Second => {
                (Whole(fract.0, fract.0), fract)
            }
            (Some(whole), None) => {
                let fract_start_and_end = whole.1;
                (whole, Fract(fract_start_and_end, fract_start_and_end))
            }
            (Some(whole), Some(fract)) if time_unit == TimeUnit::Second => (whole, fract),
            (Some(_) | None, Some(_)) => {
                return Err(ParseError::InvalidInput(
                    "Fraction only allowed together with seconds as time unit".to_owned(),
                ));
            }
        };

        // Panic on overflow during the multiplication of the multipliers or adding the exponents
        let Multiplier(coefficient, _) = time_unit.multiplier() * self.0.multiplier;
        let duration_is_negative = is_negative ^ coefficient.is_negative();

        let digits = self.0.input;
        let result = Whole::parse(&digits[whole.0..whole.1], None, None)
            .map(|s| (s, Fract::parse(&digits[fract.0..fract.1], None, None)));

        // interpret the result and a seconds overflow as maximum (minimum) `Duration`
        let (seconds, attos) = match result {
            Ok(value) => value,
            Err(ParseError::Overflow) if duration_is_negative => {
                return Ok(Duration::MIN);
            }
            Err(ParseError::Overflow) => {
                return Ok(Duration::MAX);
            }
            // only ParseError::Overflow is returned by `Seconds::parse`
            Err(_) => unreachable!(), // cov:excl-line
        };

        Ok(Self::calculate_duration(
            duration_is_negative,
            seconds,
            attos,
            coefficient,
        ))
    }

    fn parse_fuzzy(&mut self) -> Result<ParseFuzzyOutput, ParseError> {
        let fuzzy_unit = match self.0.unit {
            Some(Month) => FuzzyUnit::Month,
            Some(Year) => FuzzyUnit::Year,
            _ => return self.parse().map(ParseFuzzyOutput::Duration),
        };

        if self.0.fract.is_some() {
            return Err(ParseError::InvalidInput(
                "Fraction only allowed together with seconds as time unit".to_owned(),
            ));
        }

        let is_negative = self.0.is_negative.unwrap_or_default();
        let whole = match self.0.whole.take() {
            Some(whole) => whole,
            None if self.0.number_is_optional => {
                return Ok(ParseFuzzyOutput::FuzzyTime(FuzzyTime {
                    unit: fuzzy_unit,
                    value: if is_negative { -1 } else { 1 },
                }));
            }
            None => unreachable!(), // cov:excl-line
        };

        let result = Whole::parse(&self.0.input[whole.0..whole.1], None, None);
        // interpret the result and a years or month overflow as maximum (minimum) `Duration`
        let value = match result {
            Ok(value) if is_negative => match i64::try_from(value) {
                Ok(value) => -value,
                Err(_) => i64::MIN,
            },
            Ok(value) => i64::try_from(value).unwrap_or(i64::MAX),
            Err(ParseError::Overflow) if is_negative => {
                return Ok(ParseFuzzyOutput::FuzzyTime(FuzzyTime {
                    unit: fuzzy_unit,
                    value: i64::MIN,
                }));
            }
            Err(ParseError::Overflow) => {
                return Ok(ParseFuzzyOutput::FuzzyTime(FuzzyTime {
                    unit: fuzzy_unit,
                    value: i64::MAX,
                }));
            }
            // only ParseError::Overflow is returned by `Seconds::parse`
            Err(_) => unreachable!(), // cov:excl-line
        };

        Ok(ParseFuzzyOutput::FuzzyTime(FuzzyTime {
            unit: fuzzy_unit,
            value,
        }))
    }

    fn calculate_duration(
        is_negative: bool,
        seconds: u64,
        attos: u64,
        coefficient: i64,
    ) -> Duration {
        if seconds == 0 && attos == 0 {
            Duration::ZERO
        } else if coefficient == 1 || coefficient == -1 {
            Duration::from_std(
                is_negative,
                StdDuration::new(seconds, (attos / ATTOS_PER_NANO).try_into().unwrap()),
            )
        } else {
            let unsigned_coefficient = coefficient.unsigned_abs();

            let attos = u128::from(attos) * u128::from(unsigned_coefficient);
            match seconds.checked_mul(unsigned_coefficient).and_then(|s| {
                s.checked_add((attos / u128::from(ATTOS_PER_SEC)).try_into().unwrap())
            }) {
                Some(s) => Duration::from_std(
                    is_negative,
                    StdDuration::new(
                        s,
                        ((attos / u128::from(ATTOS_PER_NANO)) % 1_000_000_000) as u32,
                    ),
                ),
                None if is_negative => Duration::MIN,
                None => Duration::MAX,
            }
        }
    }
}

/// The main gnu relative time parser
///
/// Note this parser can be created as const at compile time.
///
/// # Examples
///
/// ```rust
/// use fundu_gnu::{Duration, RelativeTimeParser};
///
/// const PARSER: RelativeTimeParser = RelativeTimeParser::new();
///
/// let parser = &PARSER;
/// assert_eq!(parser.parse("1hour"), Ok(Duration::positive(60 * 60, 0)));
/// assert_eq!(parser.parse("minute"), Ok(Duration::positive(60, 0)));
/// assert_eq!(
///     parser.parse("2 hours"),
///     Ok(Duration::positive(2 * 60 * 60, 0))
/// );
/// assert_eq!(parser.parse("second"), Ok(Duration::positive(1, 0)));
/// assert_eq!(parser.parse("-3minutes"), Ok(Duration::negative(3 * 60, 0)));
/// assert_eq!(
///     parser.parse("3 mins ago"),
///     Ok(Duration::negative(3 * 60, 0))
/// );
/// assert_eq!(
///     parser.parse("999sec +1day"),
///     Ok(Duration::positive(86_400 + 999, 0))
/// );
/// assert_eq!(
///     parser.parse("55secs500week"),
///     Ok(Duration::positive(55 + 500 * 7 * 24 * 60 * 60, 0))
/// );
/// assert_eq!(
///     parser.parse("300mins20secs 5hour"),
///     Ok(Duration::positive(300 * 60 + 20 + 5 * 60 * 60, 0))
/// );
/// assert_eq!(
///     parser.parse("123456789"),
///     Ok(Duration::positive(123_456_789, 0))
/// );
/// assert_eq!(
///     parser.parse("42fortnight"),
///     Ok(Duration::positive(42 * 2 * 7 * 24 * 60 * 60, 0))
/// );
/// assert_eq!(
///     parser.parse("yesterday"),
///     Ok(Duration::negative(24 * 60 * 60, 0))
/// );
/// assert_eq!(parser.parse("now"), Ok(Duration::positive(0, 0)));
/// assert_eq!(
///     parser.parse("today -10seconds"),
///     Ok(Duration::negative(10, 0))
/// );
/// ```
#[derive(Debug, Eq, PartialEq)]
pub struct RelativeTimeParser<'a> {
    raw: Parser<'a>,
}

impl<'a> RelativeTimeParser<'a> {
    /// Create a new `RelativeTimeParser`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fundu_gnu::{Duration, RelativeTimeParser};
    ///
    /// let parser = RelativeTimeParser::new();
    /// assert_eq!(
    ///     parser.parse("2hours"),
    ///     Ok(Duration::positive(2 * 60 * 60, 0))
    /// );
    /// assert_eq!(parser.parse("123"), Ok(Duration::positive(123, 0)));
    /// assert_eq!(
    ///     parser.parse("3min +10sec"),
    ///     Ok(Duration::positive(3 * 60 + 10, 0))
    /// );
    /// ```
    pub const fn new() -> Self {
        Self {
            raw: Parser::with_config(CONFIG),
        }
    }
    /// Parse the `source` string into a [`Duration`] relative to the date and time of `now`
    ///
    /// Any leading and trailing whitespace is ignored. The parser saturates at the maximum of
    /// [`Duration::MAX`].
    ///
    /// # Errors
    ///
    /// Returns a [`ParseError`] if an error during the parsing process occurred
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fundu_gnu::{Duration, RelativeTimeParser};
    ///
    /// let parser = RelativeTimeParser::new();
    /// assert_eq!(
    ///     parser.parse("2hours"),
    ///     Ok(Duration::positive(2 * 60 * 60, 0))
    /// );
    /// assert_eq!(parser.parse("12 seconds"), Ok(Duration::positive(12, 0)));
    /// assert_eq!(
    ///     parser.parse("123456789"),
    ///     Ok(Duration::positive(123_456_789, 0))
    /// );
    /// assert_eq!(
    ///     parser.parse("yesterday"),
    ///     Ok(Duration::negative(24 * 60 * 60, 0))
    /// );
    /// ```
    #[inline]
    pub fn parse(&self, source: &str) -> Result<Duration, ParseError> {
        self.parse_with_date(source, None)
    }

    /// Parse the `source` string into a [`Duration`] relative to the optionally given `date`
    ///
    /// If the `date` is `None`, then the system time of `now` is assumed. Time units of `year` and
    /// `month` are parsed fuzzy since years and months are not all of equal length. Any leading and
    /// trailing whitespace is ignored. The parser saturates at the maximum of [`Duration::MAX`].
    ///
    /// # Errors
    ///
    /// Returns a [`ParseError`] if an error during the parsing process occurred or the calculation
    /// of the calculation of the given `date` plus the duration of the `source` string overflows.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fundu_gnu::{DateTime, Duration, RelativeTimeParser};
    ///
    /// let parser = RelativeTimeParser::new();
    /// assert_eq!(
    ///     parser.parse_with_date("2hours", None),
    ///     Ok(Duration::positive(2 * 60 * 60, 0))
    /// );
    ///
    /// let date_time = DateTime::from_gregorian_date_time(1970, 2, 1, 0, 0, 0, 0);
    /// assert_eq!(
    ///     parser.parse_with_date("+1month", Some(date_time)),
    ///     Ok(Duration::positive(28 * 86400, 0))
    /// );
    /// assert_eq!(
    ///     parser.parse_with_date("+1year", Some(date_time)),
    ///     Ok(Duration::positive(365 * 86400, 0))
    /// );
    ///
    /// // 1972 is a leap year
    /// let date_time = DateTime::from_gregorian_date_time(1972, 2, 1, 0, 0, 0, 0);
    /// assert_eq!(
    ///     parser.parse_with_date("+1month", Some(date_time)),
    ///     Ok(Duration::positive(29 * 86400, 0))
    /// );
    /// assert_eq!(
    ///     parser.parse_with_date("+1year", Some(date_time)),
    ///     Ok(Duration::positive(366 * 86400, 0))
    /// );
    /// ```
    pub fn parse_with_date(
        &self,
        source: &str,
        date: Option<DateTime>,
    ) -> Result<Duration, ParseError> {
        let (years, months, duration) = self.parse_fuzzy(source)?;
        if years == 0 && months == 0 {
            return Ok(duration);
        }

        // Delay the costly system call to get the utc time as late as possible
        let orig = date.unwrap_or_else(DateTime::now_utc);
        orig.checked_add_duration(&duration)
            .and_then(|date| {
                date.checked_add_gregorian(years, months, 0)
                    .and_then(|date| date.duration_since(orig))
            })
            .ok_or(ParseError::Overflow)
    }

    /// Parse the `source` string extracting `year` and `month` time units from the [`Duration`]
    ///
    /// Unlike [`RelativeTimeParser::parse`] and [`RelativeTimeParser::parse_with_date`] this method
    /// won't interpret the parsed `year` and `month` time units but simply returns the values
    /// parsed from the `source` string.
    ///
    /// The returned tuple (`years`, `months`, `Duration`) contains in the first component the
    /// amount parsed `years` as `i64`, in the second component the parsed `months` as `i64` and in
    /// the last component the rest of the parsed time units accumulated as [`Duration`].
    ///
    /// # Errors
    ///
    /// Returns a [`ParseError`] if an error during the parsing process occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fundu_gnu::{Duration, RelativeTimeParser};
    ///
    /// let parser = RelativeTimeParser::new();
    /// assert_eq!(
    ///     parser.parse_fuzzy("2hours"),
    ///     Ok((0, 0, Duration::positive(2 * 60 * 60, 0)))
    /// );
    /// assert_eq!(
    ///     parser.parse_fuzzy("2hours +123month -10years"),
    ///     Ok((-10, 123, Duration::positive(2 * 60 * 60, 0)))
    /// );
    /// ```
    #[allow(clippy::missing_panics_doc)]
    pub fn parse_fuzzy(&self, source: &str) -> Result<(i64, i64, Duration), ParseError> {
        let trimmed = trim_whitespace(source);

        let mut duration = Duration::ZERO;
        let mut years = 0i64;
        let mut months = 0i64;

        // the unwraps are safe here because both config values are defined
        let mut parser = &mut ReprParserMultiple::new(
            trimmed,
            self.raw.config.delimiter_multiple.unwrap(),
            self.raw.config.conjunctions.unwrap_or_default(),
        );

        loop {
            let (duration_repr, maybe_parser) =
                parser.parse(&self.raw.config, &TIME_UNITS, Some(&TIME_KEYWORDS))?;

            match DurationReprParser(duration_repr).parse_fuzzy()? {
                ParseFuzzyOutput::Duration(parsed_duration) => {
                    duration = if duration.is_zero() {
                        parsed_duration
                    } else if parsed_duration.is_zero() {
                        duration
                    } else {
                        duration.saturating_add(parsed_duration)
                    }
                }
                ParseFuzzyOutput::FuzzyTime(fuzzy) => match fuzzy.unit {
                    FuzzyUnit::Month => months = months.saturating_add(fuzzy.value),
                    FuzzyUnit::Year => years = years.saturating_add(fuzzy.value),
                },
            }
            match maybe_parser {
                Some(p) => parser = p,
                None => break Ok((years, months, duration)),
            }
        }
    }
}

impl<'a> Default for RelativeTimeParser<'a> {
    fn default() -> Self {
        Self::new()
    }
}

/// This struct is used internally to hold the time units used by gnu
struct TimeUnits {}

impl TimeUnitsLike for TimeUnits {
    #[inline]
    fn is_empty(&self) -> bool {
        false
    }

    #[inline]
    fn get(&self, identifier: &str) -> Option<(TimeUnit, Multiplier)> {
        match identifier {
            "sec" | "secs" | "second" | "seconds" => Some(SECOND),
            "min" | "mins" | "minute" | "minutes" => Some(MINUTE),
            "hour" | "hours" => Some(HOUR),
            "day" | "days" => Some(DAY),
            "week" | "weeks" => Some(WEEK),
            "fortnight" | "fortnights" => Some(FORTNIGHT),
            "month" | "months" => Some(MONTH),
            "year" | "years" => Some(YEAR),
            _ => None,
        }
    }
}

/// This struct is used internally to hold the time keywords used by gnu
struct TimeKeywords {}

impl TimeUnitsLike for TimeKeywords {
    #[inline]
    fn is_empty(&self) -> bool {
        false
    }

    #[inline]
    fn get(&self, identifier: &str) -> Option<(TimeUnit, Multiplier)> {
        match identifier {
            "yesterday" => Some((TimeUnit::Day, Multiplier(-1, 0))),
            "tomorrow" => Some((TimeUnit::Day, Multiplier(1, 0))),
            "now" | "today" => Some((TimeUnit::Day, Multiplier(0, 0))),
            _ => None,
        }
    }
}

/// Parse the `source` string into a [`Duration`]
///
/// Any leading and trailing whitespace is ignored. The parser saturates at the maximum of
/// [`Duration::MAX`].
///
/// This method is equivalent to [`RelativeTimeParser::parse`]. See also the documentation of
/// [`RelativeTimeParser::parse`].
///
/// # Errors
///
/// Returns a [`ParseError`] if an error during the parsing process occurred
///
/// # Examples
///
/// ```rust
/// use fundu_gnu::{parse, Duration};
///
/// assert_eq!(parse("2hours"), Ok(Duration::positive(2 * 60 * 60, 0)));
/// assert_eq!(parse("12 seconds"), Ok(Duration::positive(12, 0)));
/// assert_eq!(parse("123456789"), Ok(Duration::positive(123_456_789, 0)));
/// assert_eq!(parse("yesterday"), Ok(Duration::negative(24 * 60 * 60, 0)));
/// ```
pub fn parse(source: &str) -> Result<Duration, ParseError> {
    PARSER.parse(source)
}

/// Parse the `source` string into a [`Duration`] relative to the optionally given `date`
///
/// If the `date` is `None`, then the system time of `now` is assumed. Time units of `year` and
/// `month` are parsed fuzzy since years and months are not all of equal length. Any leading and
/// trailing whitespace is ignored. The parser saturates at the maximum of [`Duration::MAX`].
///
/// This method is equivalent to [`RelativeTimeParser::parse_with_date`]. See also the documentation
/// of [`RelativeTimeParser::parse_with_date`].
///
/// # Errors
///
/// Returns a [`ParseError`] if an error during the parsing process occurred or the calculation
/// of the calculation of the given `date` plus the duration of the `source` string overflows.
///
/// # Examples
///
/// ```rust
/// use fundu_gnu::{parse_with_date, DateTime, Duration};
///
/// assert_eq!(
///     parse_with_date("2hours", None),
///     Ok(Duration::positive(2 * 60 * 60, 0))
/// );
///
/// let date_time = DateTime::from_gregorian_date_time(1970, 2, 1, 0, 0, 0, 0);
/// assert_eq!(
///     parse_with_date("+1month", Some(date_time)),
///     Ok(Duration::positive(28 * 86400, 0))
/// );
/// assert_eq!(
///     parse_with_date("+1year", Some(date_time)),
///     Ok(Duration::positive(365 * 86400, 0))
/// );
///
/// // 1972 is a leap year
/// let date_time = DateTime::from_gregorian_date_time(1972, 2, 1, 0, 0, 0, 0);
/// assert_eq!(
///     parse_with_date("+1month", Some(date_time)),
///     Ok(Duration::positive(29 * 86400, 0))
/// );
/// assert_eq!(
///     parse_with_date("+1year", Some(date_time)),
///     Ok(Duration::positive(366 * 86400, 0))
/// );
/// ```
pub fn parse_with_date(source: &str, date: Option<DateTime>) -> Result<Duration, ParseError> {
    PARSER.parse_with_date(source, date)
}

/// Parse the `source` string extracting `year` and `month` time units from the [`Duration`]
///
/// Unlike [`RelativeTimeParser::parse`] and [`RelativeTimeParser::parse_with_date`] this method
/// won't interpret the parsed `year` and `month` time units but simply returns the values
/// parsed from the `source` string.
///
/// The returned tuple (`years`, `months`, `Duration`) contains in the first component the
/// amount parsed `years` as `i64`, in the second component the parsed `months` as `i64` and in
/// the last component the rest of the parsed time units accumulated as [`Duration`].
///
/// This method is equivalent to [`RelativeTimeParser::parse_fuzzy`]. See also the documentation of
/// [`RelativeTimeParser::parse_fuzzy`].
///
/// # Errors
///
/// Returns a [`ParseError`] if an error during the parsing process occurred.
///
/// # Examples
///
/// ```rust
/// use fundu_gnu::{parse_fuzzy, Duration};
///
/// assert_eq!(
///     parse_fuzzy("2hours"),
///     Ok((0, 0, Duration::positive(2 * 60 * 60, 0)))
/// );
/// assert_eq!(
///     parse_fuzzy("2hours +123month -10years"),
///     Ok((-10, 123, Duration::positive(2 * 60 * 60, 0)))
/// );
/// ```
pub fn parse_fuzzy(source: &str) -> Result<(i64, i64, Duration), ParseError> {
    PARSER.parse_fuzzy(source)
}

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

    #[test]
    fn test_relative_time_parser_new() {
        assert_eq!(RelativeTimeParser::new(), RelativeTimeParser::default());
    }

    #[test]
    fn test_time_units_is_empty_returns_false() {
        assert!(!TimeUnits {}.is_empty());
    }

    #[test]
    fn test_keywords_is_empty_returns_false() {
        assert!(!TimeKeywords {}.is_empty());
    }
}