mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-02 15:26:12 +00:00
Move implementation of from_timestamp*
to DateTime
This commit is contained in:
parent
0c23fd4df5
commit
27a1ee4cdc
@ -201,7 +201,6 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn timestamp(&self) -> i64 {
|
||||
const UNIX_EPOCH_DAY: i64 = 719_163;
|
||||
let gregorian_day = self.datetime.date().num_days_from_ce() as i64;
|
||||
let seconds_from_midnight = self.datetime.time().num_seconds_from_midnight() as i64;
|
||||
(gregorian_day - UNIX_EPOCH_DAY) * 86_400 + seconds_from_midnight
|
||||
@ -692,7 +691,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
|
||||
}
|
||||
|
||||
impl DateTime<Utc> {
|
||||
/// Makes a new [`DateTime<Utc>`] from the number of non-leap seconds
|
||||
/// Makes a new `DateTime<Utc>` from the number of non-leap seconds
|
||||
/// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
|
||||
/// and the number of nanoseconds since the last whole non-leap second.
|
||||
///
|
||||
@ -714,10 +713,9 @@ impl DateTime<Utc> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{DateTime, Utc};
|
||||
/// use chrono::DateTime;
|
||||
///
|
||||
/// let dt: DateTime<Utc> =
|
||||
/// DateTime::<Utc>::from_timestamp(1431648000, 0).expect("invalid timestamp");
|
||||
/// let dt = DateTime::from_timestamp(1431648000, 0).expect("invalid timestamp");
|
||||
///
|
||||
/// assert_eq!(dt.to_string(), "2015-05-15 00:00:00 UTC");
|
||||
/// assert_eq!(DateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_nanos()).unwrap(), dt);
|
||||
@ -725,16 +723,20 @@ impl DateTime<Utc> {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn from_timestamp(secs: i64, nsecs: u32) -> Option<Self> {
|
||||
Some(DateTime {
|
||||
datetime: try_opt!(NaiveDateTime::from_timestamp_opt(secs, nsecs)),
|
||||
offset: Utc,
|
||||
})
|
||||
let days = secs.div_euclid(86_400) + UNIX_EPOCH_DAY;
|
||||
let secs = secs.rem_euclid(86_400);
|
||||
if days < i32::MIN as i64 || days > i32::MAX as i64 {
|
||||
return None;
|
||||
}
|
||||
let date = try_opt!(NaiveDate::from_num_days_from_ce_opt(days as i32));
|
||||
let time = try_opt!(NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs));
|
||||
Some(date.and_time(time).and_utc())
|
||||
}
|
||||
|
||||
/// Makes a new [`DateTime<Utc>`] from the number of non-leap milliseconds
|
||||
/// Makes a new `DateTime<Utc>` from the number of non-leap milliseconds
|
||||
/// since January 1, 1970 0:00:00.000 UTC (aka "UNIX timestamp").
|
||||
///
|
||||
/// This is guaranteed to round-trip with regard to [`timestamp_millis`](DateTime::timestamp_millis).
|
||||
/// 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_opt`] or [`DateTime::with_timezone`].
|
||||
@ -746,10 +748,9 @@ impl DateTime<Utc> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use chrono::{DateTime, Utc};
|
||||
/// use chrono::DateTime;
|
||||
///
|
||||
/// let dt: DateTime<Utc> =
|
||||
/// DateTime::<Utc>::from_timestamp_millis(947638923004).expect("invalid timestamp");
|
||||
/// let dt = DateTime::from_timestamp_millis(947638923004).expect("invalid timestamp");
|
||||
///
|
||||
/// assert_eq!(dt.to_string(), "2000-01-12 01:02:03.004 UTC");
|
||||
/// assert_eq!(DateTime::from_timestamp_millis(dt.timestamp_millis()).unwrap(), dt);
|
||||
@ -757,7 +758,9 @@ impl DateTime<Utc> {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn from_timestamp_millis(millis: i64) -> Option<Self> {
|
||||
Some(try_opt!(NaiveDateTime::from_timestamp_millis(millis)).and_utc())
|
||||
let secs = millis.div_euclid(1000);
|
||||
let nsecs = millis.rem_euclid(1000) as u32 * 1_000_000;
|
||||
Self::from_timestamp(secs, nsecs)
|
||||
}
|
||||
|
||||
/// Creates a new `DateTime<Utc>` from the number of non-leap microseconds
|
||||
@ -1900,6 +1903,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
const UNIX_EPOCH_DAY: i64 = 719_163;
|
||||
|
||||
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
|
||||
fn test_encodable_json<FUtc, FFixed, E>(to_string_utc: FUtc, to_string_fixed: FFixed)
|
||||
where
|
||||
|
@ -130,8 +130,9 @@ impl NaiveDateTime {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn from_timestamp(secs: i64, nsecs: u32) -> NaiveDateTime {
|
||||
let datetime = NaiveDateTime::from_timestamp_opt(secs, nsecs);
|
||||
expect!(datetime, "invalid or out-of-range datetime")
|
||||
let datetime =
|
||||
expect!(DateTime::from_timestamp(secs, nsecs), "invalid or out-of-range datetime");
|
||||
datetime.naive_utc()
|
||||
}
|
||||
|
||||
/// Creates a new [NaiveDateTime] from milliseconds since the UNIX epoch.
|
||||
@ -161,9 +162,7 @@ impl NaiveDateTime {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn from_timestamp_millis(millis: i64) -> Option<NaiveDateTime> {
|
||||
let secs = millis.div_euclid(1000);
|
||||
let nsecs = millis.rem_euclid(1000) as u32 * 1_000_000;
|
||||
NaiveDateTime::from_timestamp_opt(secs, nsecs)
|
||||
Some(try_opt!(DateTime::from_timestamp_millis(millis)).naive_utc())
|
||||
}
|
||||
|
||||
/// Creates a new [NaiveDateTime] from microseconds since the UNIX epoch.
|
||||
@ -227,8 +226,7 @@ impl NaiveDateTime {
|
||||
pub const fn from_timestamp_nanos(nanos: i64) -> Option<NaiveDateTime> {
|
||||
let secs = nanos.div_euclid(NANOS_PER_SEC as i64);
|
||||
let nsecs = nanos.rem_euclid(NANOS_PER_SEC as i64) as u32;
|
||||
|
||||
NaiveDateTime::from_timestamp_opt(secs, nsecs)
|
||||
Some(try_opt!(DateTime::from_timestamp(secs, nsecs)).naive_utc())
|
||||
}
|
||||
|
||||
/// Makes a new `NaiveDateTime` corresponding to a UTC date and time,
|
||||
@ -264,18 +262,7 @@ impl NaiveDateTime {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
|
||||
let days = secs.div_euclid(86_400);
|
||||
let secs = secs.rem_euclid(86_400);
|
||||
if days < i32::MIN as i64 || days > i32::MAX as i64 {
|
||||
return None;
|
||||
}
|
||||
let date =
|
||||
NaiveDate::from_num_days_from_ce_opt(try_opt!((days as i32).checked_add(719_163)));
|
||||
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs);
|
||||
match (date, time) {
|
||||
(Some(date), Some(time)) => Some(NaiveDateTime { date, time }),
|
||||
(_, _) => None,
|
||||
}
|
||||
Some(try_opt!(DateTime::from_timestamp(secs, nsecs)).naive_utc())
|
||||
}
|
||||
|
||||
/// Parses a string with the specified format string and returns a new `NaiveDateTime`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user