| Crates.io | ms2 |
| lib.rs | ms2 |
| version | 0.2.0 |
| created_at | 2024-06-03 15:38:40.472679+00 |
| updated_at | 2024-06-07 12:00:32.032565+00 |
| description | A Rust library that replicates the ms package from TypeScript for Rust. |
| homepage | https://github.com/smartarray/ms-rs |
| repository | https://github.com/smartarray/ms-rs |
| max_upload_size | |
| id | 1260227 |
| size | 13,123 |
ms2-rs ⏱️ms2 is a Rust library that recreates the ms package from TypeScript for Rust. It helps you convert milliseconds to human-readable time strings and vice versa. If you need to work with time in your Rust project, ms2 makes it easy.
format or parse to use explicit and typesafe methodsAdd ms2 to your Cargo.toml:
[dependencies]
ms2 = "0.2.0"
Then use it in your code:
use ms2::ms;
fn main() {
let ms_output = ms("2 days").unwrap();
match ms_output {
ms2::MsOutput::Milliseconds(ms) => println!("{}", ms), // Outputs: 172800000
_ => panic!("Expected milliseconds"),
}
}
ms2 with an integer input to convert to a human-readable stringuse ms2::ms;
fn main() {
let time_str_output = ms(172800000).unwrap();
match time_str_output {
ms2::MsOutput::Str(time_str) => println!("{}", time_str), // Outputs: "2 days"
_ => panic!("Expected string"),
}
}
unwrap_number and unwrap_struse ms2::{ms, UnwrapMsOutput};
fn main() {
let ms_value = ms("1 hour").unwrap_number();
println!("{}", ms_value); // Outputs: 3600000
let time_str = ms(3600000).unwrap_str();
println!("{}", time_str); // Outputs: "1 hour"
}
format and parseuse ms2::{format, parse};
fn main() {
println!("parse(\"60 seconds\") = {}", parse("60 seconds").unwrap()); // parse("60 seconds") = 60000
println!("format(172_800_000) = {}", format(172_800_000)); // format(172_800_000) = 2 days
}
use ms2::{format, parse};
fn main() {
println!("parse(\"-60 seconds\") = {}", parse("-60 seconds").unwrap()); // parse("-60 seconds") = -60000
println!("format(-172_800_000) = {}", format(-172_800_000)); // format(-172_800_000) = -2 days
}
Contributions are welcome! Check the CONTRIBUTING.md file for more info.
This project is licensed under the MIT License. See the LICENSE file for details.