5
5
6
6
#[ cfg( any( feature = "alloc" , feature = "std" , test) ) ]
7
7
use core:: borrow:: Borrow ;
8
- use core:: convert:: TryFrom ;
9
8
use core:: fmt:: Write ;
10
9
use core:: ops:: { Add , AddAssign , Sub , SubAssign } ;
11
10
use core:: { fmt, str} ;
@@ -22,7 +21,6 @@ use crate::format::{Fixed, Item, Numeric, Pad};
22
21
use crate :: naive:: { Days , IsoWeek , NaiveDate , NaiveTime } ;
23
22
use crate :: oldtime:: Duration as OldDuration ;
24
23
use crate :: { DateTime , Datelike , LocalResult , Months , TimeZone , Timelike , Weekday } ;
25
- use core:: cmp:: Ordering ;
26
24
27
25
#[ cfg( feature = "rustc-serialize" ) ]
28
26
pub ( super ) mod rustc_serialize;
@@ -42,11 +40,6 @@ mod tests;
42
40
/// touching that call when we are already sure that it WILL overflow...
43
41
const MAX_SECS_BITS : usize = 44 ;
44
42
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
-
50
43
/// The minimum possible `NaiveDateTime`.
51
44
#[ deprecated( since = "0.4.20" , note = "Use NaiveDateTime::MIN instead" ) ]
52
45
pub const MIN_DATETIME : NaiveDateTime = NaiveDateTime :: MIN ;
@@ -87,32 +80,6 @@ pub struct NaiveDateTime {
87
80
time : NaiveTime ,
88
81
}
89
82
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
-
116
83
impl NaiveDateTime {
117
84
/// Makes a new `NaiveDateTime` from date and time components.
118
85
/// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time)
@@ -180,7 +147,9 @@ impl NaiveDateTime {
180
147
#[ inline]
181
148
#[ must_use]
182
149
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)
184
153
}
185
154
186
155
/// Creates a new [NaiveDateTime] from microseconds since the UNIX epoch.
@@ -207,7 +176,9 @@ impl NaiveDateTime {
207
176
#[ inline]
208
177
#[ must_use]
209
178
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)
211
182
}
212
183
213
184
/// Makes a new `NaiveDateTime` corresponding to a UTC date and time,
@@ -950,36 +921,6 @@ impl NaiveDateTime {
950
921
pub const MIN : Self = Self { date : NaiveDate :: MIN , time : NaiveTime :: MIN } ;
951
922
/// The maximum possible `NaiveDateTime`.
952
923
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
- }
983
924
}
984
925
985
926
impl Datelike for NaiveDateTime {
0 commit comments