Skip to content

Commit 37fe575

Browse files
committed
Simplify from_timestamp_millis, from_timestamp_micros
1 parent 84bbea5 commit 37fe575

File tree

1 file changed

+6
-65
lines changed

1 file changed

+6
-65
lines changed

src/naive/datetime/mod.rs

+6-65
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
#[cfg(any(feature = "alloc", feature = "std", test))]
77
use core::borrow::Borrow;
8-
use core::convert::TryFrom;
98
use core::fmt::Write;
109
use core::ops::{Add, AddAssign, Sub, SubAssign};
1110
use core::{fmt, str};
@@ -22,7 +21,6 @@ use crate::format::{Fixed, Item, Numeric, Pad};
2221
use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime};
2322
use crate::oldtime::Duration as OldDuration;
2423
use crate::{DateTime, Datelike, LocalResult, Months, TimeZone, Timelike, Weekday};
25-
use core::cmp::Ordering;
2624

2725
#[cfg(feature = "rustc-serialize")]
2826
pub(super) mod rustc_serialize;
@@ -42,11 +40,6 @@ mod tests;
4240
/// touching that call when we are already sure that it WILL overflow...
4341
const MAX_SECS_BITS: usize = 44;
4442

45-
/// Number of nanoseconds in a millisecond
46-
const NANOS_IN_MILLISECOND: u32 = 1_000_000;
47-
/// Number of nanoseconds in a second
48-
const NANOS_IN_SECOND: u32 = 1000 * NANOS_IN_MILLISECOND;
49-
5043
/// The minimum possible `NaiveDateTime`.
5144
#[deprecated(since = "0.4.20", note = "Use NaiveDateTime::MIN instead")]
5245
pub const MIN_DATETIME: NaiveDateTime = NaiveDateTime::MIN;
@@ -87,32 +80,6 @@ pub struct NaiveDateTime {
8780
time: NaiveTime,
8881
}
8982

90-
/// The unit of a timestamp expressed in fractions of a second.
91-
/// Currently either milliseconds or microseconds.
92-
///
93-
/// This is a private type, used in the implementation of
94-
/// [NaiveDateTime::from_timestamp_millis] and [NaiveDateTime::from_timestamp_micros].
95-
#[derive(Clone, Copy, Debug)]
96-
enum TimestampUnit {
97-
Millis,
98-
Micros,
99-
}
100-
101-
impl TimestampUnit {
102-
fn per_second(self) -> u32 {
103-
match self {
104-
TimestampUnit::Millis => 1_000,
105-
TimestampUnit::Micros => 1_000_000,
106-
}
107-
}
108-
fn nanos_per(self) -> u32 {
109-
match self {
110-
TimestampUnit::Millis => 1_000_000,
111-
TimestampUnit::Micros => 1_000,
112-
}
113-
}
114-
}
115-
11683
impl NaiveDateTime {
11784
/// Makes a new `NaiveDateTime` from date and time components.
11885
/// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time)
@@ -180,7 +147,9 @@ impl NaiveDateTime {
180147
#[inline]
181148
#[must_use]
182149
pub fn from_timestamp_millis(millis: i64) -> Option<NaiveDateTime> {
183-
Self::from_timestamp_unit(millis, TimestampUnit::Millis)
150+
let secs = millis.div_euclid(1000);
151+
let nsecs = millis.rem_euclid(1000) as u32 * 1000_000;
152+
NaiveDateTime::from_timestamp_opt(secs, nsecs)
184153
}
185154

186155
/// Creates a new [NaiveDateTime] from microseconds since the UNIX epoch.
@@ -207,7 +176,9 @@ impl NaiveDateTime {
207176
#[inline]
208177
#[must_use]
209178
pub fn from_timestamp_micros(micros: i64) -> Option<NaiveDateTime> {
210-
Self::from_timestamp_unit(micros, TimestampUnit::Micros)
179+
let secs = micros.div_euclid(1000_000);
180+
let nsecs = micros.rem_euclid(1000_000) as u32 * 1000;
181+
NaiveDateTime::from_timestamp_opt(secs, nsecs)
211182
}
212183

213184
/// Makes a new `NaiveDateTime` corresponding to a UTC date and time,
@@ -950,36 +921,6 @@ impl NaiveDateTime {
950921
pub const MIN: Self = Self { date: NaiveDate::MIN, time: NaiveTime::MIN };
951922
/// The maximum possible `NaiveDateTime`.
952923
pub const MAX: Self = Self { date: NaiveDate::MAX, time: NaiveTime::MAX };
953-
954-
/// Creates a new [NaiveDateTime] from milliseconds or microseconds since the UNIX epoch.
955-
///
956-
/// This is a private function used by [from_timestamp_millis] and [from_timestamp_micros].
957-
#[inline]
958-
fn from_timestamp_unit(value: i64, unit: TimestampUnit) -> Option<NaiveDateTime> {
959-
let (secs, subsecs) =
960-
(value / i64::from(unit.per_second()), value % i64::from(unit.per_second()));
961-
962-
match subsecs.cmp(&0) {
963-
Ordering::Less => {
964-
// in the case where our subsec part is negative, then we are actually in the earlier second
965-
// hence we subtract one from the seconds part, and we then add a whole second worth of nanos
966-
// to our nanos part. Due to the use of u32 datatype, it is more convenient to subtract
967-
// the absolute value of the subsec nanos from a whole second worth of nanos
968-
let nsecs = u32::try_from(subsecs.abs()).ok()? * unit.nanos_per();
969-
NaiveDateTime::from_timestamp_opt(
970-
secs.checked_sub(1)?,
971-
NANOS_IN_SECOND.checked_sub(nsecs)?,
972-
)
973-
}
974-
Ordering::Equal => NaiveDateTime::from_timestamp_opt(secs, 0),
975-
Ordering::Greater => {
976-
// convert the subsec millis into nanosecond scale so they can be supplied
977-
// as the nanoseconds parameter
978-
let nsecs = u32::try_from(subsecs).ok()? * unit.nanos_per();
979-
NaiveDateTime::from_timestamp_opt(secs, nsecs)
980-
}
981-
}
982-
}
983924
}
984925

985926
impl Datelike for NaiveDateTime {

0 commit comments

Comments
 (0)