29 releases (9 breaking)

0.11.3 Nov 28, 2024
0.11.2 Jun 6, 2024
0.10.0 May 10, 2024
0.7.1 Dec 11, 2023
0.2.0 Mar 16, 2021

#6 in Date and time

Download history 17667/week @ 2024-08-21 18846/week @ 2024-08-28 22189/week @ 2024-09-04 22167/week @ 2024-09-11 17270/week @ 2024-09-18 21407/week @ 2024-09-25 19632/week @ 2024-10-02 20010/week @ 2024-10-09 22446/week @ 2024-10-16 20844/week @ 2024-10-23 19524/week @ 2024-10-30 18785/week @ 2024-11-06 21146/week @ 2024-11-13 20863/week @ 2024-11-20 18038/week @ 2024-11-27 18530/week @ 2024-12-04

82,560 downloads per month
Used in 45 crates (32 directly)

Apache-2.0

105KB
1.5K SLoC

parse string to std::time::Duration

duration-str parser

Chrono GitHub Actions Crates.io Docs.rs Coverage Status

Features:

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"]

unit Description unit list option(one of) example
y Year ["y" , "year" , "Y" , "YEAR" , "Year"] 1y
mon Month ["mon" , "MON" , "Month" , "month" , "MONTH"] 1mon
w Week ["w" , "W" , "Week" ,"WEEK" , "week"] 1w
d Day ["d" , "D" , "Day" , "DAY" , "day"] 1d
h Hour ["h" , "hr" , "H" , "Hour" , "HOUR" , "hour"] 1h
m Minute ["m" , "M" , "Minute" , "MINUTE" , "minute" , "min" , "MIN"] 1m
s Second ["s" , "S" , "Second" , "SECOND" , "second" , "sec" , "SEC"] 1s
ms Millisecond ["ms" , "MS" , "Millisecond" , "MilliSecond" , "MILLISECOND" , "millisecond" , "mSEC"] 1ms
µs Microsecond ["µs" , "µS" , "µsecond" , "Microsecond" , "MicroSecond" , "MICROSECOND" , "microsecond" , "µSEC"] 1µs
ns Nanosecond ["ns" , "NS" , "Nanosecond" , "NanoSecond" , "NANOSECOND" , "nanosecond" , "nSEC"] 1ns

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

example

[dependencies]
duration-str = "{latest version}" 
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("3m31s").unwrap();
    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));
}
  • option filed deserialize
use duration_str::deserialize_option_duration;
use serde::*;
use std::time::Duration;

#[derive(Debug, Deserialize, PartialEq)]
struct Config {
    #[serde(default, deserialize_with = "deserialize_option_duration")]
    time_ticker: Option<Duration>,
    name: String,
}

fn main() {
    let json = r#"{"time_ticker":null,"name":"foo"}"#;
    let config: Config = serde_json::from_str(json).unwrap();

    assert_eq!(
        config,
        Config {
            time_ticker: None,
            name: "foo".into()
        }
    );

    let json = r#"{"name":"foo"}"#;
    let config: Config = serde_json::from_str(json).unwrap();
    assert_eq!(
        config,
        Config {
            time_ticker: None,
            name: "foo".into()
        }
    );
}

Also you can use deserialize_duration_chrono or deserialize_duration_time function

E.g:

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

~4MB
~78K SLoC