#duration

duration-string

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

6 releases

0.0.6 Mar 28, 2020
0.0.5 Mar 28, 2020

#72 in Date and time

Download history 40/week @ 2021-02-23 47/week @ 2021-03-02 45/week @ 2021-03-09 44/week @ 2021-03-16 56/week @ 2021-03-23 56/week @ 2021-03-30 62/week @ 2021-04-06 118/week @ 2021-04-13 136/week @ 2021-04-20 54/week @ 2021-04-27 43/week @ 2021-05-04 32/week @ 2021-05-11 44/week @ 2021-05-18 38/week @ 2021-05-25 84/week @ 2021-06-01 87/week @ 2021-06-08

278 downloads per month

Custom license

15KB
317 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]+(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));

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

~210KB