4个版本
0.1.3 | 2022年8月30日 |
---|---|
0.1.2 | 2022年8月29日 |
0.1.1 | 2022年8月29日 |
0.1.0 | 2022年8月29日 |
#298 in 日期和时间
125KB
860 行
概述
本包提供了一组组件,用于描述重复事件属性和行为。
本包的典型用法是创建一个重复事件,定义其重复规则(例如,每个星期二发生,每月的15日发生)然后从事件中获取符合规则的下一个日期。
当前支持四种周期性(每日、每周、每月和每年),通过四种类型: DailyEvent
,WeeklyEvent
,MonthlyEvent
和 YearlyEvent
。
使用年度事件的示例
use recurring_event::{YearlyEvent, Ordinal, Recurrent, MonthlyWeekdayOrdinal,
time::{Month, OffsetDateTime, Weekday, macros::datetime}};
// This yearly event starts recurring on the 14th of November 2018.
let mut event = YearlyEvent::new(datetime!(2018-11-14 00:00 UTC));
// It's an event that recurs every four years.
event.update_frequency(4);
// On a year, the event happens on every Monday and Thursday of February.
event.insert_weekday(Weekday::Monday, Month::February);
event.insert_weekday(Weekday::Thursday, Month::February);
// And also on the 7th and 15th of February.
event.insert_monthly_day_ordinal(7, Month::February);
event.insert_monthly_day_ordinal(25, Month::February);
// And also on the second Tuesday and the last Sunday of February.
event.insert_monthly_weekday_ordinal(
MonthlyWeekdayOrdinal::new(Ordinal::Second, Weekday::Tuesday),
Month::February);
event.insert_monthly_weekday_ordinal(
MonthlyWeekdayOrdinal::new(Ordinal::Last, Weekday::Sunday),
Month::February);
// The event also happens on August, every Tuesday and Saturday.
event.insert_weekday(Weekday::Tuesday, Month::August);
event.insert_weekday(Weekday::Saturday, Month::August);
// And on the 13th and 29th of August.
event.insert_monthly_day_ordinal(13, Month::August);
event.insert_monthly_day_ordinal(29, Month::August);
// And on the first Wednesday and third Friday of August.
event.insert_monthly_weekday_ordinal(
MonthlyWeekdayOrdinal::new(Ordinal::First, Weekday::Wednesday),
Month::August);
event.insert_monthly_weekday_ordinal(
MonthlyWeekdayOrdinal::new(Ordinal::Third, Weekday::Friday),
Month::August);
// One possibility for getting the next occurrence dates is to take
// advantage of the fact that a YearlyEvent implement the Iterator trait.
// Let's get the next fourty occurrence dates !
let next_occurrences_dates: Vec<OffsetDateTime> = event.take(40).collect();
let expected_dates = vec![datetime!(2022-02-03 00:00 UTC),
datetime!(2022-02-07 00:00 UTC),
datetime!(2022-02-08 00:00 UTC),
datetime!(2022-02-10 00:00 UTC),
datetime!(2022-02-14 00:00 UTC),
datetime!(2022-02-17 00:00 UTC),
datetime!(2022-02-21 00:00 UTC),
datetime!(2022-02-24 00:00 UTC),
datetime!(2022-02-25 00:00 UTC),
datetime!(2022-02-27 00:00 UTC),
datetime!(2022-02-28 00:00 UTC),
datetime!(2022-08-02 00:00 UTC),
datetime!(2022-08-03 00:00 UTC),
datetime!(2022-08-06 00:00 UTC),
datetime!(2022-08-09 00:00 UTC),
datetime!(2022-08-13 00:00 UTC),
datetime!(2022-08-16 00:00 UTC),
datetime!(2022-08-19 00:00 UTC),
datetime!(2022-08-20 00:00 UTC),
datetime!(2022-08-23 00:00 UTC),
datetime!(2022-08-27 00:00 UTC),
datetime!(2022-08-29 00:00 UTC),
datetime!(2022-08-30 00:00 UTC),
datetime!(2026-02-02 00:00 UTC),
datetime!(2026-02-05 00:00 UTC),
datetime!(2026-02-07 00:00 UTC),
datetime!(2026-02-09 00:00 UTC),
datetime!(2026-02-10 00:00 UTC),
datetime!(2026-02-12 00:00 UTC),
datetime!(2026-02-16 00:00 UTC),
datetime!(2026-02-19 00:00 UTC),
datetime!(2026-02-22 00:00 UTC),
datetime!(2026-02-23 00:00 UTC),
datetime!(2026-02-25 00:00 UTC),
datetime!(2026-02-26 00:00 UTC),
datetime!(2026-08-01 00:00 UTC),
datetime!(2026-08-04 00:00 UTC),
datetime!(2026-08-05 00:00 UTC),
datetime!(2026-08-08 00:00 UTC),
datetime!(2026-08-11 00:00 UTC)];
assert_eq!(expected_dates, next_occurrences_dates)
time
包
本包使用 time
包,特别是 OffsetDateTime
,Month
和 Weekday
类型。
OffsetDateTime
存储时间和偏移量信息(即时区)。目前,本包没有使用它们。它可用于您的代码,如果您需要管理事件中的特定时间和偏移量。
serde
和 typetag
包
(反)序列化事件类型得益于 serde
,以及(反)序列化 Box<dyn Recurrent>
特征对象得益于 typetag
。
依赖项
~1.7–2.7MB
~56K SLoC