Rust 中每 M 个月的第 N 天

Nth day of every Mth month in Rust

我一直在努力寻找一个 crate 来处理复杂的时间序列。 使用出色的 kronos crate,我可以使用 NthOfGrains() 创建 TimeSequence,例如“每个月的第 10 天”或“每年的第一个星期一”。

// 10th day of every month
tenth_day = NthOf(10, Grains(Grain::Day), Grains(Grain::Month));

// 1st Monday of every year
first_monday = Nth(1, Weekday(1), Grains(Grain::Year));

但我想对更复杂的 TimeSequence 执行此操作,例如“每隔一个月的第 27 天”。 Grain::QuarterGrain::Half 枚举变体分别代表每 3 个月和 6 个月,但这些变体是硬编码的,不会推广到不平分一年的月份,比如每 5 个月.

有什么方法可以让我用 kronos 得到这些更复杂的 TimeSequence?或者我可以使用另一个板条箱来解决这个问题吗?

discussing with kronos' maintainer, using the step_by 函数或 step_by 方法之后可以使这个工作。

这里是一个在 Grains 对象上使用 step_by 方法的例子。

use chrono::Datelike;

let mut every_3months_from_next_march_iter = Grains(Grain::Month)
    .future(&t0_april)
    .skip_while(|r| r.start.date().month() != 3)
    .step_by(3);
assert_eq!(
    every_3months_from_next_march_iter.next().unwrap(),
    Range{
        start: dt(2019, 3, 1),
        end: dt(2019, 4, 1), grain: Grain::Month
    }
);

这里是 step_by 函数的示例。

use kronos::{step_by, Grains, Grain, NthOf};

let third_day_every_5_months = NthOf(
    3,
    Grains(Grain::Day),
    step_by(Grains(Grain::Month), 5)
);