Simplify date parsing code
This commit is contained in:
parent
25a2ba7383
commit
879af4d60b
1 changed files with 11 additions and 19 deletions
30
src/util.rs
30
src/util.rs
|
@ -1,7 +1,4 @@
|
||||||
use time;
|
use time::{Tm, empty_tm};
|
||||||
use time::Tm;
|
|
||||||
use std::io;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
|
|
||||||
pub fn msdos_datetime_to_tm(time: u16, date: u16) -> Tm
|
pub fn msdos_datetime_to_tm(time: u16, date: u16) -> Tm
|
||||||
{
|
{
|
||||||
|
@ -12,22 +9,17 @@ pub fn msdos_datetime_to_tm(time: u16, date: u16) -> Tm
|
||||||
let months = (date & 0b0000000111100000) >> 5;
|
let months = (date & 0b0000000111100000) >> 5;
|
||||||
let years = (date & 0b1111111000000000) >> 9;
|
let years = (date & 0b1111111000000000) >> 9;
|
||||||
|
|
||||||
let datetime = format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
|
// Month range: Dos [1..12], Tm [0..11]
|
||||||
years as u32 + 1980,
|
// Year zero: Dos 1980, Tm 1900
|
||||||
months,
|
|
||||||
days,
|
|
||||||
hours,
|
|
||||||
minutes,
|
|
||||||
seconds);
|
|
||||||
let format = "%Y-%m-%d %H:%M:%S";
|
|
||||||
|
|
||||||
match time::strptime(&*datetime, format)
|
Tm {
|
||||||
{
|
tm_sec: seconds as i32,
|
||||||
Ok(tm) => tm,
|
tm_min: minutes as i32,
|
||||||
Err(m) => {
|
tm_hour: hours as i32,
|
||||||
let _ = write!(&mut io::stderr(), "Failed parsing date: {}", m);
|
tm_mday: days as i32,
|
||||||
time::empty_tm()
|
tm_mon: months as i32 - 1,
|
||||||
},
|
tm_year: years as i32 + 80,
|
||||||
|
..empty_tm()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue