Skip to main content

Crate fiscalclock

Crate fiscalclock 

Source
Expand description

Fiscal calendar engine: compute any fiscal calendar from a JSON definition.

A definition (see spec/definition-v1.md) describes how a fiscal year is bounded, subdivided, divided into weeks and named. FiscalCalendar validates it once and then answers every question from it: which period a date falls in, the bounds of a period, labels, a date dimension and a trace of the rules applied.

use fiscalclock::{Date, FiscalCalendar, Level};

let nrf = FiscalCalendar::from_json(r#"{
  "spec_version": 1,
  "year": {"type": "week_based",
           "anchor": {"edge": "end", "month": 1, "day": "last", "rule": "nearest"}},
  "week": {"starts_on": "sunday", "numbering": "contains_start"},
  "subdivision": {"type": "week_pattern", "periods": [4,5,4,4,5,4,4,5,4,4,5,4],
                  "quarters": [3,3,3,3], "extra_week": {"period": 12}},
  "naming": {"default": "fy", "schemes": {"fy": {"year_number": "start",
             "templates": {"year": "FY{yyyy}", "quarter": "Q{q} FY{yyyy}"}}}}
}"#).unwrap();

let fy2023 = nrf.period(Level::Year, 2023, None).unwrap();
assert_eq!(fy2023.start, Date::from_ymd(2023, 1, 29).unwrap());
assert_eq!(fy2023.last_day(), Date::from_ymd(2024, 2, 3).unwrap());
assert_eq!(fy2023.weeks, Some(53));

let here = nrf.locate(Date::from_ymd(2024, 2, 3).unwrap()).unwrap();
assert_eq!(nrf.format(&here.quarter, None).unwrap(), "Q4 FY2023");

Bindings for other languages call json::validate, json::call and FiscalCalendar::call_json, which take and return JSON text, and build their own failures with json::errors_json and json::timestamp_rejected. Every language therefore gives byte-identical results.

The crate is no_std with alloc, reads no clock, environment or global state, and never panics on any input: every failure is an Error value.

Modules§

json
The JSON API that every binding wraps.

Structs§

Candidate
One period a label could denote, reported by ErrorCode::AmbiguousLabel.
Date
A proleptic Gregorian civil date in the years 0001 to 9999.
Definition
A parsed and validated calendar definition.
DimensionRow
One day of the date dimension.
Error
A structured error: a stable code, the JSON path it concerns, and text for people.
Errors
Every error found while validating a definition, sorted by path and then code.
Explanation
The rule trace behind FiscalCalendar::locate for one date.
FiscalCalendar
A fiscal calendar built from a validated Definition.
Location
Where a date falls at every level, returned by FiscalCalendar::locate.
ParseDateError
The error returned when a string is not a strict YYYY-MM-DD date in 0001–9999.
Period
A fiscal period: the half-open interval [start, end_exclusive) at one level.
Step
One step of an Explanation.

Enums§

Details
Structured details attached to some errors.
ErrorCode
A stable error code from the catalogue in spec/api-v1.md.
Level
A level of the fiscal hierarchy.
Weekday
A day of the week.

Constants§

FIXED_COLUMNS
The fixed columns of a dimension row, before the label columns.