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

Small simplifications #1530

Merged
merged 4 commits into from
Mar 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
#[inline]
#[must_use]
pub fn date_naive(&self) -> NaiveDate {
let local = self.naive_local();
NaiveDate::from_ymd_opt(local.year(), local.month(), local.day()).unwrap()
self.naive_local().date()
}

/// Retrieves the time component.
Expand Down Expand Up @@ -653,13 +652,12 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// # Examples
///
/// ```rust
/// # use chrono::{FixedOffset, SecondsFormat, TimeZone, Utc, NaiveDate};
/// # use chrono::{FixedOffset, SecondsFormat, TimeZone, NaiveDate};
/// let dt = NaiveDate::from_ymd_opt(2018, 1, 26)
/// .unwrap()
/// .and_hms_micro_opt(18, 30, 9, 453_829)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap();
/// .and_utc();
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, false), "2018-01-26T18:30:09.453+00:00");
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Millis, true), "2018-01-26T18:30:09.453Z");
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), "2018-01-26T18:30:09Z");
Expand Down
36 changes: 12 additions & 24 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ impl<'de> de::Deserialize<'de> for DateTime<Local> {
/// .unwrap()
/// .and_hms_nano_opt(02, 04, 59, 918355733)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap();
/// .and_utc();
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -181,8 +180,7 @@ pub mod ts_nanoseconds {
/// .unwrap()
/// .and_hms_nano_opt(02, 04, 59, 918355733)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
Expand Down Expand Up @@ -278,8 +276,7 @@ pub mod ts_nanoseconds {
/// .unwrap()
/// .and_hms_nano_opt(02, 04, 59, 918355733)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// );
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -327,8 +324,7 @@ pub mod ts_nanoseconds_option {
/// .unwrap()
/// .and_hms_nano_opt(02, 04, 59, 918355733)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// ),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -429,8 +425,7 @@ pub mod ts_nanoseconds_option {
/// .unwrap()
/// .and_hms_micro_opt(02, 04, 59, 918355)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap();
/// .and_utc();
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -469,8 +464,7 @@ pub mod ts_microseconds {
/// .unwrap()
/// .and_hms_micro_opt(02, 04, 59, 918355)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
Expand Down Expand Up @@ -567,8 +561,7 @@ pub mod ts_microseconds {
/// .unwrap()
/// .and_hms_micro_opt(02, 04, 59, 918355)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// );
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -607,8 +600,7 @@ pub mod ts_microseconds_option {
/// .unwrap()
/// .and_hms_micro_opt(02, 04, 59, 918355)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// ),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -707,8 +699,7 @@ pub mod ts_microseconds_option {
/// .unwrap()
/// .and_hms_milli_opt(02, 04, 59, 918)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap();
/// .and_utc();
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -747,8 +738,7 @@ pub mod ts_milliseconds {
/// .unwrap()
/// .and_hms_milli_opt(02, 04, 59, 918)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
Expand Down Expand Up @@ -838,8 +828,7 @@ pub mod ts_milliseconds {
/// .unwrap()
/// .and_hms_milli_opt(02, 04, 59, 918)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// );
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -878,8 +867,7 @@ pub mod ts_milliseconds_option {
/// .unwrap()
/// .and_hms_milli_opt(02, 04, 59, 918)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap(),
/// .and_utc(),
/// ),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@
//! dt,
//! NaiveDate::from_ymd_opt(2014, 7, 8)?
//! .and_hms_opt(9, 10, 11)?
//! .and_local_timezone(Utc)
//! .unwrap()
//! .and_utc()
//! );
//!
//! // July 8 is 188th day of the year 2014 (`o` for "ordinal")
Expand All @@ -154,21 +153,18 @@
//!
//! let dt = NaiveDate::from_ymd_opt(2014, 7, 8)?
//! .and_hms_milli_opt(9, 10, 11, 12)?
//! .and_local_timezone(Utc)
//! .unwrap(); // `2014-07-08T09:10:11.012Z`
//! .and_utc(); // `2014-07-08T09:10:11.012Z`
//! assert_eq!(
//! dt,
//! NaiveDate::from_ymd_opt(2014, 7, 8)?
//! .and_hms_micro_opt(9, 10, 11, 12_000)?
//! .and_local_timezone(Utc)
//! .unwrap()
//! .and_utc()
//! );
//! assert_eq!(
//! dt,
//! NaiveDate::from_ymd_opt(2014, 7, 8)?
//! .and_hms_nano_opt(9, 10, 11, 12_000_000)?
//! .and_local_timezone(Utc)
//! .unwrap()
//! .and_utc()
//! );
//!
//! // dynamic verification
Expand Down Expand Up @@ -245,8 +241,7 @@
//! .unwrap()
//! .and_hms_nano_opt(12, 45, 59, 324310806)
//! .unwrap()
//! .and_local_timezone(Utc)
//! .unwrap()
//! .and_utc()
//! );
//!
//! // a sample of property manipulations (validates dynamically)
Expand Down Expand Up @@ -313,8 +308,7 @@
//! .unwrap()
//! .and_hms_nano_opt(12, 0, 9, 1)
//! .unwrap()
//! .and_local_timezone(Utc)
//! .unwrap();
//! .and_utc();
//! assert_eq!(format!("{:?}", dt_nano), "2014-11-28T12:00:09.000000001Z");
//! # }
//! # #[cfg(not(all(feature = "unstable-locales", feature = "alloc")))]
Expand Down
41 changes: 6 additions & 35 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl NaiveDate {
return Some(self);
}

match months.0 <= core::i32::MAX as u32 {
match months.0 <= i32::MAX as u32 {
true => self.diff_months(months.0 as i32),
false => None,
}
Expand Down Expand Up @@ -582,47 +582,18 @@ impl NaiveDate {
return Some(self);
}

// Copy `i32::MAX` here so we don't have to do a complicated cast
match months.0 <= 2_147_483_647 {
match months.0 <= i32::MAX as u32 {
true => self.diff_months(-(months.0 as i32)),
false => None,
}
}

const fn diff_months(self, months: i32) -> Option<Self> {
let (years, left) = ((months / 12), (months % 12));

// Determine new year (without taking months into account for now

let year = if (years > 0 && years > (MAX_YEAR - self.year()))
|| (years < 0 && years < (MIN_YEAR - self.year()))
{
return None;
} else {
self.year() + years
};

// Determine new month

let month = self.month() as i32 + left;
let (year, month) = if month <= 0 {
if year == MIN_YEAR {
return None;
}

(year - 1, month + 12)
} else if month > 12 {
if year == MAX_YEAR {
return None;
}

(year + 1, month - 12)
} else {
(year, month)
};
let months = try_opt!((self.year() * 12 + self.month() as i32 - 1).checked_add(months));
let year = months.div_euclid(12);
let month = months.rem_euclid(12) as u32 + 1;

// Clamp original day in case new month is shorter

let flags = YearFlags::from_year(year);
let feb_days = if flags.ndays() == 366 { 29 } else { 28 };
let days = [31, feb_days, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Expand All @@ -632,7 +603,7 @@ impl NaiveDate {
day = day_max;
};

NaiveDate::from_mdf(year, try_opt!(Mdf::new(month as u32, day, flags)))
NaiveDate::from_ymd_opt(year, month, day)
}

/// Add a duration in [`Days`] to the date
Expand Down
12 changes: 2 additions & 10 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,16 +903,8 @@ impl NaiveDateTime {
self.format_with_items(StrftimeItems::new(fmt))
}

/// Converts the `NaiveDateTime` into the timezone-aware `DateTime<Tz>`
/// with the provided timezone, if possible.
///
/// This can fail in cases where the local time represented by the `NaiveDateTime`
/// is not a valid local timestamp in the target timezone due to an offset transition
/// for example if the target timezone had a change from +00:00 to +01:00
/// occurring at 2015-09-05 22:59:59, then a local time of 2015-09-05 23:56:04
/// could never occur. Similarly, if the offset transitioned in the opposite direction
/// then there would be two local times of 2015-09-05 23:56:04, one at +00:00 and one
/// at +01:00.
/// Converts the `NaiveDateTime` into a timezone-aware `DateTime<Tz>` with the provided
/// time zone.
///
/// # Example
///
Expand Down
2 changes: 1 addition & 1 deletion src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ fn test_datetime_add_sub_invariant() {
#[test]
fn test_and_local_timezone() {
let ndt = NaiveDate::from_ymd_opt(2022, 6, 15).unwrap().and_hms_opt(18, 59, 36).unwrap();
let dt_utc = ndt.and_local_timezone(Utc).unwrap();
let dt_utc = ndt.and_utc();
assert_eq!(dt_utc.naive_local(), ndt);
assert_eq!(dt_utc.timezone(), Utc);

Expand Down
10 changes: 4 additions & 6 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod tests;
/// All methods accepting fractional seconds will accept such values.
///
/// ```
/// use chrono::{NaiveDate, NaiveTime, Utc};
/// use chrono::{NaiveDate, NaiveTime};
///
/// let t = NaiveTime::from_hms_milli_opt(8, 59, 59, 1_000).unwrap();
///
Expand All @@ -89,8 +89,7 @@ mod tests;
/// .unwrap()
/// .and_hms_nano_opt(23, 59, 59, 1_000_000_000)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap();
/// .and_utc();
/// # let _ = (t, dt1, dt2);
/// ```
///
Expand Down Expand Up @@ -172,14 +171,13 @@ mod tests;
/// will be represented as the second part being 60, as required by ISO 8601.
///
/// ```
/// use chrono::{NaiveDate, Utc};
/// use chrono::NaiveDate;
///
/// let dt = NaiveDate::from_ymd_opt(2015, 6, 30)
/// .unwrap()
/// .and_hms_milli_opt(23, 59, 59, 1_000)
/// .unwrap()
/// .and_local_timezone(Utc)
/// .unwrap();
/// .and_utc();
/// assert_eq!(format!("{:?}", dt), "2015-06-30T23:59:60Z");
/// ```
///
Expand Down
Loading
Loading