@@ -115,60 +115,48 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
115
115
fn format_numeric ( & self , w : & mut impl Write , spec : & Numeric , pad : & Pad ) -> fmt:: Result {
116
116
use self :: Numeric :: * ;
117
117
118
- let ( width, v) = match * spec {
119
- Year => ( 4 , self . date . map ( |d| i64:: from ( d. year ( ) ) ) ) ,
120
- YearDiv100 => ( 2 , self . date . map ( |d| i64:: from ( d. year ( ) ) . div_euclid ( 100 ) ) ) ,
121
- YearMod100 => ( 2 , self . date . map ( |d| i64:: from ( d. year ( ) ) . rem_euclid ( 100 ) ) ) ,
122
- IsoYear => ( 4 , self . date . map ( |d| i64:: from ( d. iso_week ( ) . year ( ) ) ) ) ,
123
- IsoYearDiv100 => ( 2 , self . date . map ( |d| i64:: from ( d. iso_week ( ) . year ( ) ) . div_euclid ( 100 ) ) ) ,
124
- IsoYearMod100 => ( 2 , self . date . map ( |d| i64:: from ( d. iso_week ( ) . year ( ) ) . rem_euclid ( 100 ) ) ) ,
125
- Month => ( 2 , self . date . map ( |d| i64:: from ( d. month ( ) ) ) ) ,
126
- Day => ( 2 , self . date . map ( |d| i64:: from ( d. day ( ) ) ) ) ,
127
- WeekFromSun => ( 2 , self . date . map ( |d| i64:: from ( d. weeks_from ( Weekday :: Sun ) ) ) ) ,
128
- WeekFromMon => ( 2 , self . date . map ( |d| i64:: from ( d. weeks_from ( Weekday :: Mon ) ) ) ) ,
129
- IsoWeek => ( 2 , self . date . map ( |d| i64:: from ( d. iso_week ( ) . week ( ) ) ) ) ,
130
- NumDaysFromSun => ( 1 , self . date . map ( |d| i64:: from ( d. weekday ( ) . num_days_from_sunday ( ) ) ) ) ,
131
- WeekdayFromMon => ( 1 , self . date . map ( |d| i64:: from ( d. weekday ( ) . number_from_monday ( ) ) ) ) ,
132
- Ordinal => ( 3 , self . date . map ( |d| i64:: from ( d. ordinal ( ) ) ) ) ,
133
- Hour => ( 2 , self . time . map ( |t| i64:: from ( t. hour ( ) ) ) ) ,
134
- Hour12 => ( 2 , self . time . map ( |t| i64:: from ( t. hour12 ( ) . 1 ) ) ) ,
135
- Minute => ( 2 , self . time . map ( |t| i64:: from ( t. minute ( ) ) ) ) ,
136
- Second => {
137
- ( 2 , self . time . map ( |t| i64:: from ( t. second ( ) + t. nanosecond ( ) / 1_000_000_000 ) ) )
118
+ let ( width, v) = match ( spec, self . date , self . time ) {
119
+ ( Year , Some ( d) , _) => ( 4 , i64:: from ( d. year ( ) ) ) ,
120
+ ( YearDiv100 , Some ( d) , _) => ( 2 , i64:: from ( d. year ( ) ) . div_euclid ( 100 ) ) ,
121
+ ( YearMod100 , Some ( d) , _) => ( 2 , i64:: from ( d. year ( ) ) . rem_euclid ( 100 ) ) ,
122
+ ( IsoYear , Some ( d) , _) => ( 4 , i64:: from ( d. iso_week ( ) . year ( ) ) ) ,
123
+ ( IsoYearDiv100 , Some ( d) , _) => ( 2 , i64:: from ( d. iso_week ( ) . year ( ) ) . div_euclid ( 100 ) ) ,
124
+ ( IsoYearMod100 , Some ( d) , _) => ( 2 , i64:: from ( d. iso_week ( ) . year ( ) ) . rem_euclid ( 100 ) ) ,
125
+ ( Month , Some ( d) , _) => ( 2 , i64:: from ( d. month ( ) ) ) ,
126
+ ( Day , Some ( d) , _) => ( 2 , i64:: from ( d. day ( ) ) ) ,
127
+ ( WeekFromSun , Some ( d) , _) => ( 2 , i64:: from ( d. weeks_from ( Weekday :: Sun ) ) ) ,
128
+ ( WeekFromMon , Some ( d) , _) => ( 2 , i64:: from ( d. weeks_from ( Weekday :: Mon ) ) ) ,
129
+ ( IsoWeek , Some ( d) , _) => ( 2 , i64:: from ( d. iso_week ( ) . week ( ) ) ) ,
130
+ ( NumDaysFromSun , Some ( d) , _) => ( 1 , i64:: from ( d. weekday ( ) . num_days_from_sunday ( ) ) ) ,
131
+ ( WeekdayFromMon , Some ( d) , _) => ( 1 , i64:: from ( d. weekday ( ) . number_from_monday ( ) ) ) ,
132
+ ( Ordinal , Some ( d) , _) => ( 3 , i64:: from ( d. ordinal ( ) ) ) ,
133
+ ( Hour , _, Some ( t) ) => ( 2 , i64:: from ( t. hour ( ) ) ) ,
134
+ ( Hour12 , _, Some ( t) ) => ( 2 , i64:: from ( t. hour12 ( ) . 1 ) ) ,
135
+ ( Minute , _, Some ( t) ) => ( 2 , i64:: from ( t. minute ( ) ) ) ,
136
+ ( Second , _, Some ( t) ) => ( 2 , i64:: from ( t. second ( ) + t. nanosecond ( ) / 1_000_000_000 ) ) ,
137
+ ( Nanosecond , _, Some ( t) ) => ( 9 , i64:: from ( t. nanosecond ( ) % 1_000_000_000 ) ) ,
138
+ ( Timestamp , Some ( d) , Some ( t) ) => {
139
+ let offset = self . off . as_ref ( ) . map ( |( _, o) | i64:: from ( o. local_minus_utc ( ) ) ) ;
140
+ let timestamp = d. and_time ( t) . and_utc ( ) . timestamp ( ) - offset. unwrap_or ( 0 ) ;
141
+ ( 1 , timestamp)
138
142
}
139
- Nanosecond => ( 9 , self . time . map ( |t| i64:: from ( t. nanosecond ( ) % 1_000_000_000 ) ) ) ,
140
- Timestamp => (
141
- 1 ,
142
- match ( self . date , self . time , self . off . as_ref ( ) ) {
143
- ( Some ( d) , Some ( t) , None ) => Some ( d. and_time ( t) . and_utc ( ) . timestamp ( ) ) ,
144
- ( Some ( d) , Some ( t) , Some ( & ( _, off) ) ) => {
145
- Some ( d. and_time ( t) . and_utc ( ) . timestamp ( ) - i64:: from ( off. local_minus_utc ( ) ) )
146
- }
147
- ( _, _, _) => None ,
148
- } ,
149
- ) ,
150
-
151
- // for the future expansion
152
- Internal ( ref int) => match int. _dummy { } ,
143
+ ( Internal ( _) , _, _) => return Ok ( ( ) ) , // for future expansion
144
+ _ => return Err ( fmt:: Error ) , // insufficient arguments for given format
153
145
} ;
154
146
155
- if let Some ( v) = v {
156
- if ( spec == & Year || spec == & IsoYear ) && !( 0 ..10_000 ) . contains ( & v) {
157
- // non-four-digit years require an explicit sign as per ISO 8601
158
- match * pad {
159
- Pad :: None => write ! ( w, "{:+}" , v) ,
160
- Pad :: Zero => write ! ( w, "{:+01$}" , v, width + 1 ) ,
161
- Pad :: Space => write ! ( w, "{:+1$}" , v, width + 1 ) ,
162
- }
163
- } else {
164
- match * pad {
165
- Pad :: None => write ! ( w, "{}" , v) ,
166
- Pad :: Zero => write ! ( w, "{:01$}" , v, width) ,
167
- Pad :: Space => write ! ( w, "{:1$}" , v, width) ,
168
- }
147
+ if ( spec == & Year || spec == & IsoYear ) && !( 0 ..10_000 ) . contains ( & v) {
148
+ // non-four-digit years require an explicit sign as per ISO 8601
149
+ match pad {
150
+ Pad :: None => write ! ( w, "{:+}" , v) ,
151
+ Pad :: Zero => write ! ( w, "{:+01$}" , v, width + 1 ) ,
152
+ Pad :: Space => write ! ( w, "{:+1$}" , v, width + 1 ) ,
169
153
}
170
154
} else {
171
- Err ( fmt:: Error ) // insufficient arguments for given format
155
+ match pad {
156
+ Pad :: None => write ! ( w, "{}" , v) ,
157
+ Pad :: Zero => write ! ( w, "{:01$}" , v, width) ,
158
+ Pad :: Space => write ! ( w, "{:1$}" , v, width) ,
159
+ }
172
160
}
173
161
}
174
162
0 commit comments