chrono-light

Crates.iochrono-light
lib.rschrono-light
version0.1.5
sourcesrc
created_at2022-04-18 18:33:36.70252
updated_at2022-07-06 15:33:33.636877
descriptionLight DateTime/scheduler library for Rust, no_std compatible
homepagehttps://github.com/konrads/chrono-light
repositoryhttps://github.com/konrads/chrono-light
max_upload_size
id569976
size86,597
Konrad Sosnowski (konrads)

documentation

README

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.

Commit count: 15

cargo fmt