@@ -105,16 +105,28 @@ impl DateTimeBuilder {
105
105
106
106
pub ( super ) fn build ( self ) -> Option < DateTime < FixedOffset > > {
107
107
let base = self . base . unwrap_or_else ( || chrono:: Local :: now ( ) . into ( ) ) ;
108
- let mut dt = new_date (
109
- base. year ( ) ,
110
- base. month ( ) ,
111
- base. day ( ) ,
112
- 0 ,
113
- 0 ,
114
- 0 ,
115
- 0 ,
116
- * base. offset ( ) ,
117
- ) ?;
108
+
109
+ // If any of the following items are set, we truncate the time portion
110
+ // of the base date to zero; otherwise, we use the base date as is.
111
+ let mut dt = if self . timestamp . is_none ( )
112
+ && self . date . is_none ( )
113
+ && self . time . is_none ( )
114
+ && self . weekday . is_none ( )
115
+ && self . timezone . is_none ( )
116
+ {
117
+ base
118
+ } else {
119
+ new_date (
120
+ base. year ( ) ,
121
+ base. month ( ) ,
122
+ base. day ( ) ,
123
+ 0 ,
124
+ 0 ,
125
+ 0 ,
126
+ 0 ,
127
+ * base. offset ( ) ,
128
+ ) ?
129
+ } ;
118
130
119
131
if let Some ( ts) = self . timestamp {
120
132
// TODO: How to make the fract -> nanosecond conversion more precise?
@@ -221,14 +233,6 @@ impl DateTimeBuilder {
221
233
}
222
234
223
235
for rel in self . relative {
224
- if self . timestamp . is_none ( )
225
- && self . date . is_none ( )
226
- && self . time . is_none ( )
227
- && self . weekday . is_none ( )
228
- {
229
- dt = base;
230
- }
231
-
232
236
match rel {
233
237
relative:: Relative :: Years ( x) => {
234
238
dt = dt. with_year ( dt. year ( ) + x) ?;
@@ -254,7 +258,7 @@ impl DateTimeBuilder {
254
258
relative:: Relative :: Minutes ( x) => {
255
259
dt += chrono:: Duration :: try_minutes ( x. into ( ) ) ?;
256
260
}
257
- // Seconds are special because they can be given as a float
261
+ // Seconds are special because they can be given as a float.
258
262
relative:: Relative :: Seconds ( x) => {
259
263
dt += chrono:: Duration :: try_seconds ( x as i64 ) ?;
260
264
}
0 commit comments