Date Range Utilities
A Rust crate for building and navigating inclusive date, time, and date-time ranges — yearly, semi-annual, quarterly, monthly, semi-monthly, bi-weekly, and weekly — built on top of joda_rs (LocalDate, LocalTime, LocalDateTime).
Highlights:
- Inclusive ranges (start and end dates are both included)
- Construct a period from either its start date or its end date
- Navigate to the prior or next adjacent period with consistent, leap-year-aware logic
- Query ranges:
contains_date,contains_range,overlaps,range_containing_date,ranges_before/ranges_after/ranges_window, and more TimeRangeandDateTimeRangefor time-of-day and timestamp spans, including overlap/duration calculations
Installation
Add the crate to your Cargo.toml:
[]
= "0.1"
= "0.1"
If you're developing locally against an unpublished sibling crate, use a path dependency instead:
[]
= { = "./path/to/date-range-rs" }
= "0.1"
Quick start
use AnnualDateRange;
use LocalDate;
Date range types
Every period type builds a DateRange, so they all share the same query and navigation API.
| Type | Construct from |
|---|---|
AnnualDateRange |
with_start_date / with_end_date |
SemiAnnualDateRange |
with_start_date / with_end_date |
QuarterlyDateRange |
with_start_date / with_end_date |
MonthlyDateRange |
with_end_date_on_first / with_end_date_and_start_day |
SemiMonthlyDateRange |
with_end_date |
BiWeeklyDateRange |
with_start_date / with_end_date / with_target_date |
WeeklyDateRange |
with_start_date / with_end_date / with_target_date |
Each of these returns a DateRange, which exposes:
start_date(),end_date(),len()— the bounds and inclusive day countiter(),dates(),date_at(index),dates_for_day(day)— iterate or collect the dates in the rangecontains_date(date),contains_range(other),overlaps(other),overlaps_any(others)— membership and overlap checksprior(),next(),prior_n(n),next_n(n)— move to adjacent rangesrange_containing_date(date)— find the range (walking prior/next) that contains a dateranges_before(n),ranges_before_inclusive(n),ranges_after(n),ranges_after_inclusive(n),ranges_window(before, after),ranges_containing_span(from, to)— build lists of adjacent ranges
TimeRange and DateTimeRange
TimeRange (time-of-day spans) and DateTimeRange (timestamp spans) provide the same style of API for non-date-only ranges:
use DateTimeRange;
use ;
let workday = from_time_range_on_date;
assert!;
DateTimeRange also supports overlaps, overlaps_exclusive, overlaps_completely, overlap_range, overlap_duration, and duration(); TimeRange supports overlaps and duration(), including midnight-crossing spans.
Testing
License
MIT — see Cargo.toml and the repository for details.