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 {
|
2016-01-05 19:42:52 +00:00
|
|
|
return Err("empty string".to_owned())
|
2014-07-22 01:50:53 +00:00
|
|
|
}
|
2015-01-24 09:44:48 +00:00
|
|
|
let slice = &string[..len - 1];
|
2015-05-01 22:36:15 +00:00
|
|
|
let (numstr, times) = match string.chars().next_back().unwrap() {
|
|
|
|
's' | 'S' => (slice, 1),
|
|
|
|
'm' | 'M' => (slice, 60),
|
|
|
|
'h' | 'H' => (slice, 60 * 60),
|
|
|
|
'd' | 'D' => (slice, 60 * 60 * 24),
|
2014-07-22 01:50:53 +00:00
|
|
|
val => {
|
|
|
|
if !val.is_alphabetic() {
|
|
|
|
(string, 1)
|
|
|
|
} else if string == "inf" || string == "infinity" {
|
|
|
|
("inf", 1)
|
|
|
|
} else {
|
|
|
|
return Err(format!("invalid time interval '{}'", string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-01-01 11:14:28 +00:00
|
|
|
match numstr.parse::<f64>() {
|
2015-02-03 21:19:13 +00:00
|
|
|
Ok(m) => Ok(m * times as f64),
|
|
|
|
Err(e) => Err(format!("invalid time interval '{}': {}", string, e))
|
2014-07-22 01:50:53 +00:00
|
|
|
}
|
|
|
|
}
|