Skip to content

Commit

Permalink
Rename TimeZone::timestamp* to TimeZone::and_timestamp*
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 16, 2024
1 parent d286f93 commit 447dd5a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 52 deletions.
14 changes: 7 additions & 7 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// (aka "UNIX timestamp").
///
/// The reverse operation of creating a [`DateTime`] from a timestamp can be performed
/// using [`from_timestamp`](DateTime::from_timestamp) or [`TimeZone::timestamp`].
/// using [`from_timestamp`](DateTime::from_timestamp) or [`TimeZone::and_timestamp`].
///
/// ```
/// use chrono::{DateTime, TimeZone, Utc};
Expand Down Expand Up @@ -772,7 +772,7 @@ impl DateTime<Utc> {
/// [`timestamp_subsec_nanos`](DateTime::timestamp_subsec_nanos).
///
/// If you need to create a `DateTime` with a [`TimeZone`] different from [`Utc`], use
/// [`TimeZone::timestamp`] or [`DateTime::with_timezone`].
/// [`TimeZone::and_timestamp`] or [`DateTime::with_timezone`].
///
/// The nanosecond part can exceed 1,000,000,000 in order to represent a
/// [leap second](NaiveTime#leap-second-handling), but only when `secs % 60 == 59`.
Expand Down Expand Up @@ -815,7 +815,7 @@ impl DateTime<Utc> {
/// This is guaranteed to round-trip with [`timestamp_millis`](DateTime::timestamp_millis).
///
/// If you need to create a `DateTime` with a [`TimeZone`] different from [`Utc`], use
/// [`TimeZone::timestamp_millis`] or [`DateTime::with_timezone`].
/// [`TimeZone::and_timestamp_millis`] or [`DateTime::with_timezone`].
///
/// # Errors
///
Expand Down Expand Up @@ -846,7 +846,7 @@ impl DateTime<Utc> {
/// This is guaranteed to round-trip with [`timestamp_micros`](DateTime::timestamp_micros).
///
/// If you need to create a `DateTime` with a [`TimeZone`] different from [`Utc`], use
/// [`TimeZone::timestamp_micros`] or [`DateTime::with_timezone`].
/// [`TimeZone::and_timestamp_micros`] or [`DateTime::with_timezone`].
///
/// # Errors
///
Expand Down Expand Up @@ -881,7 +881,7 @@ impl DateTime<Utc> {
/// This is guaranteed to round-trip with [`timestamp_nanos`](DateTime::timestamp_nanos).
///
/// If you need to create a `DateTime` with a [`TimeZone`] different from [`Utc`], use
/// [`TimeZone::timestamp_nanos`] or [`DateTime::with_timezone`].
/// [`TimeZone::and_timestamp_nanos`] or [`DateTime::with_timezone`].
///
/// The UNIX epoch starts on midnight, January 1, 1970, UTC.
///
Expand Down Expand Up @@ -1744,7 +1744,7 @@ impl TryFrom<SystemTime> for DateTime<Utc> {
}
}
};
Utc.timestamp(sec, nsec).single().ok_or(OutOfRange::new())
Utc.and_timestamp(sec, nsec).single().ok_or(OutOfRange::new())
}
}

Expand Down Expand Up @@ -1800,7 +1800,7 @@ impl From<js_sys::Date> for DateTime<Utc> {
))]
impl From<&js_sys::Date> for DateTime<Utc> {
fn from(date: &js_sys::Date) -> DateTime<Utc> {
Utc.timestamp_millis(date.get_time() as i64).unwrap()
Utc.and_timestamp_millis(date.get_time() as i64).unwrap()
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ pub mod ts_nanoseconds {
/// }
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(1526522699, 918355733).unwrap() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(1526522699, 918355733).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(-1, 999_999_999).unwrap() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(-1, 999_999_999).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
Expand Down Expand Up @@ -348,7 +348,7 @@ pub mod ts_nanoseconds_option {
/// }
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(1526522699, 918355733).single() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(1526522699, 918355733).single() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<DateTime<Utc>>, D::Error>
Expand Down Expand Up @@ -482,10 +482,10 @@ pub mod ts_microseconds {
/// }
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(1526522699, 918355000).unwrap() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(1526522699, 918355000).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(-1, 999_999_000).unwrap() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(-1, 999_999_000).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
Expand Down Expand Up @@ -626,7 +626,7 @@ pub mod ts_microseconds_option {
/// }
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(1526522699, 918355000).single() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(1526522699, 918355000).single() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<DateTime<Utc>>, D::Error>
Expand Down Expand Up @@ -760,10 +760,10 @@ pub mod ts_milliseconds {
/// }
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(1526522699, 918000000).unwrap() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(1526522699, 918000000).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(-1, 999_000_000).unwrap() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(-1, 999_000_000).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
Expand Down Expand Up @@ -904,7 +904,7 @@ pub mod ts_milliseconds_option {
/// }
///
/// let my_s: E<S> = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
/// assert_eq!(my_s, E::V(S { time: Some(Utc.timestamp(1526522699, 918000000).unwrap()) }));
/// assert_eq!(my_s, E::V(S { time: Some(Utc.and_timestamp(1526522699, 918000000).unwrap()) }));
/// let s: E<S> = serde_json::from_str(r#"{ "time": null }"#)?;
/// assert_eq!(s, E::V(S { time: None }));
/// let t: E<S> = serde_json::from_str(r#"{}"#)?;
Expand Down Expand Up @@ -1031,7 +1031,7 @@ pub mod ts_seconds {
/// }
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(1431684000, 0).unwrap() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(1431684000, 0).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
Expand Down Expand Up @@ -1152,7 +1152,7 @@ pub mod ts_seconds_option {
/// }
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp(1431684000, 0).single() });
/// assert_eq!(my_s, S { time: Utc.and_timestamp(1431684000, 0).single() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<DateTime<Utc>>, D::Error>
Expand Down
2 changes: 1 addition & 1 deletion src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ fn nano_roundrip() {
i64::MAX,
] {
println!("nanos: {}", nanos);
let dt = Utc.timestamp_nanos(nanos);
let dt = Utc.and_timestamp_nanos(nanos);
let nanos2 = dt.timestamp_nanos().expect("value roundtrips");
assert_eq!(nanos, nanos2);
}
Expand Down
2 changes: 1 addition & 1 deletion src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod tz_info;
/// use chrono::{DateTime, Local, TimeZone};
///
/// let dt1: DateTime<Local> = Local::now();
/// let dt2: DateTime<Local> = Local.timestamp(0, 0).unwrap();
/// let dt2: DateTime<Local> = Local.and_timestamp(0, 0).unwrap();
/// assert!(dt1 >= dt2);
/// ```
#[derive(Copy, Clone, Debug)]
Expand Down
62 changes: 31 additions & 31 deletions src/offset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ pub trait TimeZone: Sized + Clone {
/// ```
/// use chrono::{TimeZone, Utc};
///
/// assert_eq!(Utc.timestamp(1431648000, 0).unwrap().to_string(), "2015-05-15 00:00:00 UTC");
/// assert_eq!(Utc.and_timestamp(1431648000, 0).unwrap().to_string(), "2015-05-15 00:00:00 UTC");
/// ```
fn timestamp(&self, secs: i64, nsecs: u32) -> MappedLocalTime<DateTime<Self>> {
fn and_timestamp(&self, secs: i64, nsecs: u32) -> MappedLocalTime<DateTime<Self>> {
match DateTime::from_timestamp(secs, nsecs) {
Ok(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
Err(_) => MappedLocalTime::None,
Expand All @@ -209,12 +209,12 @@ pub trait TimeZone: Sized + Clone {
///
/// ```
/// use chrono::{MappedLocalTime, TimeZone, Utc};
/// match Utc.timestamp_millis(1431648000) {
/// match Utc.and_timestamp_millis(1431648000) {
/// MappedLocalTime::Single(dt) => assert_eq!(dt.timestamp(), 1431648),
/// _ => panic!("Incorrect timestamp_millis"),
/// };
/// ```
fn timestamp_millis(&self, millis: i64) -> MappedLocalTime<DateTime<Self>> {
fn and_timestamp_millis(&self, millis: i64) -> MappedLocalTime<DateTime<Self>> {
match DateTime::from_timestamp_millis(millis) {
Ok(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
Err(_) => MappedLocalTime::None,
Expand All @@ -224,16 +224,16 @@ pub trait TimeZone: Sized + Clone {
/// Makes a new `DateTime` from the number of non-leap nanoseconds
/// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").
///
/// Unlike [`timestamp_millis`](#method.timestamp_millis), this never fails.
/// Unlike [`and_timestamp_millis`](#method.and_timestamp_millis), this never fails.
///
/// # Example
///
/// ```
/// use chrono::{TimeZone, Utc};
///
/// assert_eq!(Utc.timestamp_nanos(1431648000000000).timestamp(), 1431648);
/// assert_eq!(Utc.and_timestamp_nanos(1431648000000000).timestamp(), 1431648);
/// ```
fn timestamp_nanos(&self, nanos: i64) -> DateTime<Self> {
fn and_timestamp_nanos(&self, nanos: i64) -> DateTime<Self> {
self.from_utc_datetime(&DateTime::from_timestamp_nanos(nanos).naive_utc())
}

Expand All @@ -245,9 +245,9 @@ pub trait TimeZone: Sized + Clone {
/// ```
/// use chrono::{TimeZone, Utc};
///
/// assert_eq!(Utc.timestamp_micros(1431648000000).unwrap().timestamp(), 1431648);
/// assert_eq!(Utc.and_timestamp_micros(1431648000000).unwrap().timestamp(), 1431648);
/// ```
fn timestamp_micros(&self, micros: i64) -> MappedLocalTime<DateTime<Self>> {
fn and_timestamp_micros(&self, micros: i64) -> MappedLocalTime<DateTime<Self>> {
match DateTime::from_timestamp_micros(micros) {
Ok(dt) => MappedLocalTime::Single(self.from_utc_datetime(&dt.naive_utc())),
Err(_) => MappedLocalTime::None,
Expand Down Expand Up @@ -326,29 +326,29 @@ mod tests {

#[test]
fn test_negative_millis() {
let dt = Utc.timestamp_millis(-1000).unwrap();
let dt = Utc.and_timestamp_millis(-1000).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
let dt = Utc.timestamp_millis(-7000).unwrap();
let dt = Utc.and_timestamp_millis(-7000).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:53 UTC");
let dt = Utc.timestamp_millis(-7001).unwrap();
let dt = Utc.and_timestamp_millis(-7001).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:52.999 UTC");
let dt = Utc.timestamp_millis(-7003).unwrap();
let dt = Utc.and_timestamp_millis(-7003).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:52.997 UTC");
let dt = Utc.timestamp_millis(-999).unwrap();
let dt = Utc.and_timestamp_millis(-999).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:59.001 UTC");
let dt = Utc.timestamp_millis(-1).unwrap();
let dt = Utc.and_timestamp_millis(-1).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999 UTC");
let dt = Utc.timestamp_millis(-60000).unwrap();
let dt = Utc.and_timestamp_millis(-60000).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
let dt = Utc.timestamp_millis(-3600000).unwrap();
let dt = Utc.and_timestamp_millis(-3600000).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");

for (millis, expected) in &[
(-7000, "1969-12-31 23:59:53 UTC"),
(-7001, "1969-12-31 23:59:52.999 UTC"),
(-7003, "1969-12-31 23:59:52.997 UTC"),
] {
match Utc.timestamp_millis(*millis) {
match Utc.and_timestamp_millis(*millis) {
MappedLocalTime::Single(dt) => {
assert_eq!(dt.to_string(), *expected);
}
Expand All @@ -359,36 +359,36 @@ mod tests {

#[test]
fn test_negative_nanos() {
let dt = Utc.timestamp_nanos(-1_000_000_000);
let dt = Utc.and_timestamp_nanos(-1_000_000_000);
assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
let dt = Utc.timestamp_nanos(-999_999_999);
let dt = Utc.and_timestamp_nanos(-999_999_999);
assert_eq!(dt.to_string(), "1969-12-31 23:59:59.000000001 UTC");
let dt = Utc.timestamp_nanos(-1);
let dt = Utc.and_timestamp_nanos(-1);
assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999999999 UTC");
let dt = Utc.timestamp_nanos(-60_000_000_000);
let dt = Utc.and_timestamp_nanos(-60_000_000_000);
assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
let dt = Utc.timestamp_nanos(-3_600_000_000_000);
let dt = Utc.and_timestamp_nanos(-3_600_000_000_000);
assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
}

#[test]
fn test_nanos_never_panics() {
Utc.timestamp_nanos(i64::max_value());
Utc.timestamp_nanos(i64::default());
Utc.timestamp_nanos(i64::min_value());
Utc.and_timestamp_nanos(i64::max_value());
Utc.and_timestamp_nanos(i64::default());
Utc.and_timestamp_nanos(i64::min_value());
}

#[test]
fn test_negative_micros() {
let dt = Utc.timestamp_micros(-1_000_000).unwrap();
let dt = Utc.and_timestamp_micros(-1_000_000).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:59 UTC");
let dt = Utc.timestamp_micros(-999_999).unwrap();
let dt = Utc.and_timestamp_micros(-999_999).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:59.000001 UTC");
let dt = Utc.timestamp_micros(-1).unwrap();
let dt = Utc.and_timestamp_micros(-1).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:59.999999 UTC");
let dt = Utc.timestamp_micros(-60_000_000).unwrap();
let dt = Utc.and_timestamp_micros(-60_000_000).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:59:00 UTC");
let dt = Utc.timestamp_micros(-3_600_000_000).unwrap();
let dt = Utc.and_timestamp_micros(-3_600_000_000).unwrap();
assert_eq!(dt.to_string(), "1969-12-31 23:00:00 UTC");
}
}
2 changes: 1 addition & 1 deletion src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::OutOfRange;
///
/// let dt = DateTime::from_timestamp(61, 0).unwrap();
///
/// assert_eq!(Utc.timestamp(61, 0).unwrap(), dt);
/// assert_eq!(Utc.and_timestamp(61, 0).unwrap(), dt);
/// assert_eq!(Utc.with_ymd_and_hms(1970, 1, 1, 0, 1, 1).unwrap(), dt);
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
Expand Down

0 comments on commit 447dd5a

Please sign in to comment.