Skip to content

Commit

Permalink
Add MIN and MAX DateTime, Time and NaiveDateTime (chronotope#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
robyoung authored Jul 10, 2020
1 parent a905c55 commit aaee912
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Versions with only mechanical changes will be omitted from the following list.

## 0.4.14 (unreleased)

## Improvements

* Added MIN and MAX values for `NaiveTime`, `NaiveDateTime` and `DateTime<Utc>`.


## 0.4.13

Expand Down
7 changes: 6 additions & 1 deletion src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::borrow::Borrow;
use format::DelayedFormat;
use format::{parse, ParseError, ParseResult, Parsed, StrftimeItems};
use format::{Fixed, Item};
use naive::{IsoWeek, NaiveDateTime, NaiveTime};
use naive::{self, IsoWeek, NaiveDateTime, NaiveTime};
#[cfg(feature = "clock")]
use offset::Local;
use offset::{FixedOffset, Offset, TimeZone, Utc};
Expand Down Expand Up @@ -70,6 +70,11 @@ pub struct DateTime<Tz: TimeZone> {
offset: Tz::Offset,
}

/// The minimum possilbe `DateTime<Utc>`.
pub const MIN_DATETIME: DateTime<Utc> = DateTime { datetime: naive::MIN_DATETIME, offset: Utc };
/// The maximum possible `DateTime<Utc>`.
pub const MAX_DATETIME: DateTime<Utc> = DateTime { datetime: naive::MAX_DATETIME, offset: Utc };

impl<Tz: TimeZone> DateTime<Tz> {
/// Makes a new `DateTime` with given *UTC* datetime and offset.
/// The local datetime should be constructed via the `TimeZone` trait.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub use oldtime::Duration;
pub use date::{Date, MAX_DATE, MIN_DATE};
#[cfg(feature = "rustc-serialize")]
pub use datetime::rustc_serialize::TsSeconds;
pub use datetime::{DateTime, SecondsFormat};
pub use datetime::{DateTime, SecondsFormat, MAX_DATETIME, MIN_DATETIME};
pub use format::{ParseError, ParseResult};
#[doc(no_inline)]
pub use naive::{IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
Expand Down Expand Up @@ -511,7 +511,7 @@ pub mod naive {
#[cfg(feature = "rustc-serialize")]
#[allow(deprecated)]
pub use self::datetime::rustc_serialize::TsSeconds;
pub use self::datetime::NaiveDateTime;
pub use self::datetime::{NaiveDateTime, MAX_DATETIME, MIN_DATETIME};
pub use self::isoweek::IsoWeek;
pub use self::time::NaiveTime;

Expand Down
7 changes: 7 additions & 0 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use div::div_mod_floor;
use format::DelayedFormat;
use format::{parse, ParseError, ParseResult, Parsed, StrftimeItems};
use format::{Fixed, Item, Numeric, Pad};
use naive::date::{MAX_DATE, MIN_DATE};
use naive::time::{MAX_TIME, MIN_TIME};
use naive::{IsoWeek, NaiveDate, NaiveTime};
use {Datelike, Timelike, Weekday};

Expand All @@ -26,6 +28,11 @@ use {Datelike, Timelike, Weekday};
/// touching that call when we are already sure that it WILL overflow...
const MAX_SECS_BITS: usize = 44;

/// The minimum possible `NaiveDateTime`.
pub const MIN_DATETIME: NaiveDateTime = NaiveDateTime { date: MIN_DATE, time: MIN_TIME };
/// The maximum possible `NaiveDateTime`.
pub const MAX_DATETIME: NaiveDateTime = NaiveDateTime { date: MAX_DATE, time: MAX_TIME };

/// ISO 8601 combined date and time without timezone.
///
/// # Example
Expand Down
3 changes: 3 additions & 0 deletions src/naive/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use format::{parse, ParseError, ParseResult, Parsed, StrftimeItems};
use format::{Fixed, Item, Numeric, Pad};
use Timelike;

pub const MIN_TIME: NaiveTime = NaiveTime { secs: 0, frac: 0 };
pub const MAX_TIME: NaiveTime = NaiveTime { secs: 23 * 3600 + 59 * 60 + 59, frac: 999_999_999 };

/// ISO 8601 time without timezone.
/// Allows for the nanosecond precision and optional leap second representation.
///
Expand Down

0 comments on commit aaee912

Please sign in to comment.