14 releases

0.3.8 Dec 28, 2021
0.3.7 Nov 4, 2021
0.3.4 Aug 26, 2021
0.3.3 Jul 31, 2021
0.1.4 Mar 15, 2021

#42 in Date and time

Download history 103/week @ 2021-09-28 102/week @ 2021-10-05 100/week @ 2021-10-12 165/week @ 2021-10-19 91/week @ 2021-10-26 152/week @ 2021-11-02 106/week @ 2021-11-09 49/week @ 2021-11-16 31/week @ 2021-11-23 119/week @ 2021-11-30 102/week @ 2021-12-07 50/week @ 2021-12-14 93/week @ 2021-12-21 90/week @ 2021-12-28 180/week @ 2022-01-04 72/week @ 2022-01-11

441 downloads per month
Used in ssubmit

MIT AND Apache-2.0

28KB
460 lines

parse string to std::time::Duration

Chrono GitHub Actions Crates.io Docs.rs

Notice ⚠️

The default duration unit is second.Also use below duration unit

Duration Unit List

Parse string to Duration . The String duration unit support for one of:["y","mon","w","d","h","m","s", "ms", "µs", "ns"]

  • y:Year. Support string value: ["y" | "year" | "Y" | "YEAR" | "Year"]. e.g. 1y
  • mon:Month.Support string value: ["mon" | "MON" | "Month" | "month" | "MONTH"]. e.g. 1mon
  • w:Week.Support string value: ["w" | "W" | "Week" | "WEEK" | "week"]. e.g. 1w
  • d:Day.Support string value: ["d" | "D" | "Day" | "DAY" | "day"]. e.g. 1d
  • h:Hour.Support string value: ["h" | "H" | "Hour" | "HOUR" | "hour"]. e.g. 1h
  • m:Minute.Support string value: ["m" | "M" | "Minute" | "MINUTE" | "minute" | "min" | "MIN"]. e.g. 1m
  • s:Second.Support string value: ["s" | "S" | "Second" | "SECOND" | "second" | "sec" | "SEC"]. e.g. 1s
  • ms:Millisecond.Support string value: ["ms" | "MS" | "Millisecond" | "MilliSecond" | "MILLISECOND" | "millisecond" | "mSEC" ]. e.g. 1ms
  • µs:Microsecond.Support string value: ["µs" | "µS" | "µsecond" | "Microsecond" | "MicroSecond" | "MICROSECOND" | "microsecond" | "µSEC"]. e.g. 1µs
  • ns:Nanosecond.Support string value: ["ns" | "NS" | "Nanosecond" | "NanoSecond" | "NANOSECOND" | "nanosecond" | "nSEC"]. e.g. 1ns

Also,duration_str support time duration simple evaluation(+,*). See example:

example

[dependencies]
duration-str = "0.3" 
use duration_str::parse;
use std::time::Duration;

fn main() {
    let duration = parse("1d").unwrap();
    assert_eq!(duration, Duration::new(24 * 60 * 60, 0));

    let duration = parse("3m+31").unwrap(); //the default duration unit is second.
    assert_eq!(duration, Duration::new(211, 0));

    let duration = parse("3m + 31").unwrap(); //the default duration unit is second.
    assert_eq!(duration, Duration::new(211, 0));

    let duration = parse("3m + 13s + 29ms").unwrap();
    assert_eq!(duration, Duration::new(193, 29 * 1000 * 1000 + 0 + 0));

    let duration = parse("3m + 1s + 29ms +17µs").unwrap();
    assert_eq!(
        duration,
        Duration::new(181, 29 * 1000 * 1000 + 17 * 1000 + 0)
    );

    let duration = parse("1m*10").unwrap(); //the default duration unit is second.
    assert_eq!(duration, Duration::new(600, 0));

    let duration = parse("1m*10ms").unwrap();
    assert_eq!(duration, Duration::new(0, 600 * 1000 * 1000));

    let duration = parse("1m * 1ns").unwrap();
    assert_eq!(duration, Duration::new(0, 60));

    let duration = parse("1m * 1m").unwrap();
    assert_eq!(duration, Duration::new(3600, 0));
}

deserialize in struct

deserialize to std::time::Duration

use duration_str::deserialize_duration;
use serde::*;
use std::time::Duration;

#[derive(Debug, Deserialize)]
struct Config {
    #[serde(deserialize_with = "deserialize_duration")]
    time_ticker: Duration,
}

fn main() {
    let json = r#"{"time_ticker":"1m+30"}"#;
    let config: Config = serde_json::from_str(json).unwrap();
    assert_eq!(config.time_ticker, Duration::new(60 + 30, 0));

    let json = r#"{"time_ticker":"1m+30s"}"#;
    let config: Config = serde_json::from_str(json).unwrap();
    assert_eq!(config.time_ticker, Duration::new(60 + 30, 0));
}

Also you can use deserialize_duration_chrono function

use chrono::Duration;
use duration_str::deserialize_duration_chrono;
use serde::*;

#[derive(Debug, Deserialize)]
struct Config {
    #[serde(deserialize_with = "deserialize_duration_chrono")]
    time_ticker: Duration,
}

fn main() {
    let json = r#"{"time_ticker":"1m+30"}"#;
    let config: Config = serde_json::from_str(json).unwrap();
    assert_eq!(config.time_ticker, Duration::seconds(60 + 30));
}

Dependencies

~4.5MB
~92K SLoC

a