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 18, 2024
1 parent 5f5188c commit c048627
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 68 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()
/// .unwrap();
/// 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");
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 @@ -117,7 +117,7 @@ impl<'de> de::Deserialize<'de> for DateTime<Local> {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap();
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -163,7 +163,7 @@ pub mod ts_nanoseconds {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -262,7 +262,7 @@ pub mod ts_nanoseconds {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// );
/// let my_s = S { time: time.clone() };
Expand Down Expand Up @@ -311,7 +311,7 @@ pub mod ts_nanoseconds_option {
/// .unwrap()
/// .at_hms_nano(02, 04, 59, 918355733)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// ),
/// };
Expand Down Expand Up @@ -413,7 +413,7 @@ pub mod ts_nanoseconds_option {
/// .unwrap()
/// .at_hms_micro(02, 04, 59, 918355)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap();
/// let my_s = S { time: time.clone() };
///
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()
/// .unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -551,7 +551,7 @@ pub mod ts_microseconds {
/// .unwrap()
/// .at_hms_micro(02, 04, 59, 918355)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// );
/// let my_s = S { time: time.clone() };
Expand Down Expand Up @@ -591,7 +591,7 @@ pub mod ts_microseconds_option {
/// .unwrap()
/// .at_hms_micro(02, 04, 59, 918355)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// ),
/// };
Expand Down Expand Up @@ -691,7 +691,7 @@ pub mod ts_microseconds_option {
/// .unwrap()
/// .at_hms_milli(02, 04, 59, 918)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap();
/// let my_s = S { time: time.clone() };
///
Expand Down Expand Up @@ -729,7 +729,7 @@ pub mod ts_milliseconds {
/// .unwrap()
/// .at_hms_milli(02, 04, 59, 918)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// };
/// let as_string = serde_json::to_string(&my_s)?;
Expand Down Expand Up @@ -822,7 +822,7 @@ pub mod ts_milliseconds {
/// .unwrap()
/// .at_hms_milli(02, 04, 59, 918)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// );
/// let my_s = S { time: time.clone() };
Expand Down Expand Up @@ -862,7 +862,7 @@ pub mod ts_milliseconds_option {
/// .unwrap()
/// .at_hms_milli(02, 04, 59, 918)
/// .unwrap()
/// .and_utc()
/// .in_utc()
/// .unwrap(),
/// ),
/// };
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 @@ -754,7 +754,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 @@ -135,22 +135,22 @@
//! # fn doctest() -> Option<()> {
//!
//! let dt = Utc.at_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap(); // `2014-07-08T09:10:11Z`
//! assert_eq!(dt, NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms(9, 10, 11).unwrap().and_utc().unwrap());
//! assert_eq!(dt, NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms(9, 10, 11).unwrap().in_utc().unwrap());
//!
//! // 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).unwrap().at_hms_milli(9, 10, 11, 12).unwrap().and_utc().unwrap(); // `2014-07-08T09:10:11.012Z`
//! assert_eq!(dt, NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms_micro(9, 10, 11, 12_000).unwrap().and_utc().unwrap());
//! assert_eq!(dt, NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms_nano(9, 10, 11, 12_000_000).unwrap().and_utc().unwrap());
//! let dt = NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms_milli(9, 10, 11, 12).unwrap().in_utc().unwrap(); // `2014-07-08T09:10:11.012Z`
//! assert_eq!(dt, NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms_micro(9, 10, 11, 12_000).unwrap().in_utc().unwrap());
//! assert_eq!(dt, NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms_nano(9, 10, 11, 12_000_000).unwrap().in_utc().unwrap());
//!
//! // dynamic verification
//! assert_eq!(
//! Utc.at_ymd_and_hms(2014, 7, 8, 21, 15, 33),
//! MappedLocalTime::Single(
//! NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms(21, 15, 33).unwrap().and_utc()
//! NaiveDate::from_ymd(2014, 7, 8).unwrap().at_hms(21, 15, 33).unwrap().in_utc()
//! )
//! );
//! assert_eq!(Utc.at_ymd_and_hms(2014, 7, 8, 80, 15, 33), MappedLocalTime::None);
Expand Down Expand Up @@ -193,7 +193,7 @@
//! // time zone accessor and manipulation
//! assert_eq!(dt.offset().fix().local_minus_utc(), 9 * 3600);
//! assert_eq!(dt.timezone(), FixedOffset::east(9 * 3600).unwrap());
//! assert_eq!(dt.with_timezone(&Utc), NaiveDate::from_ymd(2014, 11, 28).unwrap().at_hms_nano(12, 45, 59, 324310806).unwrap().and_utc().unwrap());
//! assert_eq!(dt.with_timezone(&Utc), NaiveDate::from_ymd(2014, 11, 28).unwrap().at_hms_nano(12, 45, 59, 324310806).unwrap().in_utc().unwrap());
//!
//! // a sample of property manipulations (validates dynamically)
//! assert_eq!(dt.with_day(29).unwrap().weekday(), Weekday::Sat); // 2014-11-29 is Saturday
Expand Down Expand Up @@ -256,7 +256,7 @@
//! assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z");
//!
//! // 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().unwrap();
//! let dt_nano = NaiveDate::from_ymd(2014, 11, 28).unwrap().at_hms_nano(12, 0, 9, 1).unwrap().in_utc().unwrap();
//! 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 @@ -716,11 +716,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
16 changes: 8 additions & 8 deletions src/naive/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub mod ts_nanoseconds {
where
S: ser::Serializer,
{
serializer.serialize_i64(dt.and_utc().timestamp_nanos().map_err(|_| {
serializer.serialize_i64(dt.in_utc().timestamp_nanos().map_err(|_| {
ser::Error::custom("value out of range for a timestamp with nanosecond precision")
})?)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ pub mod ts_nanoseconds_option {
{
match *opt {
Some(ref dt) => {
serializer.serialize_some(&dt.and_utc().timestamp_nanos().map_err(|_| {
serializer.serialize_some(&dt.in_utc().timestamp_nanos().map_err(|_| {
ser::Error::custom(
"value out of range for a timestamp with nanosecond precision",
)
Expand Down Expand Up @@ -393,7 +393,7 @@ pub mod ts_microseconds {
where
S: ser::Serializer,
{
serializer.serialize_i64(dt.and_utc().timestamp_micros())
serializer.serialize_i64(dt.in_utc().timestamp_micros())
}

/// Deserialize a `NaiveDateTime` from a microseconds timestamp
Expand Down Expand Up @@ -523,7 +523,7 @@ pub mod ts_microseconds_option {
S: ser::Serializer,
{
match *opt {
Some(ref dt) => serializer.serialize_some(&dt.and_utc().timestamp_micros()),
Some(ref dt) => serializer.serialize_some(&dt.in_utc().timestamp_micros()),
None => serializer.serialize_none(),
}
}
Expand Down Expand Up @@ -652,7 +652,7 @@ pub mod ts_milliseconds {
where
S: ser::Serializer,
{
serializer.serialize_i64(dt.and_utc().timestamp_millis())
serializer.serialize_i64(dt.in_utc().timestamp_millis())
}

/// Deserialize a `NaiveDateTime` from a milliseconds timestamp
Expand Down Expand Up @@ -779,7 +779,7 @@ pub mod ts_milliseconds_option {
S: ser::Serializer,
{
match *opt {
Some(ref dt) => serializer.serialize_some(&dt.and_utc().timestamp_millis()),
Some(ref dt) => serializer.serialize_some(&dt.in_utc().timestamp_millis()),
None => serializer.serialize_none(),
}
}
Expand Down Expand Up @@ -906,7 +906,7 @@ pub mod ts_seconds {
where
S: ser::Serializer,
{
serializer.serialize_i64(dt.and_utc().timestamp())
serializer.serialize_i64(dt.in_utc().timestamp())
}

/// Deserialize a `NaiveDateTime` from a seconds timestamp
Expand Down Expand Up @@ -1029,7 +1029,7 @@ pub mod ts_seconds_option {
S: ser::Serializer,
{
match *opt {
Some(ref dt) => serializer.serialize_some(&dt.and_utc().timestamp()),
Some(ref dt) => serializer.serialize_some(&dt.in_utc().timestamp()),
None => serializer.serialize_none(),
}
}
Expand Down
Loading

0 comments on commit c048627

Please sign in to comment.