Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer associated consts for MIN and MAX values #726

Merged
merged 2 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -184,7 +184,7 @@ jobs:
- uses: actions/checkout@v2

- name: Install cross
run: bash ci/install-cross.sh
run: cargo install cross

- uses: Swatinem/rust-cache@v1

Expand Down
43 changes: 0 additions & 43 deletions ci/install-cross.sh

This file was deleted.

15 changes: 12 additions & 3 deletions src/date.rs
Expand Up @@ -16,7 +16,7 @@ use rkyv::{Archive, Deserialize, Serialize};
use crate::format::Locale;
#[cfg(any(feature = "alloc", feature = "std", test))]
use crate::format::{DelayedFormat, Item, StrftimeItems};
use crate::naive::{self, IsoWeek, NaiveDate, NaiveTime};
use crate::naive::{IsoWeek, NaiveDate, NaiveTime};
use crate::offset::{TimeZone, Utc};
use crate::oldtime::Duration as OldDuration;
use crate::DateTime;
Expand Down Expand Up @@ -61,9 +61,13 @@ pub struct Date<Tz: TimeZone> {
}

/// The minimum possible `Date`.
pub const MIN_DATE: Date<Utc> = Date { date: naive::MIN_DATE, offset: Utc };
#[allow(deprecated)]
#[deprecated(since = "0.4.20", note = "Use Date::MAX_UTC instead")]
pub const MIN_DATE: Date<Utc> = Date::<Utc>::MIN_UTC;
/// The maximum possible `Date`.
pub const MAX_DATE: Date<Utc> = Date { date: naive::MAX_DATE, offset: Utc };
#[allow(deprecated)]
#[deprecated(since = "0.4.20", note = "Use Date::MAX_UTC instead")]
pub const MAX_DATE: Date<Utc> = Date::<Utc>::MAX_UTC;

impl<Tz: TimeZone> Date<Tz> {
/// Makes a new `Date` with given *UTC* date and offset.
Expand Down Expand Up @@ -288,6 +292,11 @@ impl<Tz: TimeZone> Date<Tz> {
false => None,
}
}

/// The minimum possible `Date`.
pub const MIN_UTC: Date<Utc> = Date { date: NaiveDate::MIN, offset: Utc };
/// The maximum possible `Date`.
pub const MAX_UTC: Date<Utc> = Date { date: NaiveDate::MAX, offset: Utc };
}

/// Maps the local date to other date with given conversion function.
Expand Down
13 changes: 10 additions & 3 deletions src/datetime/mod.rs
Expand Up @@ -24,7 +24,7 @@ use crate::format::DelayedFormat;
use crate::format::Locale;
use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems};
use crate::format::{Fixed, Item};
use crate::naive::{self, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
use crate::naive::{IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
Expand Down Expand Up @@ -89,9 +89,11 @@ pub struct DateTime<Tz: TimeZone> {
}

/// The minimum possible `DateTime<Utc>`.
pub const MIN_DATETIME: DateTime<Utc> = DateTime { datetime: naive::MIN_DATETIME, offset: Utc };
#[deprecated(since = "0.4.20", note = "Use DateTime::MIN_UTC instead")]
pub const MIN_DATETIME: DateTime<Utc> = DateTime::<Utc>::MIN_UTC;
/// The maximum possible `DateTime<Utc>`.
pub const MAX_DATETIME: DateTime<Utc> = DateTime { datetime: naive::MAX_DATETIME, offset: Utc };
#[deprecated(since = "0.4.20", note = "Use DateTime::MAX_UTC instead")]
pub const MAX_DATETIME: DateTime<Utc> = DateTime::<Utc>::MAX_UTC;

impl<Tz: TimeZone> DateTime<Tz> {
/// Makes a new `DateTime` with given *UTC* datetime and offset.
Expand Down Expand Up @@ -372,6 +374,11 @@ impl<Tz: TimeZone> DateTime<Tz> {
false => None,
}
}

/// The minimum possible `DateTime<Utc>`.
pub const MIN_UTC: DateTime<Utc> = DateTime { datetime: NaiveDateTime::MIN, offset: Utc };
/// The maximum possible `DateTime<Utc>`.
pub const MAX_UTC: DateTime<Utc> = DateTime { datetime: NaiveDateTime::MAX, offset: Utc };
}

impl Default for DateTime<Utc> {
Expand Down
12 changes: 6 additions & 6 deletions src/format/parsed.rs
Expand Up @@ -698,7 +698,7 @@ impl Parsed {
mod tests {
use super::super::{IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
use super::Parsed;
use crate::naive::{NaiveDate, NaiveTime, MAX_DATE, MIN_DATE};
use crate::naive::{NaiveDate, NaiveTime};
use crate::offset::{FixedOffset, TimeZone, Utc};
use crate::Datelike;
use crate::Weekday::*;
Expand Down Expand Up @@ -828,7 +828,7 @@ mod tests {
assert_eq!(parse!(year_div_100: 19, year_mod_100: -1, month: 1, day: 1), Err(OUT_OF_RANGE));
assert_eq!(parse!(year_div_100: 0, year_mod_100: 0, month: 1, day: 1), ymd(0, 1, 1));
assert_eq!(parse!(year_div_100: -1, year_mod_100: 42, month: 1, day: 1), Err(OUT_OF_RANGE));
let max_year = MAX_DATE.year();
let max_year = NaiveDate::MAX.year();
assert_eq!(
parse!(year_div_100: max_year / 100,
year_mod_100: max_year % 100, month: 1, day: 1),
Expand Down Expand Up @@ -1081,22 +1081,22 @@ mod tests {

// more timestamps
let max_days_from_year_1970 =
MAX_DATE.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1));
NaiveDate::MAX.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1));
let year_0_from_year_1970 =
NaiveDate::from_ymd(0, 1, 1).signed_duration_since(NaiveDate::from_ymd(1970, 1, 1));
let min_days_from_year_1970 =
MIN_DATE.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1));
NaiveDate::MIN.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1));
assert_eq!(
parse!(timestamp: min_days_from_year_1970.num_seconds()),
ymdhms(MIN_DATE.year(), 1, 1, 0, 0, 0)
ymdhms(NaiveDate::MIN.year(), 1, 1, 0, 0, 0)
);
assert_eq!(
parse!(timestamp: year_0_from_year_1970.num_seconds()),
ymdhms(0, 1, 1, 0, 0, 0)
);
assert_eq!(
parse!(timestamp: max_days_from_year_1970.num_seconds() + 86399),
ymdhms(MAX_DATE.year(), 12, 31, 23, 59, 59)
ymdhms(NaiveDate::MAX.year(), 12, 31, 23, 59, 59)
);

// leap seconds #1: partial fields
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -472,11 +472,13 @@ macro_rules! try_opt {
}

mod date;
#[allow(deprecated)]
pub use date::{Date, MAX_DATE, MIN_DATE};

mod datetime;
#[cfg(feature = "rustc-serialize")]
pub use datetime::rustc_serialize::TsSeconds;
#[allow(deprecated)]
pub use datetime::{DateTime, SecondsFormat, MAX_DATETIME, MIN_DATETIME};

pub mod format;
Expand Down