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
//! `datetime-rs` provides a representation of a date and time.

use std::cmp::Ordering;
use std::str::FromStr;
use std::time::SystemTime;

use format::FormattedDateTime;
use strptime::ParseError;
use strptime::ParseResult;
use strptime::Parser;
use strptime::RawDateTime;

#[macro_export]
macro_rules! datetime {
  ($y:literal-$m:literal-$d:literal $h:literal : $mi:literal : $s:literal) => {{
    #[allow(clippy::zero_prefixed_literal)]
    {
      $crate::DateTime::ymd($y, $m, $d).hms($h, $mi, $s).build()
    }
  }};
  ($y:literal-$m:literal-$d:literal $h:literal : $mi:literal : $s:literal $($tz:ident)::+) => {{
    #[cfg(feature = "tz")]
    #[allow(clippy::zero_prefixed_literal)]
    {
      match $crate::DateTime::ymd($y, $m, $d).hms($h, $mi, $s).tz($crate::tz::$($tz)::+) {
        Ok(dt) => dt.build(),
        Err(_) => panic!("invalid date/time and time zone combination"),
      }
    }
    #[cfg(not(feature = "tz"))]
    {
      compile_error!("The `tz` feature must be enabled to specify a time zone.");
    }
  }};
}

#[cfg(feature = "diesel-pg")]
mod db;
mod format;
pub mod interval;
#[cfg(feature = "serde")]
mod serde;

pub use date::date;
pub use date::Date;
pub use date::Weekday;

/// Time zone compnents (re-exported from `date-rs` crate).
#[cfg(feature = "tz")]
pub mod tz {
  pub use date::tz::*;

  #[derive(Clone, Copy, Debug, Eq, PartialEq)]
  pub(crate) enum TimeZone {
    Unspecified,
    Tz(::tz::TimeZoneRef<'static>),
    FixedOffset(i32),
  }

  impl TimeZone {
    pub(crate) const fn ut_offset(&self, timestamp: i64) -> TzResult<i32> {
      match self {
        Self::Unspecified => Ok(0),
        Self::FixedOffset(offset) => Ok(*offset),
        Self::Tz(tz) => match tz.find_local_time_type(timestamp) {
          Ok(t) => Ok(t.ut_offset()),
          Err(e) => Err(e),
        },
      }
    }
  }
}

/// A representation of a date and time.
#[derive(Clone, Copy, Debug, Eq)]
pub struct DateTime {
  seconds: i64,
  nanos: u32,
  #[cfg(feature = "tz")]
  tz: tz::TimeZone,
}

impl DateTime {
  /// Create a new date and time object.
  pub const fn ymd(year: i16, month: u8, day: u8) -> DateTimeBuilder {
    DateTimeBuilder {
      date: Date::new(year, month, day),
      seconds: 0,
      nanos: 0,
      #[cfg(feature = "tz")]
      tz: tz::TimeZone::Unspecified,
      offset: 0,
    }
  }

  /// Create a new date and time object from the given Unix timestamp.
  pub const fn from_timestamp(timestamp: i64, nanos: u32) -> Self {
    let mut timestamp = timestamp;
    let mut nanos = nanos;
    while nanos >= 1_000_000_000 {
      nanos -= 1_000_000_000;
      timestamp += 1;
    }
    Self {
      seconds: timestamp,
      nanos,
      #[cfg(feature = "tz")]
      tz: tz::TimeZone::Unspecified,
    }
  }

  /// Return the current timestamp.
  ///
  /// ## Panic
  ///
  /// Panics if the system clock is set prior to January 1, 1970.
  pub fn now() -> Self {
    let dur = SystemTime::now()
      .duration_since(SystemTime::UNIX_EPOCH)
      .expect("System clock set prior to January 1, 1970");
    Self::from_timestamp(dur.as_secs() as i64, dur.subsec_nanos())
  }
}

#[cfg(feature = "tz")]
impl DateTime {
  /// Set the time zone to the provided time zone, without adjusting the underlying absolute
  /// timestamp.
  #[inline]
  pub const fn with_tz(mut self, tz: tz::TimeZoneRef<'static>) -> Self {
    self.tz = tz::TimeZone::Tz(tz);
    self
  }
}

/// Accessors
impl DateTime {
  /// The year for this date.
  #[inline]
  pub const fn year(&self) -> i16 {
    Date::from_timestamp(self.tz_adjusted_seconds()).year()
  }

  /// The month for this date.
  #[inline]
  pub const fn month(&self) -> u8 {
    Date::from_timestamp(self.tz_adjusted_seconds()).month()
  }

  /// The day of the month for this date.
  #[inline]
  pub const fn day(&self) -> u8 {
    Date::from_timestamp(self.tz_adjusted_seconds()).day()
  }

  /// The day of the week for this date.
  #[inline]
  pub const fn weekday(&self) -> Weekday {
    Date::from_timestamp(self.tz_adjusted_seconds()).weekday()
  }

  /// The hour of the day for this date and time. Range: `[0, 24)`
  #[inline]
  pub const fn hour(&self) -> u8 {
    (self.tz_adjusted_seconds() % 86_400 / 3_600) as u8
  }

  /// The minute of the hour for this date and time. Range: `[0, 60)`
  #[inline]
  pub const fn minute(&self) -> u8 {
    ((self.tz_adjusted_seconds() % 3600) / 60) as u8
  }

  /// The second of the minute for this date and time. Range: `[0, 60)`
  #[inline]
  pub const fn second(&self) -> u8 {
    (self.tz_adjusted_seconds() % 60) as u8
  }

  /// The nanosecond of the second for this date and time. Range: `[0, 1_000_000_000)`
  #[inline]
  pub const fn nanosecond(&self) -> u32 {
    self.nanos
  }

  /// The ordinal day of the year.
  #[inline]
  pub const fn day_of_year(&self) -> u16 {
    self.date().day_of_year()
  }

  /// The date corresponding to this datetime.
  #[inline]
  pub const fn date(&self) -> Date {
    Date::from_timestamp(self.tz_adjusted_seconds())
  }

  /// The number of seconds since the Unix epoch for this date and time.
  #[inline]
  pub const fn as_seconds(&self) -> i64 {
    self.seconds
  }

  /// The number of milliseconds since the Unix epoch for this date and time.
  #[inline]
  pub const fn as_milliseconds(&self) -> i64 {
    self.seconds * 1_000 + (self.nanos / 1_000_000) as i64
  }

  /// The number of microseconds since the Unix epoch for this date and time.
  #[inline]
  pub const fn as_microseconds(&self) -> i64 {
    self.seconds * 1_000_000 + (self.nanos / 1_000) as i64
  }

  /// The number of nanoseconds since the Unix epoch for this date and time.
  #[inline]
  pub const fn as_nanoseconds(&self) -> i128 {
    self.seconds as i128 * 1_000_000_000 + self.nanos as i128
  }

  /// Provide the number of seconds since the epoch in the time zone with the same offset as this
  /// datetime's time zone.
  #[inline(always)]
  const fn tz_adjusted_seconds(&self) -> i64 {
    self.seconds + self.tz_offset()
  }

  /// Provide the offset, in seconds
  const fn tz_offset(&self) -> i64 {
    #[cfg(feature = "tz")]
    {
      match self.tz.ut_offset(self.seconds) {
        Ok(offset) => offset as i64,
        Err(_) => panic!("Invalid time zone"),
      }
    }
    #[cfg(not(feature = "tz"))]
    0
  }
}

impl DateTime {
  /// Format the given date and time according to the provided `strftime`-like string.
  pub fn format(&self, format: &'static str) -> FormattedDateTime {
    FormattedDateTime { dt: self, format }
  }
}

impl DateTime {
  /// Parse a date from a string, according to the provided format string.
  pub fn parse(datetime_str: impl AsRef<str>, fmt: &'static str) -> ParseResult<Self> {
    let parser = Parser::new(fmt);
    parser.parse(datetime_str)?.try_into()
  }
}

impl PartialEq for DateTime {
  fn eq(&self, other: &Self) -> bool {
    self.seconds == other.seconds && self.nanos == other.nanos
  }
}

impl PartialOrd for DateTime {
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}

impl Ord for DateTime {
  fn cmp(&self, other: &Self) -> Ordering {
    let seconds_cmp = self.seconds.cmp(&other.seconds);
    match seconds_cmp {
      Ordering::Equal => self.nanos.cmp(&other.nanos),
      _ => seconds_cmp,
    }
  }
}

impl FromStr for DateTime {
  type Err = ParseError;

  #[rustfmt::skip]
  fn from_str(s: &str) -> ParseResult<Self> {
    // Attempt several common formats.
    if let Ok(dt) = Parser::new("%Y-%m-%dT%H:%M:%S").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%dT%H:%M:%S%z").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%d %H:%M:%S").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%d %H:%M:%S%z").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%dT%H:%M:%S%.6f").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%dT%H:%M:%S%.6f%z").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%d %H:%M:%S%.6f").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%d %H:%M:%S%.6f%z").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%dT%H:%M:%S%.9f").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%dT%H:%M:%S%.9f%z").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%d %H:%M:%S%.9f").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%d %H:%M:%S%.9f%z").parse(s) { return dt.try_into(); }
    if let Ok(dt) = Parser::new("%Y-%m-%d %H:%M:%SZ").parse(s) { return dt.try_into(); }
    Parser::new("%Y-%m-%dT%H:%M:%SZ").parse(s)?.try_into()
  }
}

impl TryFrom<RawDateTime> for DateTime {
  type Error = ParseError;

  fn try_from(value: RawDateTime) -> ParseResult<Self> {
    let date = value.date()?;
    let time = value.time().unwrap_or_default();
    Ok(match time.utc_offset() {
      #[cfg(feature = "tz")]
      Some(utc_offset) => Self::ymd(date.year(), date.month(), date.day())
        .hms(time.hour(), time.minute(), time.second())
        .nanos(time.nanosecond() as u32)
        .utc_offset(utc_offset)
        .build(),
      #[cfg(not(feature = "tz"))]
      Some(_) => panic!("Enable the `tz` feature to parse datetimes with UTC offsets."),
      None => Self::ymd(date.year(), date.month(), date.day())
        .hms(time.hour(), time.minute(), time.second())
        .nanos(time.nanosecond() as u32)
        .build(),
    })
  }
}

/// An intermediate builder for [`DateTime`].
#[must_use]
pub struct DateTimeBuilder {
  date: Date,
  seconds: i64,
  nanos: u32,
  #[cfg(feature = "tz")]
  tz: tz::TimeZone,
  offset: i64,
}

impl DateTimeBuilder {
  /// Attach an hour, minute, and second to the datetime.
  pub const fn hms(mut self, hour: u8, minute: u8, second: u8) -> Self {
    assert!(hour < 24, "Hour out of bounds");
    assert!(minute < 60, "Minute out of bounds");
    assert!(second < 60, "Second out of bounds");
    self.seconds = (hour as i64 * 3600) + (minute as i64 * 60) + second as i64;
    self
  }

  /// Attach fractional to the datetime.
  pub const fn nanos(mut self, nanos: u32) -> Self {
    assert!(nanos < 1_000_000_000, "Nanos out of bounds.");
    self.nanos = nanos;
    self
  }

  /// Attach a timezone to the datetime.
  ///
  /// This method assumes that the timezone _modifies_ the underlying timestamp; in other words,
  /// the YMD/HMS specified to the date and time builder should be preserved, and the time zone's
  /// offset applied to the underlying timestamp to preserve the date and time on the wall clock.
  #[cfg(feature = "tz")]
  pub const fn tz(mut self, tz: tz::TimeZoneRef<'static>) -> tz::TzResult<Self> {
    self.offset = match tz.find_local_time_type(self.date.timestamp() + self.seconds) {
      Ok(t) => t.ut_offset() as i64,
      Err(e) => return Err(e),
    };
    self.tz = tz::TimeZone::Tz(tz);
    Ok(self)
  }

  /// Attach a UTC offset to the datetime.
  ///
  /// This method assumes that the offset _modifies_ the underlying timestamp; in other words, the
  /// YMD/HMS specified to the date and time builder should be preserved, and the offset applied to
  /// the underlying timestamp to preserve the date and time on the wall clock.
  #[cfg(feature = "tz")]
  pub(crate) const fn utc_offset(mut self, offset: i32) -> Self {
    self.offset = offset as i64;
    self.tz = tz::TimeZone::FixedOffset(offset);
    self
  }

  /// Build the final [`DateTime`] object.
  pub const fn build(self) -> DateTime {
    DateTime {
      seconds: self.date.timestamp() + self.seconds - self.offset,
      nanos: self.nanos,
      #[cfg(feature = "tz")]
      tz: self.tz,
    }
  }
}

trait Sealed {}
impl Sealed for date::Date {}

/// Convert from a date into a datetime, by way of a builder.
#[allow(private_bounds)]
pub trait FromDate: Sealed {
  /// Create a `DateTimeBuilder` for this Date.
  fn hms(self, hour: u8, minute: u8, second: u8) -> DateTimeBuilder;
}

impl FromDate for date::Date {
  fn hms(self, hour: u8, minute: u8, second: u8) -> DateTimeBuilder {
    DateTimeBuilder {
      date: self,
      seconds: 0,
      nanos: 0,
      #[cfg(feature = "tz")]
      tz: tz::TimeZone::Unspecified,
      offset: 0,
    }
    .hms(hour, minute, second)
  }
}

#[cfg(test)]
mod tests {
  use assert2::check;
  use strptime::ParseResult;

  #[cfg(feature = "tz")]
  use crate::tz;
  use crate::DateTime;
  use crate::FromDate;

  #[test]
  fn test_zero() {
    let dt = datetime! { 1970-01-01 00:00:00 };
    check!(dt.seconds == 0);
  }

  #[test]
  fn test_accessors() {
    let dt = datetime! { 2012-04-21 11:00:00 };
    check!(dt.year() == 2012);
    check!(dt.month() == 4);
    check!(dt.day() == 21);
    check!(dt.hour() == 11);
    check!(dt.minute() == 0);
    check!(dt.second() == 0);
  }

  #[test]
  fn test_more_accessors() {
    let dt = datetime! { 2024-02-29 13:15:45 };
    check!(dt.year() == 2024);
    check!(dt.month() == 2);
    check!(dt.day() == 29);
    check!(dt.hour() == 13);
    check!(dt.minute() == 15);
    check!(dt.second() == 45);
  }

  #[test]
  fn test_parse_str() -> ParseResult<()> {
    for s in [
      "2012-04-21 11:00:00",
      "2012-04-21T11:00:00",
      "2012-04-21 11:00:00.000000",
      "2012-04-21 11:00:00Z",
      "2012-04-21T11:00:00.000000",
      "2012-04-21T11:00:00Z",
    ] {
      let dt = s.parse::<DateTime>()?;
      check!(dt.year() == 2012);
      check!(dt.month() == 4);
      check!(dt.day() == 21);
      check!(dt.hour() == 11);
    }

    Ok(())
  }

  #[test]
  #[cfg(feature = "tz")]
  fn test_parse_str_tz() -> ParseResult<()> {
    for s in
      ["2012-04-21 11:00:00-0400", "2012-04-21T11:00:00-0400", "2012-04-21 11:00:00.000000-0400"]
    {
      let dt = s.parse::<DateTime>()?;
      check!(dt.year() == 2012);
      check!(dt.month() == 4);
      check!(dt.day() == 21);
      check!(dt.hour() == 11);
    }
    Ok(())
  }

  #[test]
  #[allow(clippy::inconsistent_digit_grouping)]
  fn test_precision() {
    let dt = DateTime::ymd(2012, 4, 21).hms(15, 0, 0).build();
    check!(dt.as_seconds() == 1335020400);
    check!(dt.as_milliseconds() == 1335020400_000);
    check!(dt.as_microseconds() == 1335020400_000_000);
    check!(dt.as_nanoseconds() == 1335020400_000_000_000);
  }

  #[cfg(feature = "tz")]
  #[test]
  fn test_tz() -> tz::TzResult<()> {
    let dt = DateTime::ymd(2012, 4, 21).hms(11, 0, 0).tz(tz::us::EASTERN)?.build();
    check!(dt.as_seconds() == 1335020400);
    check!(dt.year() == 2012);
    check!(dt.month() == 4);
    check!(dt.day() == 21);
    check!(dt.hour() == 11);
    let dt = DateTime::ymd(1970, 1, 1).tz(tz::us::PACIFIC)?.build();
    check!(dt.as_seconds() == 3600 * 8);
    Ok(())
  }

  #[cfg(feature = "tz")]
  #[test]
  fn test_unix_tz() {
    let dt = DateTime::from_timestamp(1335020400, 0).with_tz(tz::us::EASTERN);
    check!(dt.as_seconds() == 1335020400);
    check!(dt.year() == 2012);
    check!(dt.month() == 4);
    check!(dt.day() == 21);
    check!(dt.hour() == 11);
  }

  #[test]
  fn test_from_date_trait() {
    let dt = date::date! { 2012-04-21 }.hms(11, 0, 0).build();
    check!(dt.year() == 2012);
    check!(dt.month() == 4);
    check!(dt.day() == 21);
    check!(dt.hour() == 11);
  }
}