fiscalclock
The Rust core of fiscalclock, a fiscal calendar engine. You describe a fiscal calendar as a JSON definition, and the crate answers questions about it: which fiscal year, quarter, period and week a date falls in, the bounds of any period, labels such as Q4 FY2023, and a day-by-day date dimension.
The crate has no built-in calendars. Every calendar, including well-known ones, is a definition you supply. The format is specified in spec/definition-v1.md and the operations in spec/api-v1.md.
Status as of 15 Nov 2026: 0.2.0 adds definitions whose year rule changes, with transition periods (see the changelog). The definition format and API are version 1 drafts and may change before 1.0.
Example
A 4-5-4 calendar whose year ends on the Saturday nearest 31 January:
use ;
let definition = 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}"}}}}
}"#;
let cal = from_json.unwrap;
let fy2023 = cal.period.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
let here = cal.locate.unwrap;
assert_eq!;
API
FiscalCalendar validates a definition once and answers every operation from it. The operations are specified in spec/api-v1.md.
impl FiscalCalendar {
fn from_json(definition: &str) -> Result<FiscalCalendar, Errors>
fn new(def: Definition) -> FiscalCalendar // from Definition::from_json(&str)
fn definition(&self) -> &Definition
fn calendar_id(&self) -> &str // first 16 hex digits of the SHA-256 of canonical_json
fn canonical_json(&self) -> &str
fn locate(&self, date: Date) -> Result<Location, Error>
fn period(&self, level: Level, year: i32, index: Option<u32>) -> Result<Period, Error>
fn shift(&self, period: &Period, n: i64) -> Result<Period, Error>
fn parent(&self, period: &Period) -> Result<Period, Error>
fn children(&self, period: &Period, level: Level) -> Result<Vec<Period>, Error>
fn years(&self, from: i32, to: i32) -> Result<Vec<Period>, Error>
fn format(&self, period: &Period, scheme: Option<&str>) -> Result<String, Error>
fn parse(&self, label: &str) -> Result<Period, Error>
fn dimension(&self, start: Date, end_exclusive: Date) -> Result<Vec<DimensionRow>, Error>
fn dimension_csv(&self, start: Date, end_exclusive: Date) -> Result<String, Error>
fn explain(&self, date: Date) -> Result<Explanation, Error>
fn label_columns(&self) -> Vec<String>
fn call_json(&self, op: &str, args: &str) -> String
}
LevelisYear,Quarter,PeriodorWeek.indexisNonefor a year.Dateis a civil date:Date::from_ymd(2024, 2, 3)andDate::parse_iso("2024-02-03")returnOption<Date>, andDateimplementsFromStrandDisplayinYYYY-MM-DDform.Periodhas the public fieldslevel,year,index,start,end_exclusive,days,weeksandhas_extra_week, and the methodlast_day(). Operations that take aPeriodrecompute it from(level, year, index).from_jsonandDefinition::from_jsonreturnErrors, every validation error found. The operations return oneError.
JSON API
fiscalclock::json::validate(definition) and fiscalclock::json::call(definition, op, args) take and return JSON text. The Python and npm packages call these functions and return their output unchanged, so the same definition and arguments give byte-identical JSON in every language. json::call validates the definition on every call. For repeated calls with one definition, build a FiscalCalendar once and use FiscalCalendar::call_json(op, args), which returns the same text.
let args = r#"{"period":{"level":"quarter","year":2023,"index":4}}"#;
let out = call;
assert_eq!;
assert_eq!;
Errors
Every failure is an Error value with a stable code, the JSON path of the offending field or argument, a message, an optional hint and optional details. In JSON, errors are returned as {"errors":[...]}. Match on code and path, not on the wording of message or hint. The codes are listed in the error catalogue.
When a label matches more than one period, parse returns AMBIGUOUS_LABEL with every candidate in details, and never picks one:
use ;
let two_schemes = from_json.unwrap;
let err = two_schemes.parse.unwrap_err;
assert_eq!;
assert_eq!;
let Some = &err.details else ;
let found: = candidates
.iter
.map
.collect;
assert_eq!;
Properties
- Every period is a half-open interval
[start, end_exclusive);last_day()is derived. - Dates are civil dates (no time or time zone) in the proleptic Gregorian calendar. Fiscal years must lie wholly between 0001-01-02 and 9999-12-30.
#![no_std]withalloc, and#![forbid(unsafe_code)]. The crate reads no clock, environment variable or global state.- Invalid input returns an error value. The test suite feeds malformed input to every operation and checks that it never panics.
- Dependencies:
serde,serde_jsonandsha2. - Minimum supported Rust version: 1.85.
How the results are checked
- Shared conformance vectors are run by the Rust, Python and npm test suites. Expected results come from cited sources or from independent generators, never from fiscalclock itself. See
conformance/. - Week-based years are checked against Python's
date.isocalendar()for every year from 1600 to 2399, and a slow day-by-day reference implementation is compared with the core for every date from 1900 to 2200. - Property tests check that periods tile and nest, and that
shiftandformat/parseround-trip. - On every push to
main, and on every pull request that changes a file the package jobs read, CI runs all vectors through Rust on Linux and through the Python and npm packages on Linux, macOS and Windows, and requires the 14 raw outputs to match the Rust output byte for byte.
Supply chain
This package is built and published by the release workflow from a version tag on main, after a smoke test of the built packages on Linux, macOS and Windows and the maintainer's approval. Every release file has a build provenance attestation. To check the .crate file attached to the GitHub release:
Report vulnerabilities privately as described in SECURITY.md. Changes are listed in the changelog.
Licence
Licensed under either of Apache License, Version 2.0 or MIT licence at your option.