#date-time #schedule #date #time #calendar #scheduler #codec

no-std chrono-light

Light DateTime/scheduler library for Rust, no_std compatible

6 releases

0.1.5 Jul 6, 2022
0.1.4 Jun 26, 2022
0.1.2 May 30, 2022
0.1.0 Apr 18, 2022

#313 in Date and time

MIT/Apache

85KB
785 lines

Simple DateTime/Scheduler library

test

Provides DateTime and Schedule capabilities with no external library dependencies. Works in std/no std, optional support for scale codec encoding/decoding/type info.

Functionality provided:

  • conversion from unixtime (mills from epoch) to DateTime, and vice versa
  • finding next occurrence for a schedule comprising:
    • start DateTime
    • repeat frequency (multiples of year/month/week/day/hour/minute/second/millis)
    • optional end DateTime
  • scale feature enabling Encode/Decode/TypeInfo support for DateTime/Schedule structs.

Scope

This library works with DateTimes and schedules within years of [1970, 4000].

Does not support timezones.

Overflow of months (>12), days (>28, >30, >31), hour (>23), minute/second (>59), millis (>999) is discouraged yet allowed, with excess added eg. 31 April ~= 1 May. Underflow of month/day (=0) causes panic. To avoid panic, validate hand crafted DateTime via Calendar::validate() or convert to unixtime via Calendar::to_unixtime_opt().

Reasoning behind these restrictions is to keep the footprint reasonably compact and performance reasonably fast, and cater for real life scenarios.

Typical usage

use chrono_light::prelude::*;

let c = Calendar::create();
let now_in_ms: u64 = 1650412800000;  // represents 20/04/2022 00:00:00:000
let schedule = Schedule {
    start: DateTime { year: 2020, month: 4, day: 30, hour: 0, minute: 0, second: 0, ms: 0 },
    items: vec![(Frequency::Year, 1)],
    end: Some(DateTime { year: 2025, month: 4, day: 30, hour: 0, minute: 0, second: 0, ms: 0 })
};
assert!(c.validate_schedule(&schedule).is_ok());
assert_eq!(Some(10*24*60*60*1000), c.next_occurrence_ms(&c.from_unixtime(now_in_ms), &schedule));  // triggers in 10 days

Utility within a pallet

Chrono-light serves as a scheduling mechanism for scheduler-pallet.

Dependencies

~0–580KB
~12K SLoC