1
- use std:: borrow:: Cow ;
2
1
use std:: convert:: TryFrom ;
3
2
4
3
use byteorder:: { ByteOrder , LittleEndian } ;
5
4
use bytes:: Buf ;
5
+ use time:: macros:: format_description;
6
6
use time:: { Date , OffsetDateTime , PrimitiveDateTime , Time , UtcOffset } ;
7
7
8
8
use crate :: decode:: Decode ;
@@ -87,7 +87,7 @@ impl<'r> Decode<'r, MySql> for Time {
87
87
// are 0 then the length is 0 and no further data is send
88
88
// https://dev.mysql.com/doc/internals/en/binary-protocol-value.html
89
89
if len == 0 {
90
- return Ok ( Time :: try_from_hms_micro ( 0 , 0 , 0 , 0 ) . unwrap ( ) ) ;
90
+ return Ok ( Time :: MIDNIGHT ) ;
91
91
}
92
92
93
93
// is negative : int<1>
@@ -101,21 +101,11 @@ impl<'r> Decode<'r, MySql> for Time {
101
101
decode_time ( len - 5 , buf)
102
102
}
103
103
104
- MySqlValueFormat :: Text => {
105
- let s = value. as_str ( ) ?;
106
-
107
- // If there are less than 9 digits after the decimal point
108
- // We need to zero-pad
109
- // TODO: Ask [time] to add a parse % for less-than-fixed-9 nanos
110
-
111
- let s = if s. len ( ) < 20 {
112
- Cow :: Owned ( format ! ( "{:0<19}" , s) )
113
- } else {
114
- Cow :: Borrowed ( s)
115
- } ;
116
-
117
- Time :: parse ( & * s, "%H:%M:%S.%N" ) . map_err ( Into :: into)
118
- }
104
+ MySqlValueFormat :: Text => Time :: parse (
105
+ value. as_str ( ) ?,
106
+ & format_description ! ( "[hour]:[minute]:[second].[subsecond]" ) ,
107
+ )
108
+ . map_err ( Into :: into) ,
119
109
}
120
110
}
121
111
}
@@ -148,7 +138,7 @@ impl<'r> Decode<'r, MySql> for Date {
148
138
}
149
139
MySqlValueFormat :: Text => {
150
140
let s = value. as_str ( ) ?;
151
- Date :: parse ( s, "%Y-%m-%d" ) . map_err ( Into :: into)
141
+ Date :: parse ( s, & format_description ! ( "[year]-[month]-[day]" ) ) . map_err ( Into :: into)
152
142
}
153
143
}
154
144
}
@@ -211,21 +201,22 @@ impl<'r> Decode<'r, MySql> for PrimitiveDateTime {
211
201
MySqlValueFormat :: Text => {
212
202
let s = value. as_str ( ) ?;
213
203
214
- // If there are less than 9 digits after the decimal point
215
- // We need to zero-pad
216
- // TODO: Ask [time] to add a parse % for less-than-fixed-9 nanos
217
-
218
- let s = if s. len ( ) < 31 {
219
- if s. contains ( '.' ) {
220
- Cow :: Owned ( format ! ( "{:0<30}" , s) )
221
- } else {
222
- Cow :: Owned ( format ! ( "{}.000000000" , s) )
223
- }
204
+ // If there are no nanoseconds parse without them
205
+ if s. contains ( '.' ) {
206
+ PrimitiveDateTime :: parse (
207
+ s,
208
+ & format_description ! (
209
+ "[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"
210
+ ) ,
211
+ )
212
+ . map_err ( Into :: into)
224
213
} else {
225
- Cow :: Borrowed ( s)
226
- } ;
227
-
228
- PrimitiveDateTime :: parse ( & * s, "%Y-%m-%d %H:%M:%S.%N" ) . map_err ( Into :: into)
214
+ PrimitiveDateTime :: parse (
215
+ s,
216
+ & format_description ! ( "[year]-[month]-[day] [hour]:[minute]:[second]" ) ,
217
+ )
218
+ . map_err ( Into :: into)
219
+ }
229
220
}
230
221
}
231
222
}
@@ -237,7 +228,7 @@ fn encode_date(date: &Date, buf: &mut Vec<u8>) {
237
228
. unwrap_or_else ( |_| panic ! ( "Date out of range for Mysql: {}" , date) ) ;
238
229
239
230
buf. extend_from_slice ( & year. to_le_bytes ( ) ) ;
240
- buf. push ( date. month ( ) ) ;
231
+ buf. push ( date. month ( ) . into ( ) ) ;
241
232
buf. push ( date. day ( ) ) ;
242
233
}
243
234
@@ -247,9 +238,9 @@ fn decode_date(buf: &[u8]) -> Result<Option<Date>, BoxDynError> {
247
238
return Ok ( None ) ;
248
239
}
249
240
250
- Date :: try_from_ymd (
241
+ Date :: from_calendar_date (
251
242
LittleEndian :: read_u16 ( buf) as i32 ,
252
- buf[ 2 ] as u8 ,
243
+ time :: Month :: try_from ( buf[ 2 ] as u8 ) ? ,
253
244
buf[ 3 ] as u8 ,
254
245
)
255
246
. map_err ( Into :: into)
@@ -278,6 +269,6 @@ fn decode_time(len: u8, mut buf: &[u8]) -> Result<Time, BoxDynError> {
278
269
0
279
270
} ;
280
271
281
- Time :: try_from_hms_micro ( hour, minute, seconds, micros as u32 )
272
+ Time :: from_hms_micro ( hour, minute, seconds, micros as u32 )
282
273
. map_err ( |e| format ! ( "Time out of range for MySQL: {}" , e) . into ( ) )
283
274
}
0 commit comments