Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/datetime/rustc_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct TsSeconds<Tz: TimeZone>(DateTime<Tz>);

#[allow(deprecated)]
impl<Tz: TimeZone> From<TsSeconds<Tz>> for DateTime<Tz> {
/// Pull the inner DateTime<Tz> out
/// Pull the inner `DateTime<Tz>` out
#[allow(deprecated)]
fn from(obj: TsSeconds<Tz>) -> DateTime<Tz> {
obj.0
Expand Down
2 changes: 1 addition & 1 deletion src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Month {
}

impl num_traits::FromPrimitive for Month {
/// Returns an Option<Month> from a i64, assuming a 1-index, January = 1.
/// Returns an `Option<Month>` from a i64, assuming a 1-index, January = 1.
///
/// `Month::from_i64(n: i64)`: | `1` | `2` | ... | `12`
/// ---------------------------| -------------------- | --------------------- | ... | -----
Expand Down
68 changes: 29 additions & 39 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Days {
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
pub struct NaiveDate {
ymdf: DateImpl, // (year << 13) | of
ymdf: DateImpl,
}

/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
Expand Down Expand Up @@ -223,18 +223,13 @@ fn test_date_bounds() {

impl NaiveDate {
/// Makes a new `NaiveDate` from year and packed ordinal-flags, with a verification.
fn from_of(year: i32, of: Of) -> Option<NaiveDate> {
if (MIN_YEAR..=MAX_YEAR).contains(&year) && of.valid() {
let Of(of) = of;
Some(NaiveDate { ymdf: (year << 13) | (of as DateImpl) })
} else {
None
}
fn from_parts(year: i32, of: Of) -> Option<NaiveDate> {
Some(NaiveDate { ymdf: DateImpl::from_parts(year, of)? })
}

/// Makes a new `NaiveDate` from year and packed month-day-flags, with a verification.
fn from_mdf(year: i32, mdf: Mdf) -> Option<NaiveDate> {
NaiveDate::from_of(year, mdf.to_of())
NaiveDate::from_parts(year, mdf.to_of()?)
}

/// Makes a new `NaiveDate` from the [calendar date](#calendar-date)
Expand Down Expand Up @@ -267,7 +262,7 @@ impl NaiveDate {
/// ```
pub fn from_ymd_opt(year: i32, month: u32, day: u32) -> Option<NaiveDate> {
let flags = YearFlags::from_year(year);
NaiveDate::from_mdf(year, Mdf::new(month, day, flags))
NaiveDate::from_mdf(year, Mdf::new(month, day, flags)?)
}

/// Makes a new `NaiveDate` from the [ordinal date](#ordinal-date)
Expand Down Expand Up @@ -301,7 +296,7 @@ impl NaiveDate {
/// ```
pub fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate> {
let flags = YearFlags::from_year(year);
NaiveDate::from_of(year, Of::new(ordinal, flags))
NaiveDate::from_parts(year, Of::new(ordinal, flags)?)
}

/// Makes a new `NaiveDate` from the [ISO week date](#week-date)
Expand Down Expand Up @@ -368,20 +363,20 @@ impl NaiveDate {
if weekord <= delta {
// ordinal < 1, previous year
let prevflags = YearFlags::from_year(year - 1);
NaiveDate::from_of(
NaiveDate::from_parts(
year - 1,
Of::new(weekord + prevflags.ndays() - delta, prevflags),
Of::new(weekord + prevflags.ndays() - delta, prevflags)?,
)
} else {
let ordinal = weekord - delta;
let ndays = flags.ndays();
if ordinal <= ndays {
// this year
NaiveDate::from_of(year, Of::new(ordinal, flags))
NaiveDate::from_parts(year, Of::new(ordinal, flags)?)
} else {
// ordinal > ndays, next year
let nextflags = YearFlags::from_year(year + 1);
NaiveDate::from_of(year + 1, Of::new(ordinal - ndays, nextflags))
NaiveDate::from_parts(year + 1, Of::new(ordinal - ndays, nextflags)?)
}
}
} else {
Expand Down Expand Up @@ -424,7 +419,7 @@ impl NaiveDate {
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags))
NaiveDate::from_parts(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
}

/// Makes a new `NaiveDate` by counting the number of occurrences of a particular day-of-week
Expand Down Expand Up @@ -615,7 +610,7 @@ impl NaiveDate {
let days = [31, feb_days, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let day = Ord::min(self.day(), days[(month - 1) as usize]);

NaiveDate::from_mdf(year, Mdf::new(month as u32, day, flags))
NaiveDate::from_mdf(year, Mdf::new(month as u32, day, flags)?)
}

/// Add a duration in [`Days`] to the date
Expand Down Expand Up @@ -870,28 +865,23 @@ impl NaiveDate {
/// Returns the packed ordinal-flags.
#[inline]
fn of(&self) -> Of {
Of((self.ymdf & 0b1_1111_1111_1111) as u32)
self.ymdf.of()
}

/// Makes a new `NaiveDate` with the packed month-day-flags changed.
///
/// Returns `None` when the resulting `NaiveDate` would be invalid.
#[inline]
fn with_mdf(&self, mdf: Mdf) -> Option<NaiveDate> {
self.with_of(mdf.to_of())
self.with_of(mdf.to_of()?)
}

/// Makes a new `NaiveDate` with the packed ordinal-flags changed.
///
/// Returns `None` when the resulting `NaiveDate` would be invalid.
#[inline]
fn with_of(&self, of: Of) -> Option<NaiveDate> {
if of.valid() {
let Of(of) = of;
Some(NaiveDate { ymdf: (self.ymdf & !0b1_1111_1111_1111) | of as DateImpl })
} else {
None
}
Some(NaiveDate { ymdf: DateImpl::from_parts(self.year(), of)? })
}

/// Makes a new `NaiveDate` for the next calendar date.
Expand All @@ -918,7 +908,7 @@ impl NaiveDate {
/// ```
#[inline]
pub fn succ_opt(&self) -> Option<NaiveDate> {
self.with_of(self.of().succ()).or_else(|| NaiveDate::from_ymd_opt(self.year() + 1, 1, 1))
Some(NaiveDate { ymdf: self.ymdf.succ_opt()? })
}

/// Makes a new `NaiveDate` for the previous calendar date.
Expand All @@ -945,7 +935,7 @@ impl NaiveDate {
/// ```
#[inline]
pub fn pred_opt(&self) -> Option<NaiveDate> {
self.with_of(self.of().pred()).or_else(|| NaiveDate::from_ymd_opt(self.year() - 1, 12, 31))
Some(NaiveDate { ymdf: self.ymdf.pred_opt()? })
}

/// Adds the `days` part of given `Duration` to the current date.
Expand Down Expand Up @@ -976,7 +966,7 @@ impl NaiveDate {

let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags))
NaiveDate::from_parts(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
}

/// Subtracts the `days` part of given `Duration` from the current date.
Expand Down Expand Up @@ -1007,7 +997,7 @@ impl NaiveDate {

let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
NaiveDate::from_of(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags))
NaiveDate::from_parts(year_div_400 * 400 + year_mod_400 as i32, Of::new(ordinal, flags)?)
}

/// Subtracts another `NaiveDate` from the current date.
Expand Down Expand Up @@ -1191,9 +1181,9 @@ impl NaiveDate {
}

/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ };
pub const MIN: NaiveDate = NaiveDate { ymdf: DateImpl::MIN };
/// The maximum possible `NaiveDate` (December 31, 262143 CE).
pub const MAX: NaiveDate = NaiveDate { ymdf: (MAX_YEAR << 13) | (365 << 4) | 0o17 /*F*/ };
pub const MAX: NaiveDate = NaiveDate { ymdf: DateImpl::MAX };
}

impl Datelike for NaiveDate {
Expand All @@ -1209,7 +1199,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn year(&self) -> i32 {
self.ymdf >> 13
self.ymdf.year()
}

/// Returns the month number starting from 1.
Expand Down Expand Up @@ -1376,7 +1366,7 @@ impl Datelike for NaiveDate {

#[inline]
fn iso_week(&self) -> IsoWeek {
isoweek::iso_week_from_yof(self.year(), self.of())
isoweek::iso_week_from_yof(self.year(), self.of()).unwrap()
}

/// Makes a new `NaiveDate` with the year number changed.
Expand Down Expand Up @@ -1429,7 +1419,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn with_month(&self, month: u32) -> Option<NaiveDate> {
self.with_mdf(self.mdf().with_month(month))
self.with_mdf(self.mdf().with_month(month)?)
}

/// Makes a new `NaiveDate` with the month number (starting from 0) changed.
Expand All @@ -1448,7 +1438,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn with_month0(&self, month0: u32) -> Option<NaiveDate> {
self.with_mdf(self.mdf().with_month(month0 + 1))
self.with_mdf(self.mdf().with_month(month0 + 1)?)
}

/// Makes a new `NaiveDate` with the day of month (starting from 1) changed.
Expand All @@ -1467,7 +1457,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn with_day(&self, day: u32) -> Option<NaiveDate> {
self.with_mdf(self.mdf().with_day(day))
self.with_mdf(self.mdf().with_day(day)?)
}

/// Makes a new `NaiveDate` with the day of month (starting from 0) changed.
Expand All @@ -1486,7 +1476,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn with_day0(&self, day0: u32) -> Option<NaiveDate> {
self.with_mdf(self.mdf().with_day(day0 + 1))
self.with_mdf(self.mdf().with_day(day0 + 1)?)
}

/// Makes a new `NaiveDate` with the day of year (starting from 1) changed.
Expand All @@ -1510,7 +1500,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn with_ordinal(&self, ordinal: u32) -> Option<NaiveDate> {
self.with_of(self.of().with_ordinal(ordinal))
self.with_of(self.of().with_ordinal(ordinal)?)
}

/// Makes a new `NaiveDate` with the day of year (starting from 0) changed.
Expand All @@ -1534,7 +1524,7 @@ impl Datelike for NaiveDate {
/// ```
#[inline]
fn with_ordinal0(&self, ordinal0: u32) -> Option<NaiveDate> {
self.with_of(self.of().with_ordinal(ordinal0 + 1))
self.with_of(self.of().with_ordinal(ordinal0 + 1)?)
}
}

Expand Down
Loading