Rename NaiveDate::and_time to DateTime::at

This commit is contained in:
Paul Dicker 2024-03-18 19:02:29 +01:00 committed by Paul Dicker
parent 9cf2adc55b
commit 16462e2a8c
8 changed files with 31 additions and 31 deletions

View File

@ -648,7 +648,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
#[must_use]
pub fn with_time(&self, time: NaiveTime) -> MappedLocalTime<Self> {
self.timezone().from_local_datetime(self.overflowing_naive_local().date().and_time(time))
self.timezone().from_local_datetime(self.overflowing_naive_local().date().at(time))
}
/// Makes a new `DateTime` with the hour number changed.
@ -761,7 +761,7 @@ impl DateTime<Utc> {
}
let date = try_err!(NaiveDate::from_num_days_from_ce(days as i32));
let time = try_err!(NaiveTime::from_num_seconds_from_midnight(secs as u32, nsecs));
Ok(date.and_time(time).and_utc())
Ok(date.at(time).and_utc())
}
/// Makes a new `DateTime<Utc>` from the number of non-leap milliseconds

View File

@ -43,7 +43,7 @@ impl TimeZone for DstTester {
DstTester::TO_WINTER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local());
.at(DstTester::transition_start_local());
let local_to_winter_transition_end = NaiveDate::from_ymd(
local.year(),
@ -51,7 +51,7 @@ impl TimeZone for DstTester {
DstTester::TO_WINTER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local() - TimeDelta::hours(1));
.at(DstTester::transition_start_local() - TimeDelta::hours(1));
let local_to_summer_transition_start = NaiveDate::from_ymd(
local.year(),
@ -59,7 +59,7 @@ impl TimeZone for DstTester {
DstTester::TO_SUMMER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local());
.at(DstTester::transition_start_local());
let local_to_summer_transition_end = NaiveDate::from_ymd(
local.year(),
@ -67,7 +67,7 @@ impl TimeZone for DstTester {
DstTester::TO_SUMMER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local() + TimeDelta::hours(1));
.at(DstTester::transition_start_local() + TimeDelta::hours(1));
if local < local_to_winter_transition_end || local >= local_to_summer_transition_end {
MappedLocalTime::Single(DstTester::summer_offset())
@ -95,7 +95,7 @@ impl TimeZone for DstTester {
DstTester::TO_WINTER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local())
.at(DstTester::transition_start_local())
- DstTester::summer_offset();
let utc_to_summer_transition = NaiveDate::from_ymd(
@ -104,7 +104,7 @@ impl TimeZone for DstTester {
DstTester::TO_SUMMER_MONTH_DAY.1,
)
.unwrap()
.and_time(DstTester::transition_start_local())
.at(DstTester::transition_start_local())
- DstTester::winter_offset();
if utc < utc_to_winter_transition || utc >= utc_to_summer_transition {
@ -597,7 +597,7 @@ fn signed_duration_since_autoref() {
}
#[test]
fn test_datetime_date_and_time() {
fn test_datetime_date_at() {
let tz = FixedOffset::east(5 * 60 * 60).unwrap();
let d = tz.at_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap();
assert_eq!(d.time(), NaiveTime::from_hms(7, 8, 9).unwrap());
@ -1542,7 +1542,7 @@ fn test_min_max_add_days() {
assert_eq!(beyond_min.checked_add_days(Days::new(0)), Some(beyond_min));
assert_eq!(
beyond_min.checked_add_days(Days::new(1)),
Some(offset_min.from_utc_datetime((NaiveDate::MIN + Days(1)).and_time(NaiveTime::MIN)))
Some(offset_min.from_utc_datetime((NaiveDate::MIN + Days(1)).at(NaiveTime::MIN)))
);
assert_eq!(beyond_min.checked_sub_days(Days::new(0)), Some(beyond_min));
assert_eq!(beyond_min.checked_sub_days(Days::new(1)), None);
@ -1552,7 +1552,7 @@ fn test_min_max_add_days() {
assert_eq!(beyond_max.checked_sub_days(Days::new(0)), Some(beyond_max));
assert_eq!(
beyond_max.checked_sub_days(Days::new(1)),
Some(offset_max.from_utc_datetime((NaiveDate::MAX - Days(1)).and_time(max_time)))
Some(offset_max.from_utc_datetime((NaiveDate::MAX - Days(1)).at(max_time)))
);
}
@ -1567,7 +1567,7 @@ fn test_min_max_add_months() {
assert_eq!(beyond_min.checked_add_months(Months::new(0)), Some(beyond_min));
assert_eq!(
beyond_min.checked_add_months(Months::new(1)),
Some(offset_min.from_utc_datetime((NaiveDate::MIN + Months(1)).and_time(NaiveTime::MIN)))
Some(offset_min.from_utc_datetime((NaiveDate::MIN + Months(1)).at(NaiveTime::MIN)))
);
assert_eq!(beyond_min.checked_sub_months(Months::new(0)), Some(beyond_min));
assert_eq!(beyond_min.checked_sub_months(Months::new(1)), None);
@ -1577,7 +1577,7 @@ fn test_min_max_add_months() {
assert_eq!(beyond_max.checked_sub_months(Months::new(0)), Some(beyond_max));
assert_eq!(
beyond_max.checked_sub_months(Months::new(1)),
Some(offset_max.from_utc_datetime((NaiveDate::MAX - Months(1)).and_time(max_time)))
Some(offset_max.from_utc_datetime((NaiveDate::MAX - Months(1)).at(max_time)))
);
}

View File

@ -182,10 +182,10 @@ fn format_inner(
Timestamp => (
1,
match (date, time, off) {
(Some(d), Some(t), None) => Some(d.and_time(*t).and_utc().timestamp()),
(Some(d), Some(t), Some(&(_, off))) => Some(
d.and_time(*t).and_utc().timestamp() - i64::from(off.local_minus_utc()),
),
(Some(d), Some(t), None) => Some(d.at(*t).and_utc().timestamp()),
(Some(d), Some(t), Some(&(_, off))) => {
Some(d.at(*t).and_utc().timestamp() - i64::from(off.local_minus_utc()))
}
(_, _, _) => None,
},
),

View File

@ -752,7 +752,7 @@ impl Parsed {
let date = self.to_naive_date();
let time = self.to_naive_time();
if let (Ok(date), Ok(time)) = (date, time) {
let datetime = date.and_time(time);
let datetime = date.at(time);
// verify the timestamp field if any
// the following is safe, `timestamp` is very limited in range
@ -814,7 +814,7 @@ impl Parsed {
// validate other fields (e.g. week) and return
let date = parsed.to_naive_date()?;
let time = parsed.to_naive_time()?;
Ok(date.and_time(time))
Ok(date.at(time))
} else {
// reproduce the previous error(s)
date?;

View File

@ -623,13 +623,13 @@ impl NaiveDate {
/// let d = NaiveDate::from_ymd(2015, 6, 3).unwrap();
/// let t = NaiveTime::from_hms_milli(12, 34, 56, 789).unwrap();
///
/// let dt: NaiveDateTime = d.and_time(t);
/// let dt: NaiveDateTime = d.at(t);
/// assert_eq!(dt.date(), d);
/// assert_eq!(dt.time(), t);
/// ```
#[inline]
#[must_use]
pub const fn and_time(self, time: NaiveTime) -> NaiveDateTime {
pub const fn at(self, time: NaiveTime) -> NaiveDateTime {
NaiveDateTime::new(self, time)
}
@ -656,7 +656,7 @@ impl NaiveDate {
#[inline]
pub const fn at_hms(self, hour: u32, min: u32, sec: u32) -> Result<NaiveDateTime, Error> {
let time = try_err!(NaiveTime::from_hms(hour, min, sec));
Ok(self.and_time(time))
Ok(self.at(time))
}
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond.
@ -693,7 +693,7 @@ impl NaiveDate {
milli: u32,
) -> Result<NaiveDateTime, Error> {
let time = try_err!(NaiveTime::from_hms_milli(hour, min, sec, milli));
Ok(self.and_time(time))
Ok(self.at(time))
}
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond.
@ -730,7 +730,7 @@ impl NaiveDate {
micro: u32,
) -> Result<NaiveDateTime, Error> {
let time = try_err!(NaiveTime::from_hms_micro(hour, min, sec, micro));
Ok(self.and_time(time))
Ok(self.at(time))
}
/// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond.
@ -767,7 +767,7 @@ impl NaiveDate {
nano: u32,
) -> Result<NaiveDateTime, Error> {
let time = try_err!(NaiveTime::from_hms_nano(hour, min, sec, nano));
Ok(self.and_time(time))
Ok(self.at(time))
}
/// Returns the packed month-day-flags.

View File

@ -71,7 +71,7 @@ pub struct NaiveDateTime {
impl NaiveDateTime {
/// Makes a new `NaiveDateTime` from date and time components.
/// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time)
/// Equivalent to [`date.at(time)`](./struct.NaiveDate.html#method.at)
/// and many other helper constructors on `NaiveDate`.
///
/// # Example
@ -939,7 +939,7 @@ impl NaiveDateTime {
/// The Unix Epoch, 1970-01-01 00:00:00.
pub const UNIX_EPOCH: Self =
expect(ok!(NaiveDate::from_ymd(1970, 1, 1)), "").and_time(NaiveTime::MIN);
expect(ok!(NaiveDate::from_ymd(1970, 1, 1)), "").at(NaiveTime::MIN);
}
impl From<NaiveDate> for NaiveDateTime {

View File

@ -187,7 +187,7 @@ fn naive_date_time_from_system_time(
// We have a concrete date.
let date = NaiveDate::from_ymd(st.wYear as i32, st.wMonth as u32, st.wDay as u32)
.map_err(|_| ())?;
return Ok(Some(date.and_time(time)));
return Ok(Some(date.at(time)));
}
// Resolve a rule with month, weekday, and nth weekday of the month to a date in the current
@ -213,7 +213,7 @@ fn naive_date_time_from_system_time(
}
Err(_) => return Err(()), // `st.wMonth` must be invalid
};
Ok(Some(date.and_time(time)))
Ok(Some(date.at(time)))
}
#[cfg(test)]

View File

@ -90,8 +90,8 @@ fn try_verify_against_date_command() {
let mut children = vec![];
for year in [1975, 1976, 1977, 2020, 2021, 2022, 2073, 2074, 2075, 2076, 2077].iter() {
children.push(thread::spawn(|| {
let mut date = NaiveDate::from_ymd(*year, 1, 1).unwrap().and_time(NaiveTime::MIN);
let end = NaiveDate::from_ymd(*year + 1, 1, 1).unwrap().and_time(NaiveTime::MIN);
let mut date = NaiveDate::from_ymd(*year, 1, 1).unwrap().at(NaiveTime::MIN);
let end = NaiveDate::from_ymd(*year + 1, 1, 1).unwrap().at(NaiveTime::MIN);
while date <= end {
verify_against_date_command_local(DATE_PATH, date);
date += chrono::TimeDelta::hours(1);