Skip to content

Commit 464d3ac

Browse files
committed
Impl 0 time value
1 parent f2ac009 commit 464d3ac

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

src/value/decode.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -251,27 +251,31 @@ 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);
255255

256-
let neg = v.read_u8().unwrap();
257-
if neg != 0u8 {
258-
unimplemented!();
259-
}
260-
261-
let days = u64::from(v.read_u32::<LittleEndian>().unwrap());
262-
let hours = u64::from(v.read_u8().unwrap());
263-
let minutes = u64::from(v.read_u8().unwrap());
264-
let seconds = u64::from(v.read_u8().unwrap());
265-
let micros = if v.len() == 12 {
266-
v.read_u32::<LittleEndian>().unwrap()
256+
if v.len() == 0 {
257+
Duration::from_secs(0)
267258
} else {
268-
0
269-
};
259+
let neg = v.read_u8().unwrap();
260+
if neg != 0u8 {
261+
unimplemented!();
262+
}
270263

271-
Duration::new(
272-
days * 86_400 + hours * 3_600 + minutes * 60 + seconds,
273-
micros * 1_000,
274-
)
264+
let days = u64::from(v.read_u32::<LittleEndian>().unwrap());
265+
let hours = u64::from(v.read_u8().unwrap());
266+
let minutes = u64::from(v.read_u8().unwrap());
267+
let seconds = u64::from(v.read_u8().unwrap());
268+
let micros = if v.len() == 12 {
269+
v.read_u32::<LittleEndian>().unwrap()
270+
} else {
271+
0
272+
};
273+
274+
Duration::new(
275+
days * 86_400 + hours * 3_600 + minutes * 60 + seconds,
276+
micros * 1_000,
277+
)
278+
}
275279
} else {
276280
panic!("invalid type conversion from {:?} to datetime", self)
277281
}
@@ -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(8u8)?;
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)