mirror of
https://github.com/jht5945/rust_util.git
synced 2026-01-13 07:40:05 +08:00
Compare commits
2 Commits
90ffe7c7d9
...
6e821a4cd9
| Author | SHA1 | Date | |
|---|---|---|---|
| 6e821a4cd9 | |||
| 755dc5619d |
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rust_util"
|
||||
version = "0.6.15"
|
||||
version = "0.6.16"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Hatter's Rust Util"
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{ self, ErrorKind, prelude::* },
|
||||
time::{ SystemTime, Duration },
|
||||
};
|
||||
use std::{ fs::File, io::{ self, ErrorKind, prelude::* } };
|
||||
use std::time::{ SystemTime, Duration };
|
||||
|
||||
use crate::{ XResult, new_box_ioerror };
|
||||
use crate::util_size;
|
||||
|
||||
@@ -9,39 +9,35 @@ pub const SIZE_PB: i64 = SIZE_TB * SIZE_KB;
|
||||
|
||||
pub fn parse_size(size: &str) -> XResult<i64> {
|
||||
let lower_size = size.to_lowercase();
|
||||
let no_last_b_size = if lower_size.ends_with('b') {
|
||||
&lower_size[0..lower_size.len()-1]
|
||||
} else {
|
||||
&lower_size
|
||||
let no_last_b_size = crate::iff!(
|
||||
lower_size.ends_with('b'), &lower_size[0..lower_size.len()-1], &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') {
|
||||
return Ok((SIZE_KB 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('m') {
|
||||
return Ok((SIZE_MB 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('g') {
|
||||
return Ok((SIZE_GB 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('t') {
|
||||
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);
|
||||
match no_last_b_size.chars().last() {
|
||||
Some('k') => parse_and_process_size(SIZE_KB),
|
||||
Some('m') => parse_and_process_size(SIZE_MB),
|
||||
Some('g') => parse_and_process_size(SIZE_GB),
|
||||
Some('t') => parse_and_process_size(SIZE_TB),
|
||||
Some('p') => parse_and_process_size(SIZE_PB),
|
||||
_ => Ok(no_last_b_size.parse::<i64>()?),
|
||||
}
|
||||
|
||||
Ok(no_last_b_size.parse::<i64>()?)
|
||||
}
|
||||
|
||||
pub fn get_display_size(size: i64) -> String {
|
||||
if size < SIZE_KB {
|
||||
size.to_string()
|
||||
} 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 {
|
||||
format!("{:.*}MB", 2, (size as f64) / 1024. / 1024.)
|
||||
format!("{:.*}MB", 2, (size as f64) / SIZE_MB as f64)
|
||||
} 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 {
|
||||
format!("{:.*}TB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024.)
|
||||
format!("{:.*}TB", 2, (size as f64) / SIZE_TB as f64)
|
||||
} else {
|
||||
format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.)
|
||||
format!("{:.*}PB", 2, (size as f64) / SIZE_PB as f64)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::time::SystemTime;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn get_current_secs() -> u64 {
|
||||
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 {
|
||||
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());
|
||||
}
|
||||
Reference in New Issue
Block a user