#duration #format-duration #string #convert-string #lib #visa-versa #smhdwy

duration-string

string to duration and visa-versa lib. format is [0-9]+(ns|us|ms|[smhdwy]) such as 100ms, 1s, 2h, 1y

10 unstable releases (3 breaking)

0.3.0 Mar 22, 2023
0.2.0 Jan 5, 2023
0.1.1 Jun 7, 2022
0.0.6 Mar 28, 2020

#86 in Date and time

Download history 7242/week @ 2023-12-13 4073/week @ 2023-12-20 3004/week @ 2023-12-27 7460/week @ 2024-01-03 6983/week @ 2024-01-10 8661/week @ 2024-01-17 9865/week @ 2024-01-24 11801/week @ 2024-01-31 10887/week @ 2024-02-07 8830/week @ 2024-02-14 8504/week @ 2024-02-21 8627/week @ 2024-02-28 11179/week @ 2024-03-06 9088/week @ 2024-03-13 9333/week @ 2024-03-20 7520/week @ 2024-03-27

38,551 downloads per month
Used in 23 crates (12 directly)

Custom license

18KB
363 lines

duration-string

duration-string is a string to duration and visa-versa lib.

Crates.io MIT licensed

Takes a string such as 100ms, 2s, 5m and converts it into a Duration Takes a duration and makes it into string.

The string format is [0-9]+(ns|us|ms|[smhdwy])

Example

String to duration

use std::convert::TryFrom;
use duration_string::DurationString;
use std::time::Duration;
let d: Duration = DurationString::try_from(String::from("100ms")).unwrap().into();
assert_eq!(d, Duration::from_millis(100));
// Alternatively:
let d: Duration = "100ms".parse::<DurationString>().unwrap().into();
assert_eq!(d, Duration::from_millis(100));

duration to string

use std::convert::TryFrom;
use duration_string::*;
use std::time::Duration;
let d: String = DurationString::from(Duration::from_millis(100)).into();
assert_eq!(d, String::from("100ms"));

Serde support

You can enable serialize/unserialize support by adding the feature serde_support

  • Add serde_support to the dependency duration-string = { version = "0.0.1", features = ["serde_support"] }
  • Add derive to struct
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Foo {
 duration: DurationString
}

License: MIT

Dependencies

~175KB