1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-13 07:40:05 +08:00

Compare commits

..

2 Commits

Author SHA1 Message Date
6e821a4cd9 chore: coding style 2020-11-08 23:10:33 +08:00
755dc5619d feat: add parse_duration 2020-11-08 22:58:31 +08:00
4 changed files with 63 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "rust_util" name = "rust_util"
version = "0.6.15" version = "0.6.16"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Hatter's Rust Util" description = "Hatter's Rust Util"

View File

@@ -1,8 +1,5 @@
use std::{ use std::{ fs::File, io::{ self, ErrorKind, prelude::* } };
fs::File, use std::time::{ SystemTime, Duration };
io::{ self, ErrorKind, prelude::* },
time::{ SystemTime, Duration },
};
use crate::{ XResult, new_box_ioerror }; use crate::{ XResult, new_box_ioerror };
use crate::util_size; use crate::util_size;

View File

@@ -9,39 +9,35 @@ pub const SIZE_PB: i64 = SIZE_TB * SIZE_KB;
pub fn parse_size(size: &str) -> XResult<i64> { pub fn parse_size(size: &str) -> XResult<i64> {
let lower_size = size.to_lowercase(); let lower_size = size.to_lowercase();
let no_last_b_size = if lower_size.ends_with('b') { let no_last_b_size = crate::iff!(
&lower_size[0..lower_size.len()-1] lower_size.ends_with('b'), &lower_size[0..lower_size.len()-1], &lower_size
} else { );
&lower_size let parse_and_process_size = |mul: i64| -> XResult<i64> {
Ok((mul as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64)
}; };
if no_last_b_size.ends_with('k') { match no_last_b_size.chars().last() {
return Ok((SIZE_KB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64); Some('k') => parse_and_process_size(SIZE_KB),
} else if no_last_b_size.ends_with('m') { Some('m') => parse_and_process_size(SIZE_MB),
return Ok((SIZE_MB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64); Some('g') => parse_and_process_size(SIZE_GB),
} else if no_last_b_size.ends_with('g') { Some('t') => parse_and_process_size(SIZE_TB),
return Ok((SIZE_GB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64); Some('p') => parse_and_process_size(SIZE_PB),
} else if no_last_b_size.ends_with('t') { _ => Ok(no_last_b_size.parse::<i64>()?),
return Ok((SIZE_TB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
} else if no_last_b_size.ends_with('p') {
return Ok((SIZE_PB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
} }
Ok(no_last_b_size.parse::<i64>()?)
} }
pub fn get_display_size(size: i64) -> String { pub fn get_display_size(size: i64) -> String {
if size < SIZE_KB { if size < SIZE_KB {
size.to_string() size.to_string()
} else if size < SIZE_MB { } else if size < SIZE_MB {
format!("{:.*}KB", 2, (size as f64) / 1024.) format!("{:.*}KB", 2, (size as f64) / SIZE_KB as f64)
} else if size < SIZE_GB { } else if size < SIZE_GB {
format!("{:.*}MB", 2, (size as f64) / 1024. / 1024.) format!("{:.*}MB", 2, (size as f64) / SIZE_MB as f64)
} else if size < SIZE_TB { } else if size < SIZE_TB {
format!("{:.*}GB", 2, (size as f64) / 1024. / 1024. / 1024.) format!("{:.*}GB", 2, (size as f64) / SIZE_GB as f64)
} else if size < SIZE_PB { } else if size < SIZE_PB {
format!("{:.*}TB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024.) format!("{:.*}TB", 2, (size as f64) / SIZE_TB as f64)
} else { } else {
format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.) format!("{:.*}PB", 2, (size as f64) / SIZE_PB as f64)
} }
} }

View File

@@ -1,4 +1,5 @@
use std::time::SystemTime; use std::time::SystemTime;
use std::time::Duration;
pub fn get_current_secs() -> u64 { pub fn get_current_secs() -> u64 {
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0 /* SHOULD NOT HAPPEN */ ) SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0 /* SHOULD NOT HAPPEN */ )
@@ -7,3 +8,45 @@ pub fn get_current_secs() -> u64 {
pub fn get_current_millis() -> u128 { pub fn get_current_millis() -> u128 {
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map(|d| d.as_millis()).unwrap_or(0 /* SHOULD NOT HAPPEN */ ) SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map(|d| d.as_millis()).unwrap_or(0 /* SHOULD NOT HAPPEN */ )
} }
pub fn parse_duration(t: &str) -> Option<Duration> {
if t.is_empty() {
return None;
}
let parse_and_process_time = |mul: u32| {
t[..t.len() - 1].parse::<f64>().map(|ti| Duration::from_millis((ti * mul as f64) as u64)).ok()
};
match t.to_ascii_lowercase().chars().last() {
Some('s') => parse_and_process_time(1000),
Some('m') => parse_and_process_time(60 * 1000),
Some('h') => parse_and_process_time(60 * 60 * 1000),
Some('d') => parse_and_process_time(24 * 60 * 60 * 1000),
_ => t.parse::<u64>().map(|t| Duration::from_millis(t)).ok(),
}
}
#[test]
fn test_get_current_secs() {
assert!(get_current_secs() != 0);
}
#[test]
fn test_get_current_millis() {
assert!(get_current_millis() != 0);
}
#[test]
fn test_parse_duration() {
assert_eq!(None, parse_duration(""));
assert_eq!(None, parse_duration("X"));
assert_eq!(None, parse_duration("S"));
assert_eq!(Duration::from_millis(1), parse_duration("1").unwrap());
assert_eq!(Duration::from_millis(100), parse_duration("100").unwrap());
assert_eq!(Duration::from_millis(1000), parse_duration("1s").unwrap());
assert_eq!(Duration::from_millis(2000), parse_duration("2S").unwrap());
assert_eq!(Duration::from_millis(1500), parse_duration("1.5s").unwrap());
assert_eq!(Duration::from_millis(60000), parse_duration("1m").unwrap());
assert_eq!(Duration::from_millis(3600000), parse_duration("1h").unwrap());
assert_eq!(Duration::from_millis(1800000), parse_duration("0.5h").unwrap());
assert_eq!(Duration::from_millis(24*3600000), parse_duration("1d").unwrap());
}