28 个版本 (9 个破坏性更新)

0.11.2 2024年6月6日
0.10.0 2024年5月10日
0.7.1 2023年12月11日
0.7.0 2023年9月18日
0.2.0 2021年3月16日

日期和时间 中排名第 7

Download history 20258/week @ 2024-05-02 22494/week @ 2024-05-09 22560/week @ 2024-05-16 22464/week @ 2024-05-23 20180/week @ 2024-05-30 25689/week @ 2024-06-06 23674/week @ 2024-06-13 21514/week @ 2024-06-20 21135/week @ 2024-06-27 19065/week @ 2024-07-04 21196/week @ 2024-07-11 18535/week @ 2024-07-18 19366/week @ 2024-07-25 17538/week @ 2024-08-01 20340/week @ 2024-08-08 17462/week @ 2024-08-15

每月下载量 78,138
30 crate 中使用 30 (直接使用 24 个)

Apache-2.0

105KB
1.5K SLoC

将字符串解析为 std::time::Duration

duration-str parser

Chrono GitHub Actions Crates.io Docs.rs Coverage Status

功能

注意 ⚠️

默认持续时间单位为秒。也使用以下 持续时间单位

持续时间单位列表

将字符串解析为Duration。支持的字符串持续时间单位包括以下之一:["y","mon","w","d","h","m","s", "ms", "µs", "ns"]

单位 描述 单位列表选项(以下之一) 示例
y ["y" , "year" , "Y" , "YEAR" , "Year"] 1y
mon ["mon" , "MON" , "Month" , "month" , "MONTH"] 1mon
w ["w" , "W" , "Week" ,"WEEK" , "week"] 1w
d ["d" , "D" , "Day" , "DAY" , "day"] 1d
h 小时 ["h" , "hr" , "H" , "Hour" , "HOUR" , "hour"] 1h
m 分钟 ["m" , "M" , "Minute" , "MINUTE" , "minute" , "min" , "MIN"] 1m
s ["s" , "S" , "Second" , "SECOND" , "second" , "sec" , "SEC"] 1s
ms 毫秒 ["ms" , "MS" , "Millisecond" , "MilliSecond" , "MILLISECOND" , "millisecond" , "mSEC"] 1ms
µs 微秒 ["µs" , "µS" , "µsecond" , "Microsecond" , "MicroSecond" , "MICROSECOND" , "microsecond" , "µSEC"] 1µs
ns 纳秒 ["ns" , "NS" , "Nanosecond" , "NanoSecond" , "NANOSECOND" , "nanosecond" , "nSEC"] 1ns

此外,duration_str支持时间持续简单评估(+,*)。请参阅示例

示例

[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));
}

在结构体中反序列化

反序列化为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));
}
  • 选项字段反序列化
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()
        }
    );
}

您还可以使用deserialize_duration_chronodeserialize_duration_time函数

例如

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));
}

依赖关系

~4MB
~79K SLoC