Skip to content

Commit

Permalink
Rename TimeZone::with_ymd_and_hms to and_ymd_and_hms
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 16, 2024
1 parent 80a0389 commit 759def9
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 156 deletions.
2 changes: 1 addition & 1 deletion bench/benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn bench_naivedate_add_signed(c: &mut Criterion) {
}

fn bench_datetime_with(c: &mut Criterion) {
let dt = FixedOffset::east(3600).unwrap().with_ymd_and_hms(2023, 9, 23, 7, 36, 0).unwrap();
let dt = FixedOffset::east(3600).unwrap().and_ymd_and_hms(2023, 9, 23, 7, 36, 0).unwrap();
c.bench_function("bench_datetime_with", |b| {
b.iter(|| black_box(black_box(dt).with_hour(12)).unwrap())
});
Expand Down
2 changes: 1 addition & 1 deletion ci/core-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
use chrono::{TimeZone, Utc};

pub fn create_time() {
let _ = Utc.with_ymd_and_hms(2019, 1, 1, 0, 0, 0).unwrap();
let _ = Utc.and_ymd_and_hms(2019, 1, 1, 0, 0, 0).unwrap();
}
36 changes: 17 additions & 19 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::prelude::*;
///
/// let date: DateTime<Utc> = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
/// let date: DateTime<Utc> = Utc.and_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
/// let other: DateTime<FixedOffset> =
/// FixedOffset::east(23).unwrap().with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
/// FixedOffset::east(23).unwrap().and_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
/// assert_eq!(date.date_naive(), other.date_naive());
/// ```
#[inline]
Expand All @@ -130,7 +130,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::{DateTime, TimeZone, Utc};
///
/// let dt: DateTime<Utc> = Utc.with_ymd_and_hms(2015, 5, 15, 0, 0, 0).unwrap();
/// let dt: DateTime<Utc> = Utc.and_ymd_and_hms(2015, 5, 15, 0, 0, 0).unwrap();
/// assert_eq!(dt.timestamp(), 1431648000);
///
/// assert_eq!(DateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_nanos())?, dt);
Expand Down Expand Up @@ -1044,7 +1044,7 @@ impl DateTime<FixedOffset> {
/// # use chrono::{DateTime, FixedOffset, TimeZone};
/// assert_eq!(
/// DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT").unwrap(),
/// FixedOffset::east(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
/// FixedOffset::east(0).unwrap().and_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
/// );
/// ```
pub fn parse_from_rfc2822(s: &str) -> ParseResult<DateTime<FixedOffset>> {
Expand Down Expand Up @@ -1132,7 +1132,7 @@ impl DateTime<FixedOffset> {
/// .unwrap();
/// assert_eq!(
/// datetime,
/// FixedOffset::east(2 * 3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
/// FixedOffset::east(2 * 3600).unwrap().and_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()
/// );
/// assert_eq!(remainder, " trailing text");
/// ```
Expand Down Expand Up @@ -1178,7 +1178,7 @@ where
/// ```rust
/// use chrono::prelude::*;
///
/// let date_time: DateTime<Utc> = Utc.with_ymd_and_hms(2017, 04, 02, 12, 50, 32).unwrap();
/// let date_time: DateTime<Utc> = Utc.and_ymd_and_hms(2017, 04, 02, 12, 50, 32).unwrap();
/// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M"));
/// assert_eq!(formatted, "02/04/2017 12:50");
/// ```
Expand Down Expand Up @@ -1304,11 +1304,11 @@ impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateTime<Tz2>> for DateTime<Tz> {
/// use chrono::prelude::*;
///
/// let earlier = Utc
/// .with_ymd_and_hms(2015, 5, 15, 2, 0, 0)
/// .and_ymd_and_hms(2015, 5, 15, 2, 0, 0)
/// .unwrap()
/// .with_timezone(&FixedOffset::west(1 * 3600).unwrap());
/// let later = Utc
/// .with_ymd_and_hms(2015, 5, 15, 3, 0, 0)
/// .and_ymd_and_hms(2015, 5, 15, 3, 0, 0)
/// .unwrap()
/// .with_timezone(&FixedOffset::west(5 * 3600).unwrap());
///
Expand Down Expand Up @@ -1852,20 +1852,20 @@ where
E: ::core::fmt::Debug,
{
assert_eq!(
to_string_utc(&Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()).ok(),
to_string_utc(&Utc.and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()).ok(),
Some(r#""2014-07-24T12:34:06Z""#.into())
);

assert_eq!(
to_string_fixed(
&FixedOffset::east(3660).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
&FixedOffset::east(3660).unwrap().and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
)
.ok(),
Some(r#""2014-07-24T12:34:06+01:01""#.into())
);
assert_eq!(
to_string_fixed(
&FixedOffset::east(3650).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
&FixedOffset::east(3650).unwrap().and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
)
.ok(),
// An offset with seconds is not allowed by RFC 3339, so we round it to the nearest minute.
Expand All @@ -1892,25 +1892,23 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(

assert_eq!(
norm(&utc_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
norm(&Some(Utc.and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
);
assert_eq!(
norm(&utc_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
norm(&Some(Utc.and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
);

assert_eq!(
norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
norm(&Some(
FixedOffset::east(0).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
))
norm(&Some(FixedOffset::east(0).unwrap().and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
);
assert_eq!(
norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
norm(&Some(
FixedOffset::east(60 * 60 + 23 * 60)
.unwrap()
.with_ymd_and_hms(2014, 7, 24, 13, 57, 6)
.and_ymd_and_hms(2014, 7, 24, 13, 57, 6)
.unwrap()
))
);
Expand All @@ -1919,11 +1917,11 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(
// the conversion didn't change the instant itself
assert_eq!(
local_from_str(r#""2014-07-24T12:34:06Z""#).expect("local should parse"),
Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
Utc.and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
);
assert_eq!(
local_from_str(r#""2014-07-24T13:57:06+01:23""#).expect("local should parse with offset"),
Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
Utc.and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
);

assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
Expand Down
12 changes: 6 additions & 6 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ pub mod ts_milliseconds_option {
/// time: DateTime<Utc>,
/// }
///
/// let time = Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap();
/// let time = Utc.and_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap();
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -1002,7 +1002,7 @@ pub mod ts_seconds {
/// time: DateTime<Utc>,
/// }
///
/// let my_s = S { time: Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap() };
/// let my_s = S { time: Utc.and_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap() };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
/// # Ok::<(), serde_json::Error>(())
Expand Down Expand Up @@ -1088,7 +1088,7 @@ pub mod ts_seconds {
/// time: Option<DateTime<Utc>>,
/// }
///
/// let time = Some(Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap());
/// let time = Some(Utc.and_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap());
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -1120,7 +1120,7 @@ pub mod ts_seconds_option {
/// time: Option<DateTime<Utc>>,
/// }
///
/// let my_s = S { time: Some(Utc.with_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap()) };
/// let my_s = S { time: Some(Utc.and_ymd_and_hms(2015, 5, 15, 10, 0, 0).unwrap()) };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1431684000}"#);
/// # Ok::<(), serde_json::Error>(())
Expand Down Expand Up @@ -1226,7 +1226,7 @@ mod tests {
// it is not self-describing.
use bincode::{deserialize, serialize};

let dt = Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap();
let dt = Utc.and_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap();
let encoded = serialize(&dt).unwrap();
let decoded: DateTime<Utc> = deserialize(&encoded).unwrap();
assert_eq!(dt, decoded);
Expand Down Expand Up @@ -1269,7 +1269,7 @@ mod tests {
let tz = TestTimeZone;
assert_eq!(format!("{:?}", &tz), "TEST");

let dt = tz.with_ymd_and_hms(2023, 4, 24, 21, 10, 33).unwrap();
let dt = tz.and_ymd_and_hms(2023, 4, 24, 21, 10, 33).unwrap();
let encoded = serde_json::to_string(&dt).unwrap();
dbg!(&encoded);
let decoded: DateTime<FixedOffset> = serde_json::from_str(&encoded).unwrap();
Expand Down
Loading

0 comments on commit 759def9

Please sign in to comment.