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
//! ISO 8601 combined date and time with local time zone.

use crate::{AvroValue, JsonValue};
use chrono::{
    format::ParseError, Datelike, Days, Local, Months, NaiveDate, NaiveDateTime, NaiveTime,
    SecondsFormat, TimeZone, Timelike, Utc, Weekday,
};
use serde::{Deserialize, Serialize, Serializer};
use std::{
    fmt,
    ops::{Add, AddAssign, Sub, SubAssign},
    str::FromStr,
    time::Duration,
};

mod date;
mod duration;
mod time;

pub use date::Date;
pub use duration::{parse_duration, ParseDurationError};
pub use time::Time;

/// Alias for [`chrono::DateTime<Local>`](chrono::DateTime).
type LocalDateTime = chrono::DateTime<Local>;

/// A wrapper type for [`chrono::DateTime<Local>`](chrono::DateTime).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize)]
pub struct DateTime(LocalDateTime);

impl DateTime {
    /// Returns a new instance which corresponds to the current date and time.
    #[inline]
    pub fn now() -> Self {
        Self(Local::now())
    }

    /// Returns the number of non-leap seconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn current_timestamp() -> i64 {
        Utc::now().timestamp()
    }

    /// Returns the number of non-leap milliseconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn current_timestamp_millis() -> i64 {
        Utc::now().timestamp_millis()
    }

    /// Returns the number of non-leap microseconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn current_timestamp_micros() -> i64 {
        Utc::now().timestamp_micros()
    }

    /// Returns a new instance corresponding to a UTC date and time,
    /// from the number of non-leap seconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn from_timestamp(secs: i64) -> Self {
        let dt = NaiveDateTime::from_timestamp_opt(secs, 0).unwrap_or_default();
        Self(Local.from_utc_datetime(&dt))
    }

    /// Returns a new instance corresponding to a UTC date and time,
    /// from the number of non-leap milliseconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn from_timestamp_millis(millis: i64) -> Self {
        let dt = NaiveDateTime::from_timestamp_millis(millis).unwrap_or_default();
        Self(Local.from_utc_datetime(&dt))
    }

    /// Returns a new instance corresponding to a UTC date and time,
    /// from the number of non-leap microseconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn from_timestamp_micros(micros: i64) -> Self {
        let dt = NaiveDateTime::from_timestamp_micros(micros).unwrap_or_default();
        Self(Local.from_utc_datetime(&dt))
    }

    /// Returns the number of non-leap seconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn timestamp(&self) -> i64 {
        self.0.timestamp()
    }

    /// Returns the number of non-leap milliseconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn timestamp_millis(&self) -> i64 {
        self.0.timestamp_millis()
    }

    /// Returns the number of non-leap microseconds since the midnight UTC on January 1, 1970.
    #[inline]
    pub fn timestamp_micros(&self) -> i64 {
        self.0.timestamp_micros()
    }

    /// Returns the difference in seconds between `self` and
    /// the same date-time as evaluated in the UTC time zone.
    #[inline]
    pub fn timezone_offset(&self) -> i32 {
        self.0.offset().local_minus_utc()
    }

    /// Parses an RFC 2822 date and time.
    #[inline]
    pub fn parse_utc_str(s: &str) -> Result<Self, ParseError> {
        let datetime = chrono::DateTime::parse_from_rfc2822(s)?;
        Ok(Self(datetime.with_timezone(&Local)))
    }

    /// Parses an RFC 3339 and ISO 8601 date and time.
    #[inline]
    pub fn parse_iso_str(s: &str) -> Result<Self, ParseError> {
        let datetime = chrono::DateTime::parse_from_rfc3339(s)?;
        Ok(Self(datetime.with_timezone(&Local)))
    }

    /// Parses a string with the specified format string.
    /// See [`format::strftime`](chrono::format::strftime) for the supported escape sequences.
    #[inline]
    pub fn parse_from_str(s: &str, fmt: &str) -> Result<Self, ParseError> {
        let datetime = chrono::DateTime::parse_from_str(s, fmt)?;
        Ok(Self(datetime.with_timezone(&Local)))
    }

    /// Returns a UTC timestamp string.
    #[inline]
    pub fn to_utc_timestamp(&self) -> String {
        let datetime = self.0.with_timezone(&Utc);
        format!("{}", datetime.format("%Y-%m-%d %H:%M:%S%.6f"))
    }

    /// Returns an RFC 2822 date and time string.
    #[inline]
    pub fn to_utc_string(&self) -> String {
        let datetime = self.0.with_timezone(&Utc);
        format!("{} GMT", datetime.to_rfc2822().trim_end_matches(" +0000"))
    }

    /// Return an RFC 3339 and ISO 8601 date and time string with subseconds
    /// formatted as [`SecondsFormat::Millis`].
    #[inline]
    pub fn to_iso_string(&self) -> String {
        let datetime = self.0.with_timezone(&Utc);
        datetime.to_rfc3339_opts(SecondsFormat::Millis, true)
    }

    /// Formats the combined date and time with the specified format string.
    /// See [`format::strftime`](chrono::format::strftime) for the supported escape sequences.
    #[inline]
    pub fn format(&self, fmt: &str) -> String {
        format!("{}", self.0.format(fmt))
    }

    /// Returns a date-only string in the format `%Y-%m-%d`.
    #[inline]
    pub fn format_date(&self) -> String {
        format!("{}", self.0.format("%Y-%m-%d"))
    }

    /// Returns a time-only string in the format `%H:%M:%S`.
    #[inline]
    pub fn format_time(&self) -> String {
        format!("{}", self.0.format("%H:%M:%S"))
    }

    /// Returns a date-time string in the format `%Y-%m-%d %H:%M:%S` with the `Local` time zone.
    pub fn format_local(&self) -> String {
        format!("{}", self.0.format("%Y-%m-%d %H:%M:%S"))
    }

    /// Returns a date-time string in the format `%Y-%m-%d %H:%M:%S` with the `Utc` time zone.
    #[inline]
    pub fn format_utc(&self) -> String {
        let datetime = self.0.with_timezone(&Utc);
        format!("{}", datetime.format("%Y-%m-%d %H:%M:%S"))
    }

    /// Returns the amount of time elapsed from another datetime to this one,
    /// or zero duration if that datetime is later than this one.
    #[inline]
    pub fn duration_since(&self, earlier: DateTime) -> Duration {
        (self.0 - earlier.0).to_std().unwrap_or_default()
    }

    /// Returns the duration of time between `self` and `other`.
    #[inline]
    pub fn span_between(&self, other: DateTime) -> Duration {
        let duration = if self > &other {
            self.0 - other.0
        } else {
            other.0 - self.0
        };
        duration.to_std().unwrap_or_default()
    }

    /// Returns the duration of time between `self` and `DateTime::now()`.
    #[inline]
    pub fn span_between_now(&self) -> Duration {
        self.span_between(Self::now())
    }

    /// Returns the duration of time from `self` to `DateTime::now()`.
    #[inline]
    pub fn span_before_now(&self) -> Option<Duration> {
        let current = Self::now();
        if self <= &current {
            (current.0 - self.0).to_std().ok()
        } else {
            None
        }
    }

    /// Returns the duration of time from `DateTime::now()` to `self`.
    #[inline]
    pub fn span_after_now(&self) -> Option<Duration> {
        let current = Self::now();
        if self >= &current {
            (self.0 - current.0).to_std().ok()
        } else {
            None
        }
    }

    /// Retrieves the date component.
    #[inline]
    pub fn date(&self) -> Date {
        self.0.date_naive().into()
    }

    /// Retrieves the time component.
    #[inline]
    pub fn time(&self) -> Time {
        self.0.time().into()
    }

    /// Returns the year number in the calendar date.
    #[inline]
    pub fn year(&self) -> i32 {
        self.0.year()
    }

    /// Returns the month number starting from 1.
    ///
    /// The return value ranges from 1 to 12.
    #[inline]
    pub fn month(&self) -> u32 {
        self.0.month()
    }

    /// Returns the day of month starting from 1.
    ///
    /// The return value ranges from 1 to 31. (The last day of month differs by months.)
    #[inline]
    pub fn day(&self) -> u32 {
        self.0.day()
    }

    /// Returns the hour number from 0 to 23.
    #[inline]
    pub fn hour(&self) -> u32 {
        self.0.hour()
    }

    /// Returns the minute number from 0 to 59.
    #[inline]
    pub fn minute(&self) -> u32 {
        self.0.minute()
    }

    /// Returns the second number from 0 to 59.
    #[inline]
    pub fn second(&self) -> u32 {
        self.0.second()
    }

    /// Returns the millisecond number from 0 to 999.
    #[inline]
    pub fn millisecond(&self) -> u32 {
        self.0.timestamp_subsec_millis() % 1000
    }

    /// Returns the microsecond number from 0 to 999.
    #[inline]
    pub fn microsecond(&self) -> u32 {
        self.0.timestamp_subsec_micros() % 1000
    }

    /// Returns the nanosecond number from 0 to 999.
    #[inline]
    pub fn nanosecond(&self) -> u32 {
        self.0.timestamp_subsec_nanos() % 1_000_000
    }

    /// Returns the ISO week number starting from 1.
    ///
    /// The return value ranges from 1 to 53. (The last week of year differs by years.)
    #[inline]
    pub fn week(&self) -> u32 {
        self.0.iso_week().week()
    }

    /// Returns the day of year starting from 1.
    ///
    /// The return value ranges from 1 to 366. (The last day of year differs by years.)
    #[inline]
    pub fn day_of_year(&self) -> u32 {
        self.0.ordinal()
    }

    /// Returns the day of week starting from 0 (Sunday) to 6 (Saturday).
    #[inline]
    pub fn day_of_week(&self) -> u8 {
        self.iso_day_of_week() % 7
    }

    /// Returns the ISO day of week starting from 1 (Monday) to 7 (Sunday).
    #[inline]
    pub fn iso_day_of_week(&self) -> u8 {
        (self.0.weekday() as u8) + 1
    }

    /// Returns `true` if the current year is a leap year.
    #[inline]
    pub fn is_leap_year(&self) -> bool {
        self.0.date_naive().leap_year()
    }

    /// Returns `true` if the current day is weekend.
    #[inline]
    pub fn is_weekend(&self) -> bool {
        matches!(self.0.weekday(), Weekday::Sat | Weekday::Sun)
    }

    /// Returns the number of days in the current year.
    #[inline]
    pub fn days_in_current_year(&self) -> u32 {
        if self.is_leap_year() {
            366
        } else {
            365
        }
    }

    /// Returns the number of days in the current month.
    pub fn days_in_current_month(&self) -> u32 {
        let month = self.month();
        match month {
            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
            4 | 6 | 9 | 11 => 30,
            2 => {
                if self.is_leap_year() {
                    29
                } else {
                    28
                }
            }
            _ => panic!("invalid month: {month}"),
        }
    }

    /// Returns the start of the current year.
    pub fn start_of_current_year(&self) -> Self {
        let year = self.year();
        let date = NaiveDate::from_ymd_opt(year, 1, 1).unwrap_or_default();
        let dt = NaiveDateTime::new(date, NaiveTime::default());
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the end of the current year.
    pub fn end_of_current_year(&self) -> Self {
        let year = self.year();
        let dt = NaiveDate::from_ymd_opt(year, 12, 31)
            .and_then(|date| date.and_hms_milli_opt(23, 59, 59, 1_000))
            .unwrap_or_default();
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the start of the current month.
    pub fn start_of_current_month(&self) -> Self {
        let year = self.year();
        let month = self.month();
        let date = NaiveDate::from_ymd_opt(year, month, 1).unwrap_or_default();
        let dt = NaiveDateTime::new(date, NaiveTime::default());
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the end of the current month.
    pub fn end_of_current_month(&self) -> Self {
        let year = self.year();
        let month = self.month();
        let day = self.days_in_current_month();
        let dt = NaiveDate::from_ymd_opt(year, month, day)
            .and_then(|date| date.and_hms_milli_opt(23, 59, 59, 1_000))
            .unwrap_or_default();
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the start of the current day.
    pub fn start_of_current_day(&self) -> Self {
        let date = self.0.date_naive();
        let dt = NaiveDateTime::new(date, NaiveTime::default());
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the end of the current day.
    pub fn end_of_current_day(&self) -> Self {
        let date = self.0.date_naive();
        let dt = date
            .and_hms_milli_opt(23, 59, 59, 1_000)
            .unwrap_or_default();
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the start of the year.
    pub fn start_of_year(year: i32) -> Self {
        let date = NaiveDate::from_ymd_opt(year, 1, 1).unwrap_or_default();
        let dt = NaiveDateTime::new(date, NaiveTime::default());
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the end of the year.
    pub fn end_of_year(year: i32) -> Self {
        let dt = NaiveDate::from_ymd_opt(year + 1, 1, 1)
            .and_then(|date| date.pred_opt())
            .and_then(|date| date.and_hms_milli_opt(23, 59, 59, 1_000))
            .unwrap_or_default();
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the start of the month.
    pub fn start_of_month(year: i32, month: u32) -> Self {
        let date = NaiveDate::from_ymd_opt(year, month, 1).unwrap_or_default();
        let dt = NaiveDateTime::new(date, NaiveTime::default());
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the end of the month.
    pub fn end_of_month(year: i32, month: u32) -> Self {
        let dt = NaiveDate::from_ymd_opt(year, month + 1, 1)
            .and_then(|date| date.pred_opt())
            .and_then(|date| date.and_hms_milli_opt(23, 59, 59, 1_000))
            .unwrap_or_default();
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the start of the day.
    pub fn start_of_day(year: i32, month: u32, day: u32) -> Self {
        let date = NaiveDate::from_ymd_opt(year, month, day).unwrap_or_default();
        let dt = NaiveDateTime::new(date, NaiveTime::default());
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Returns the end of the month.
    pub fn end_of_day(year: i32, month: u32, day: u32) -> Self {
        let date = NaiveDate::from_ymd_opt(year, month, day).unwrap_or_default();
        let dt = date
            .and_hms_milli_opt(23, 59, 59, 1_000)
            .unwrap_or_default();
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }

    /// Adds a duration in months to the date part of the `DateTime`.
    /// Returns `None` if the resulting date would be out of range.
    #[inline]
    pub fn checked_add_months(self, months: u32) -> Option<Self> {
        self.0.checked_add_months(Months::new(months)).map(Self)
    }

    /// Subtracts a duration in months from the date part of the `DateTime`.
    /// Returns `None` if the resulting date would be out of range.
    #[inline]
    pub fn checked_sub_months(self, months: u32) -> Option<Self> {
        self.0.checked_sub_months(Months::new(months)).map(Self)
    }

    /// Adds a duration in days to the date part of the `DateTime`.
    /// Returns `None` if the resulting date would be out of range.
    #[inline]
    pub fn checked_add_days(self, days: u32) -> Option<Self> {
        self.0
            .checked_add_days(Days::new(u64::from(days)))
            .map(Self)
    }

    /// Subtracts a duration in days from the date part of the `DateTime`.
    /// Returns `None` if the resulting date would be out of range.
    #[inline]
    pub fn checked_sub_days(self, days: u32) -> Option<Self> {
        self.0
            .checked_sub_days(Days::new(u64::from(days)))
            .map(Self)
    }
}

impl Default for DateTime {
    /// Returns an instance which corresponds to **the current date and time**.
    #[inline]
    fn default() -> Self {
        Self::now()
    }
}

impl fmt::Display for DateTime {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0.format("%Y-%m-%d %H:%M:%S%.6f %z"))
    }
}

impl Serialize for DateTime {
    #[inline]
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.to_utc_timestamp())
    }
}

impl From<Date> for DateTime {
    fn from(d: Date) -> Self {
        let dt = NaiveDateTime::new(d.into(), NaiveTime::default());
        let offset = Local.offset_from_utc_datetime(&dt);
        Self(LocalDateTime::from_naive_utc_and_offset(
            dt - offset,
            offset,
        ))
    }
}

impl From<LocalDateTime> for DateTime {
    #[inline]
    fn from(dt: LocalDateTime) -> Self {
        Self(dt)
    }
}

impl From<DateTime> for LocalDateTime {
    #[inline]
    fn from(dt: DateTime) -> Self {
        dt.0
    }
}

impl From<DateTime> for AvroValue {
    #[inline]
    fn from(dt: DateTime) -> Self {
        AvroValue::String(dt.to_string())
    }
}

impl From<DateTime> for JsonValue {
    #[inline]
    fn from(dt: DateTime) -> Self {
        JsonValue::String(dt.to_string())
    }
}

impl FromStr for DateTime {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let length = s.len();
        if length == 10 {
            let date = s.parse::<NaiveDate>()?;
            let dt = NaiveDateTime::new(date, NaiveTime::default());
            let offset = Local.offset_from_utc_datetime(&dt);
            Ok(LocalDateTime::from_naive_utc_and_offset(dt, offset).into())
        } else if length == 19 {
            let dt = s.parse::<NaiveDateTime>()?;
            let offset = Local.offset_from_utc_datetime(&dt);
            Ok(LocalDateTime::from_naive_utc_and_offset(dt, offset).into())
        } else if s.contains('+') || s.contains(" -") {
            LocalDateTime::from_str(s).map(Self)
        } else if s.ends_with('Z') {
            let dt = s.parse::<chrono::DateTime<Utc>>()?;
            Ok(dt.with_timezone(&Local).into())
        } else {
            let dt = [s, "Z"].concat().parse::<chrono::DateTime<Utc>>()?;
            Ok(dt.with_timezone(&Local).into())
        }
    }
}

impl Add<Duration> for DateTime {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Duration) -> Self {
        let duration = chrono::Duration::from_std(rhs).expect("Duration value is out of range");
        let datetime = self
            .0
            .checked_add_signed(duration)
            .expect("`DateTime + Duration` overflowed");
        Self(datetime)
    }
}

impl AddAssign<Duration> for DateTime {
    #[inline]
    fn add_assign(&mut self, rhs: Duration) {
        *self = *self + rhs;
    }
}

impl Sub<Duration> for DateTime {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Duration) -> Self {
        let duration = chrono::Duration::from_std(rhs).expect("Duration value is out of range");
        let datetime = self
            .0
            .checked_sub_signed(duration)
            .expect("`DateTime - Duration` overflowed");
        Self(datetime)
    }
}

impl SubAssign<Duration> for DateTime {
    #[inline]
    fn sub_assign(&mut self, rhs: Duration) {
        *self = *self - rhs;
    }
}

#[cfg(test)]
mod tests {
    use super::{Date, DateTime};

    #[test]
    fn it_parses_datetime() {
        assert!("2023-12-31".parse::<DateTime>().is_ok());
        assert!("2023-12-31T18:00:00".parse::<DateTime>().is_ok());
        assert!("2023-07-13T02:16:33.449Z".parse::<DateTime>().is_ok());
        assert!("2023-06-10 05:17:23.713071 +0800"
            .parse::<DateTime>()
            .is_ok());

        let datetime = "2023-11-30 16:24:30.654321 +0800"
            .parse::<DateTime>()
            .unwrap();
        let start_day = datetime.start_of_current_day();
        let end_day = datetime.end_of_current_day();
        assert_eq!("2023-11-30", start_day.format_date());
        assert_eq!("00:00:00", start_day.format_time());
        assert_eq!("2023-11-30", end_day.format_date());
        assert_eq!("23:59:60", end_day.format_time());

        let date = "2023-11-30".parse::<Date>().unwrap();
        let datetime = DateTime::from(date);
        assert!(datetime.day_of_week() == 4);
        assert_eq!("2023-11-30", datetime.format_date());
        assert_eq!("00:00:00", datetime.format_time());
    }
}