Skip to content

Commit

Permalink
Rename DateTime::and_time to DateTime::at
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 18, 2024
1 parent af627eb commit c2f3132
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ 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(),
DstTester::TO_WINTER_MONTH_DAY.0,
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(),
DstTester::TO_SUMMER_MONTH_DAY.0,
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(),
DstTester::TO_SUMMER_MONTH_DAY.0,
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())
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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)))
);
}

Expand All @@ -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);
Expand All @@ -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)))
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
),
Expand Down
4 changes: 2 additions & 2 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,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
Expand Down Expand Up @@ -812,7 +812,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?;
Expand Down
12 changes: 6 additions & 6 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,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)
}

Expand All @@ -685,7 +685,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.
Expand Down Expand Up @@ -722,7 +722,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.
Expand Down Expand Up @@ -759,7 +759,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.
Expand Down Expand Up @@ -796,7 +796,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.
Expand Down
4 changes: 2 additions & 2 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -954,7 +954,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 {
Expand Down
4 changes: 2 additions & 2 deletions src/offset/local/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions tests/dateutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c2f3132

Please sign in to comment.