Skip to content

Commit 6fef48f

Browse files
authored
Merge pull request jonhoo#17 from simonhdickson/master
Fix time field length when us=0
2 parents 7bb7c06 + 2e13236 commit 6fef48f

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/value/decode.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ use std::time::Duration;
251251
impl<'a> Into<Duration> for Value<'a> {
252252
fn into(self) -> Duration {
253253
if let ValueInner::Time(mut v) = self.0 {
254-
assert!(v.len() == 8 || v.len() == 12);
254+
assert!(v.len() == 0 || v.len() == 8 || v.len() == 12);
255+
256+
if v.len() == 0 {
257+
return Duration::from_secs(0);
258+
}
255259

256260
let neg = v.read_u8().unwrap();
257261
if neg != 0u8 {
@@ -452,6 +456,12 @@ mod tests {
452456
time::Duration::from_secs(1893),
453457
ColumnType::MYSQL_TYPE_TIME
454458
);
459+
rt!(
460+
dur_zero,
461+
time::Duration,
462+
time::Duration::from_secs(0),
463+
ColumnType::MYSQL_TYPE_TIME
464+
);
455465
rt!(
456466
bytes,
457467
&[u8],

src/value/encode.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,24 @@ impl ToMysqlValue for Duration {
527527

528528
match c.coltype {
529529
ColumnType::MYSQL_TYPE_TIME => {
530-
if us != 0 {
531-
w.write_u8(12u8)?;
530+
if self.as_secs() == 0 && us == 0 {
531+
w.write_u8(0u8)?;
532532
} else {
533-
w.write_u8(9u8)?;
534-
}
533+
if us != 0 {
534+
w.write_u8(12u8)?;
535+
} else {
536+
w.write_u8(8u8)?;
537+
}
535538

536-
w.write_u8(0u8)?; // positive only (for now)
537-
w.write_u32::<LittleEndian>(d as u32)?;
538-
w.write_u8(h as u8)?;
539-
w.write_u8(m as u8)?;
540-
w.write_u8(s as u8)?;
539+
w.write_u8(0u8)?; // positive only (for now)
540+
w.write_u32::<LittleEndian>(d as u32)?;
541+
w.write_u8(h as u8)?;
542+
w.write_u8(m as u8)?;
543+
w.write_u8(s as u8)?;
541544

542-
if us != 0 {
543-
w.write_u32::<LittleEndian>(us)?;
545+
if us != 0 {
546+
w.write_u32::<LittleEndian>(us)?;
547+
}
544548
}
545549
Ok(())
546550
}
@@ -719,6 +723,8 @@ mod tests {
719723
chrono::Utc.ymd(1989, 12, 7).and_hms(8, 0, 4).naive_utc()
720724
);
721725
rt!(dur, time::Duration, time::Duration::from_secs(1893));
726+
rt!(dur_micro, time::Duration, time::Duration::new(1893, 5000));
727+
rt!(dur_zero, time::Duration, time::Duration::from_secs(0));
722728
rt!(bytes, Vec<u8>, vec![0x42, 0x00, 0x1a]);
723729
rt!(string, String, "foobar".to_owned());
724730
}

0 commit comments

Comments
 (0)