File tree 4 files changed +22
-2
lines changed
4 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -298,7 +298,13 @@ impl OffsetDateTime {
298
298
/// );
299
299
/// ```
300
300
pub fn unix_timestamp ( self ) -> i64 {
301
- ( self - Self :: unix_epoch ( ) ) . whole_seconds ( )
301
+ let days = ( self . utc_datetime . date . julian_day ( )
302
+ - internals:: Date :: from_yo_unchecked ( 1970 , 1 ) . julian_day ( ) )
303
+ * 86_400 ;
304
+ let hours = self . utc_datetime . hour ( ) as i64 * 3_600 ;
305
+ let minutes = self . utc_datetime . minute ( ) as i64 * 60 ;
306
+ let seconds = self . utc_datetime . second ( ) as i64 ;
307
+ days + hours + minutes + seconds
302
308
}
303
309
304
310
/// Get the [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time).
Original file line number Diff line number Diff line change @@ -132,7 +132,13 @@ impl PrimitiveDateTime {
132
132
#[ allow( deprecated) ]
133
133
#[ deprecated( since = "0.2.7" , note = "This method assumes an offset of UTC." ) ]
134
134
pub fn timestamp ( self ) -> i64 {
135
- ( self - Self :: unix_epoch ( ) ) . whole_seconds ( )
135
+ let days = ( self . date . julian_day ( )
136
+ - internals:: Date :: from_yo_unchecked ( 1970 , 1 ) . julian_day ( ) )
137
+ * 86_400 ;
138
+ let hours = self . hour ( ) as i64 * 3_600 ;
139
+ let minutes = self . minute ( ) as i64 * 60 ;
140
+ let seconds = self . second ( ) as i64 ;
141
+ days + hours + minutes + seconds
136
142
}
137
143
138
144
/// Get the `Date` component of the `PrimitiveDateTime`.
Original file line number Diff line number Diff line change @@ -123,6 +123,10 @@ fn timestamp() {
123
123
. timestamp( ) ,
124
124
3_600 ,
125
125
) ;
126
+ assert_eq ! (
127
+ ( OffsetDateTime :: unix_epoch( ) - 1 . nanoseconds( ) ) . timestamp( ) ,
128
+ -1
129
+ ) ;
126
130
}
127
131
128
132
#[ test]
Original file line number Diff line number Diff line change @@ -45,6 +45,10 @@ fn from_unix_timestamp() {
45
45
fn timestamp ( ) {
46
46
assert_eq ! ( PrimitiveDateTime :: unix_epoch( ) . timestamp( ) , 0 ) ;
47
47
assert_eq ! ( date!( 2019 - 01 - 01 ) . midnight( ) . timestamp( ) , 1_546_300_800 ) ;
48
+ assert_eq ! (
49
+ ( PrimitiveDateTime :: unix_epoch( ) - 1 . nanoseconds( ) ) . timestamp( ) ,
50
+ -1
51
+ ) ;
48
52
}
49
53
50
54
#[ test]
You can’t perform that action at this time.
0 commit comments