Skip to content

Commit

Permalink
Rename NaiveDateTime::and_utc to in_utc
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 22, 2024
1 parent 0697e88 commit f80253c
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 72 deletions.
24 changes: 12 additions & 12 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::NaiveDate;
///
/// let dt = NaiveDate::from_ymd(1970, 1, 1)?.at_hms_milli(0, 0, 1, 444)?.and_utc();
/// let dt = NaiveDate::from_ymd(1970, 1, 1)?.at_hms_milli(0, 0, 1, 444)?.in_utc();
/// assert_eq!(dt.timestamp_millis(), 1_444);
///
/// let dt = NaiveDate::from_ymd(2001, 9, 9)?.at_hms_milli(1, 46, 40, 555)?.and_utc();
/// let dt = NaiveDate::from_ymd(2001, 9, 9)?.at_hms_milli(1, 46, 40, 555)?.in_utc();
/// assert_eq!(dt.timestamp_millis(), 1_000_000_000_555);
/// # Ok::<(), chrono::Error>(())
/// ```
Expand All @@ -172,10 +172,10 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::NaiveDate;
///
/// let dt = NaiveDate::from_ymd(1970, 1, 1)?.at_hms_micro(0, 0, 1, 444)?.and_utc();
/// let dt = NaiveDate::from_ymd(1970, 1, 1)?.at_hms_micro(0, 0, 1, 444)?.in_utc();
/// assert_eq!(dt.timestamp_micros(), 1_000_444);
///
/// let dt = NaiveDate::from_ymd(2001, 9, 9)?.at_hms_micro(1, 46, 40, 555)?.and_utc();
/// let dt = NaiveDate::from_ymd(2001, 9, 9)?.at_hms_micro(1, 46, 40, 555)?.in_utc();
/// assert_eq!(dt.timestamp_micros(), 1_000_000_000_000_555);
/// # Ok::<(), chrono::Error>(())
/// ```
Expand All @@ -201,22 +201,22 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
/// use chrono::{Error, NaiveDate};
///
/// let dt = NaiveDate::from_ymd(1970, 1, 1)?.at_hms_nano(0, 0, 1, 444)?.and_utc();
/// let dt = NaiveDate::from_ymd(1970, 1, 1)?.at_hms_nano(0, 0, 1, 444)?.in_utc();
/// assert_eq!(dt.timestamp_nanos(), Ok(1_000_000_444));
///
/// let dt = NaiveDate::from_ymd(2001, 9, 9)?.at_hms_nano(1, 46, 40, 555)?.and_utc();
/// let dt = NaiveDate::from_ymd(2001, 9, 9)?.at_hms_nano(1, 46, 40, 555)?.in_utc();
/// assert_eq!(dt.timestamp_nanos(), Ok(1_000_000_000_000_000_555));
///
/// let dt = NaiveDate::from_ymd(1677, 9, 21)?.at_hms_nano(0, 12, 43, 145_224_192)?.and_utc();
/// let dt = NaiveDate::from_ymd(1677, 9, 21)?.at_hms_nano(0, 12, 43, 145_224_192)?.in_utc();
/// assert_eq!(dt.timestamp_nanos(), Ok(-9_223_372_036_854_775_808));
///
/// let dt = NaiveDate::from_ymd(2262, 4, 11)?.at_hms_nano(23, 47, 16, 854_775_807)?.and_utc();
/// let dt = NaiveDate::from_ymd(2262, 4, 11)?.at_hms_nano(23, 47, 16, 854_775_807)?.in_utc();
/// assert_eq!(dt.timestamp_nanos(), Ok(9_223_372_036_854_775_807));
///
/// let dt = NaiveDate::from_ymd(1677, 9, 21)?.at_hms_nano(0, 12, 43, 145_224_191)?.and_utc();
/// let dt = NaiveDate::from_ymd(1677, 9, 21)?.at_hms_nano(0, 12, 43, 145_224_191)?.in_utc();
/// assert_eq!(dt.timestamp_nanos(), Err(Error::OutOfRange));
///
/// let dt = NaiveDate::from_ymd(2262, 4, 11)?.at_hms_nano(23, 47, 16, 854_775_808)?.and_utc();
/// let dt = NaiveDate::from_ymd(2262, 4, 11)?.at_hms_nano(23, 47, 16, 854_775_808)?.in_utc();
/// assert_eq!(dt.timestamp_nanos(), Err(Error::OutOfRange));
/// # Ok::<(), Error>(())
/// ```
Expand Down Expand Up @@ -532,7 +532,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// .unwrap()
/// .at_hms_micro(18, 30, 9, 453_829)
/// .unwrap()
/// .and_utc();
/// .in_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 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.at(time).and_utc())
Ok(date.at(time).in_utc())
}

/// Makes a new `DateTime<Utc>` from the number of non-leap milliseconds
Expand Down
24 changes: 12 additions & 12 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<'de> de::Deserialize<'de> for DateTime<Local> {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc();
/// .in_utc();
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -167,7 +167,7 @@ pub mod ts_nanoseconds {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355733}"#);
Expand Down Expand Up @@ -265,7 +265,7 @@ pub mod ts_nanoseconds {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// );
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -313,7 +313,7 @@ pub mod ts_nanoseconds_option {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// ),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -414,7 +414,7 @@ pub mod ts_nanoseconds_option {
/// .unwrap()
/// .at_hms_micro(02, 04, 59, 918355)
/// .unwrap()
/// .and_utc();
/// .in_utc();
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -451,7 +451,7 @@ pub mod ts_microseconds {
/// .unwrap()
/// .at_hms_micro(02, 04, 59, 918355)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918355}"#);
Expand Down Expand Up @@ -550,7 +550,7 @@ pub mod ts_microseconds {
/// .unwrap()
/// .at_hms_micro(02, 04, 59, 918355)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// );
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -589,7 +589,7 @@ pub mod ts_microseconds_option {
/// .unwrap()
/// .at_hms_micro(02, 04, 59, 918355)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// ),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -685,7 +685,7 @@ pub mod ts_microseconds_option {
/// }
///
/// let time =
/// NaiveDate::from_ymd(2018, 5, 17).unwrap().at_hms_milli(02, 04, 59, 918).unwrap().and_utc();
/// NaiveDate::from_ymd(2018, 5, 17).unwrap().at_hms_milli(02, 04, 59, 918).unwrap().in_utc();
/// let my_s = S { time: time.clone() };
///
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -722,7 +722,7 @@ pub mod ts_milliseconds {
/// .unwrap()
/// .at_hms_milli(02, 04, 59, 918)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
/// assert_eq!(as_string, r#"{"time":1526522699918}"#);
Expand Down Expand Up @@ -810,7 +810,7 @@ pub mod ts_milliseconds {
/// }
///
/// let time = Some(
/// NaiveDate::from_ymd(2018, 5, 17).unwrap().at_hms_milli(02, 04, 59, 918).unwrap().and_utc(),
/// NaiveDate::from_ymd(2018, 5, 17).unwrap().at_hms_milli(02, 04, 59, 918).unwrap().in_utc(),
/// );
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -849,7 +849,7 @@ pub mod ts_milliseconds_option {
/// .unwrap()
/// .at_hms_milli(02, 04, 59, 918)
/// .unwrap()
/// .and_utc(),
/// .in_utc(),
/// ),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down
10 changes: 5 additions & 5 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn test_datetime_from_timestamp_nanos() {
fn test_datetime_from_timestamp() {
let from_timestamp = |secs| DateTime::from_timestamp(secs, 0);
let ymdhms =
|y, m, d, h, n, s| NaiveDate::from_ymd(y, m, d).unwrap().at_hms(h, n, s).unwrap().and_utc();
|y, m, d, h, n, s| NaiveDate::from_ymd(y, m, d).unwrap().at_hms(h, n, s).unwrap().in_utc();
assert_eq!(from_timestamp(-1), Ok(ymdhms(1969, 12, 31, 23, 59, 59)));
assert_eq!(from_timestamp(0), Ok(ymdhms(1970, 1, 1, 0, 0, 0)));
assert_eq!(from_timestamp(1), Ok(ymdhms(1970, 1, 1, 0, 0, 1)));
Expand All @@ -258,7 +258,7 @@ fn test_datetime_from_timestamp() {
#[test]
fn test_datetime_timestamp() {
let to_timestamp = |y, m, d, h, n, s| {
NaiveDate::from_ymd(y, m, d).unwrap().at_hms(h, n, s).unwrap().and_utc().timestamp()
NaiveDate::from_ymd(y, m, d).unwrap().at_hms(h, n, s).unwrap().in_utc().timestamp()
};
assert_eq!(to_timestamp(1969, 12, 31, 23, 59, 59), -1);
assert_eq!(to_timestamp(1970, 1, 1, 0, 0, 0), 0);
Expand Down Expand Up @@ -841,7 +841,7 @@ fn test_rfc3339_opts() {
assert_eq!(dt.to_rfc3339_opts(Nanos, false), "2018-01-11T10:05:13.084660000+08:00");
assert_eq!(dt.to_rfc3339_opts(AutoSi, false), "2018-01-11T10:05:13.084660+08:00");

let ut = dt.naive_utc().and_utc();
let ut = dt.naive_utc().in_utc();
assert_eq!(ut.to_rfc3339_opts(Secs, false), "2018-01-11T02:05:13+00:00");
assert_eq!(ut.to_rfc3339_opts(Secs, true), "2018-01-11T02:05:13Z");
assert_eq!(ut.to_rfc3339_opts(Millis, false), "2018-01-11T02:05:13.084+00:00");
Expand Down Expand Up @@ -1379,7 +1379,7 @@ fn test_years_elapsed() {
#[test]
fn test_datetime_add_assign() {
let naivedatetime = NaiveDate::from_ymd(2000, 1, 1).unwrap().at_hms(0, 0, 0).unwrap();
let datetime = naivedatetime.and_utc();
let datetime = naivedatetime.in_utc();
let mut datetime_add = datetime;

datetime_add += TimeDelta::seconds(60);
Expand Down Expand Up @@ -1416,7 +1416,7 @@ fn test_datetime_add_assign_local() {
#[test]
fn test_datetime_sub_assign() {
let naivedatetime = NaiveDate::from_ymd(2000, 1, 1).unwrap().at_hms(12, 0, 0).unwrap();
let datetime = naivedatetime.and_utc();
let datetime = naivedatetime.in_utc();
let mut datetime_sub = datetime;

datetime_sub -= TimeDelta::minutes(90);
Expand Down
4 changes: 2 additions & 2 deletions src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ fn format_inner(
Timestamp => (
1,
match (date, time, off) {
(Some(d), Some(t), None) => Some(d.at(*t).and_utc().timestamp()),
(Some(d), Some(t), None) => Some(d.at(*t).in_utc().timestamp()),
(Some(d), Some(t), Some(&(_, off))) => {
Some(d.at(*t).and_utc().timestamp() - i64::from(off.local_minus_utc()))
Some(d.at(*t).in_utc().timestamp() - i64::from(off.local_minus_utc()))
}
(_, _, _) => None,
},
Expand Down
2 changes: 1 addition & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! let formatted = format!("{}", date_time.format("%Y-%m-%d %H:%M:%S"));
//! assert_eq!(formatted, "2020-11-10 00:01:32");
//!
//! let parsed = NaiveDateTime::parse_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?.and_utc();
//! let parsed = NaiveDateTime::parse_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?.in_utc();
//! assert_eq!(parsed, date_time);
//! # }
//! # Ok::<(), chrono::ParseError>(())
Expand Down
2 changes: 1 addition & 1 deletion src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ impl Parsed {

// verify the timestamp field if any
// the following is safe, `timestamp` is very limited in range
let timestamp = datetime.and_utc().timestamp() - i64::from(offset);
let timestamp = datetime.in_utc().timestamp() - i64::from(offset);
if let Some(given_timestamp) = self.timestamp {
// if `datetime` represents a leap second, it might be off by one second.
if given_timestamp != timestamp
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,35 @@
//! dt,
//! NaiveDate::from_ymd(2014, 7, 8)?
//! .at_hms(9, 10, 11)?
//! .and_utc()
//! .in_utc()
//! );
//!
//! // July 8 is 188th day of the year 2014 (`o` for "ordinal")
//! assert_eq!(dt, NaiveDate::from_yo(2014, 189).unwrap().at_hms(9, 10, 11).unwrap().and_utc());
//! assert_eq!(dt, NaiveDate::from_yo(2014, 189).unwrap().at_hms(9, 10, 11).unwrap().in_utc());
//! // July 8 is Tuesday in ISO week 28 of the year 2014.
//! assert_eq!(dt, NaiveDate::from_isoywd(2014, 28, Weekday::Tue).unwrap().at_hms(9, 10, 11).unwrap().and_utc());
//! assert_eq!(dt, NaiveDate::from_isoywd(2014, 28, Weekday::Tue).unwrap().at_hms(9, 10, 11).unwrap().in_utc());
//!
//! let dt = NaiveDate::from_ymd(2014, 7, 8)?
//! .at_hms_milli(9, 10, 11, 12)?
//! .and_utc(); // `2014-07-08T09:10:11.012Z`
//! .in_utc(); // `2014-07-08T09:10:11.012Z`
//! assert_eq!(
//! dt,
//! NaiveDate::from_ymd(2014, 7, 8)?
//! .at_hms_micro(9, 10, 11, 12_000)?
//! .and_utc()
//! .in_utc()
//! );
//! assert_eq!(
//! dt,
//! NaiveDate::from_ymd(2014, 7, 8)?
//! .at_hms_nano(9, 10, 11, 12_000_000)?
//! .and_utc()
//! .in_utc()
//! );
//!
//! // dynamic verification
//! assert_eq!(
//! Utc.at_ymd_and_hms(2014, 7, 8, 21, 15, 33),
//! MappedLocalTime::Single(
//! NaiveDate::from_ymd(2014, 7, 8)?.at_hms(21, 15, 33)?.and_utc()
//! NaiveDate::from_ymd(2014, 7, 8)?.at_hms(21, 15, 33)?.in_utc()
//! )
//! );
//! assert_eq!(Utc.at_ymd_and_hms(2014, 7, 8, 80, 15, 33), MappedLocalTime::None);
Expand Down Expand Up @@ -203,7 +203,7 @@
//! assert_eq!(dt.timezone(), FixedOffset::east(9 * 3600)?);
//! assert_eq!(
//! dt.with_timezone(&Utc),
//! NaiveDate::from_ymd(2014, 11, 28)?.at_hms_nano(12, 45, 59, 324310806)?.and_utc()
//! NaiveDate::from_ymd(2014, 11, 28)?.at_hms_nano(12, 45, 59, 324310806)?.in_utc()
//! );
//!
//! // a sample of property manipulations (validates dynamically)
Expand Down Expand Up @@ -271,7 +271,7 @@
//!
//! // Note that milli/nanoseconds are only printed if they are non-zero
//! let dt_nano =
//! NaiveDate::from_ymd(2014, 11, 28).unwrap().at_hms_nano(12, 0, 9, 1).unwrap().and_utc();
//! NaiveDate::from_ymd(2014, 11, 28).unwrap().at_hms_nano(12, 0, 9, 1).unwrap().in_utc();
//! assert_eq!(format!("{:?}", dt_nano), "2014-11-28T12:00:09.000000001Z");
//! # }
//! # #[cfg(not(all(feature = "unstable-locales", feature = "alloc")))]
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 @@ -708,11 +708,11 @@ impl NaiveDateTime {
///
/// ```
/// use chrono::{NaiveDate, Utc};
/// let dt = NaiveDate::from_ymd(2023, 1, 30).unwrap().at_hms(19, 32, 33).unwrap().and_utc();
/// let dt = NaiveDate::from_ymd(2023, 1, 30).unwrap().at_hms(19, 32, 33).unwrap().in_utc();
/// assert_eq!(dt.timezone(), Utc);
/// ```
#[must_use]
pub const fn and_utc(self) -> DateTime<Utc> {
pub const fn in_utc(self) -> DateTime<Utc> {
DateTime::from_naive_utc_and_offset(self, Utc)
}

Expand Down
Loading

0 comments on commit f80253c

Please sign in to comment.