2014-07-22 01:50:53 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Arcterus <arcterus@mail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn from_str(string: &str) -> Result<f64, String> {
|
|
|
|
let len = string.len();
|
|
|
|
if len == 0 {
|
|
|
|
return Err("empty string".to_string())
|
|
|
|
}
|
|
|
|
let slice = string.slice_to(len - 1);
|
|
|
|
let (numstr, times) = match string.char_at(len - 1) {
|
|
|
|
's' | 'S' => (slice, 1u),
|
|
|
|
'm' | 'M' => (slice, 60u),
|
|
|
|
'h' | 'H' => (slice, 60u * 60),
|
|
|
|
'd' | 'D' => (slice, 60u * 60 * 24),
|
|
|
|
val => {
|
|
|
|
if !val.is_alphabetic() {
|
|
|
|
(string, 1)
|
|
|
|
} else if string == "inf" || string == "infinity" {
|
|
|
|
("inf", 1)
|
|
|
|
} else {
|
|
|
|
return Err(format!("invalid time interval '{}'", string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-11-17 14:56:00 +00:00
|
|
|
match ::std::str::from_str::<f64>(numstr) {
|
2014-07-22 01:50:53 +00:00
|
|
|
Some(m) => Ok(m * times as f64),
|
|
|
|
None => Err(format!("invalid time interval '{}'", string))
|
|
|
|
}
|
|
|
|
}
|