Add MIN and MAX DateTime, Time and NaiveDateTime (#452)

Resolves #386
This commit is contained in:
Rob Young 2020-07-10 22:32:19 +01:00 committed by GitHub
parent a905c556d8
commit aaee912228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 3 deletions

View File

@ -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

View File

@ -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};
@ -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.

View File

@ -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};
@ -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;

View File

@ -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};
@ -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

View File

@ -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.
///