1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
`duration-string` is a library to convert from `String` to `Duration` and vice-versa.
Uses zero dependencies unless `serde` feature is enabled.
[](https://github.com/RonniSkansing/duration-string/actions/workflows/build.yaml)

Takes a `String` such as `100ms`, `2s`, `5m 30s`, `1h10m` and converts it into a `Duration`.
Takes a `Duration` and converts it into `String`.
The `String` format is a multiply of `[0-9]+(ns|us|ms|[smhdwy])`
`String` to `Duration`:
```rust
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`:
```rust
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"));
```
You can enable _serialization/deserialization_ support by adding the feature `serde`
-
duration-string = { version = "0.5.0", features = ["serde"] }
-
use DurationString;
use ;
This project is licensed under the [MIT](https://opensource.org/licenses/MIT) License.
See [LICENSE](./LICENSE) file for details.